GeneralPhpdocAnnotationRemoveFixerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Phpdoc;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @author Gert de Pagter
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer
  24. */
  25. final class GeneralPhpdocAnnotationRemoveFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @param _AutogeneratedInputConfiguration $config
  29. *
  30. * @dataProvider provideFixCases
  31. */
  32. public function testFix(string $expected, ?string $input = null, array $config = []): void
  33. {
  34. $this->fixer->configure($config);
  35. $this->doTest($expected, $input);
  36. }
  37. public static function provideFixCases(): iterable
  38. {
  39. yield 'An Annotation gets removed' => [
  40. '<?php
  41. /**
  42. * @internal
  43. */
  44. function hello($name)
  45. {
  46. return "hello " . $name;
  47. }',
  48. '<?php
  49. /**
  50. * @internal
  51. * @param string $name
  52. */
  53. function hello($name)
  54. {
  55. return "hello " . $name;
  56. }',
  57. ['annotations' => ['param']],
  58. ];
  59. yield 'It removes multiple annotations' => [
  60. '<?php
  61. /**
  62. * @author me
  63. * @internal
  64. */
  65. function hello($name)
  66. {
  67. return "hello " . $name;
  68. }',
  69. '<?php
  70. /**
  71. * @author me
  72. * @internal
  73. * @param string $name
  74. * @return string
  75. * @throws \Exception
  76. */
  77. function hello($name)
  78. {
  79. return "hello " . $name;
  80. }',
  81. ['annotations' => ['param', 'return', 'throws']],
  82. ];
  83. yield 'It does nothing if no configuration is given' => [
  84. '<?php
  85. /**
  86. * @author me
  87. * @internal
  88. * @param string $name
  89. * @return string
  90. * @throws \Exception
  91. */
  92. function hello($name)
  93. {
  94. return "hello " . $name;
  95. }',
  96. ];
  97. yield 'It works on multiple functions' => [
  98. '<?php
  99. /**
  100. * @param string $name
  101. * @throws \Exception
  102. */
  103. function hello($name)
  104. {
  105. return "hello " . $name;
  106. }
  107. /**
  108. */
  109. function goodBye()
  110. {
  111. return 0;
  112. }
  113. function noComment()
  114. {
  115. callOtherFunction();
  116. }',
  117. '<?php
  118. /**
  119. * @author me
  120. * @internal
  121. * @param string $name
  122. * @return string
  123. * @throws \Exception
  124. */
  125. function hello($name)
  126. {
  127. return "hello " . $name;
  128. }
  129. /**
  130. * @internal
  131. * @author Piet-Henk
  132. * @return int
  133. */
  134. function goodBye()
  135. {
  136. return 0;
  137. }
  138. function noComment()
  139. {
  140. callOtherFunction();
  141. }',
  142. ['annotations' => ['author', 'return', 'internal']],
  143. ];
  144. yield 'Nothing happens to non doc-block comments' => [
  145. '<?php
  146. /*
  147. * @internal
  148. * @param string $name
  149. */
  150. function hello($name)
  151. {
  152. return "hello " . $name;
  153. }',
  154. null,
  155. ['annotations' => ['internal', 'param', 'return']],
  156. ];
  157. yield 'Nothing happens if to be deleted annotations are not present' => [
  158. '<?php
  159. /**
  160. * @internal
  161. * @param string $name
  162. */
  163. function hello($name)
  164. {
  165. return "hello " . $name;
  166. }',
  167. null,
  168. ['annotations' => ['author', 'test', 'return', 'deprecated']],
  169. ];
  170. yield [
  171. '<?php
  172. while ($something = myFunction($foo)) {}
  173. ',
  174. '<?php
  175. /** @noinspection PhpAssignmentInConditionInspection */
  176. while ($something = myFunction($foo)) {}
  177. ',
  178. ['annotations' => ['noinspection']],
  179. ];
  180. yield [
  181. '<?php
  182. /**
  183. * @internal
  184. * @AuThOr Jane Doe
  185. */
  186. function foo() {}',
  187. '<?php
  188. /**
  189. * @internal
  190. * @author John Doe
  191. * @AuThOr Jane Doe
  192. */
  193. function foo() {}',
  194. ['annotations' => ['author'], 'case_sensitive' => true],
  195. ];
  196. yield [
  197. '<?php
  198. /**
  199. * @internal
  200. */
  201. function foo() {}',
  202. '<?php
  203. /**
  204. * @internal
  205. * @author John Doe
  206. * @AuThOr Jane Doe
  207. */
  208. function foo() {}',
  209. ['annotations' => ['author'], 'case_sensitive' => false],
  210. ];
  211. }
  212. }