GeneralPhpdocAnnotationRemoveFixerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. final class GeneralPhpdocAnnotationRemoveFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param array<string, mixed> $config
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null, array $config = []): void
  29. {
  30. $this->fixer->configure($config);
  31. $this->doTest($expected, $input);
  32. }
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'An Annotation gets removed' => [
  36. '<?php
  37. /**
  38. * @internal
  39. */
  40. function hello($name)
  41. {
  42. return "hello " . $name;
  43. }',
  44. '<?php
  45. /**
  46. * @internal
  47. * @param string $name
  48. */
  49. function hello($name)
  50. {
  51. return "hello " . $name;
  52. }',
  53. ['annotations' => ['param']],
  54. ];
  55. yield 'It removes multiple annotations' => [
  56. '<?php
  57. /**
  58. * @author me
  59. * @internal
  60. */
  61. function hello($name)
  62. {
  63. return "hello " . $name;
  64. }',
  65. '<?php
  66. /**
  67. * @author me
  68. * @internal
  69. * @param string $name
  70. * @return string
  71. * @throws \Exception
  72. */
  73. function hello($name)
  74. {
  75. return "hello " . $name;
  76. }',
  77. ['annotations' => ['param', 'return', 'throws']],
  78. ];
  79. yield 'It does nothing if no configuration is given' => [
  80. '<?php
  81. /**
  82. * @author me
  83. * @internal
  84. * @param string $name
  85. * @return string
  86. * @throws \Exception
  87. */
  88. function hello($name)
  89. {
  90. return "hello " . $name;
  91. }',
  92. ];
  93. yield 'It works on multiple functions' => [
  94. '<?php
  95. /**
  96. * @param string $name
  97. * @throws \Exception
  98. */
  99. function hello($name)
  100. {
  101. return "hello " . $name;
  102. }
  103. /**
  104. */
  105. function goodBye()
  106. {
  107. return 0;
  108. }
  109. function noComment()
  110. {
  111. callOtherFunction();
  112. }',
  113. '<?php
  114. /**
  115. * @author me
  116. * @internal
  117. * @param string $name
  118. * @return string
  119. * @throws \Exception
  120. */
  121. function hello($name)
  122. {
  123. return "hello " . $name;
  124. }
  125. /**
  126. * @internal
  127. * @author Piet-Henk
  128. * @return int
  129. */
  130. function goodBye()
  131. {
  132. return 0;
  133. }
  134. function noComment()
  135. {
  136. callOtherFunction();
  137. }',
  138. ['annotations' => ['author', 'return', 'internal']],
  139. ];
  140. yield 'Nothing happens to non doc-block comments' => [
  141. '<?php
  142. /*
  143. * @internal
  144. * @param string $name
  145. */
  146. function hello($name)
  147. {
  148. return "hello " . $name;
  149. }',
  150. null,
  151. ['annotations' => ['internal', 'param', 'return']],
  152. ];
  153. yield 'Nothing happens if to be deleted annotations are not present' => [
  154. '<?php
  155. /**
  156. * @internal
  157. * @param string $name
  158. */
  159. function hello($name)
  160. {
  161. return "hello " . $name;
  162. }',
  163. null,
  164. ['annotations' => ['author', 'test', 'return', 'deprecated']],
  165. ];
  166. yield [
  167. '<?php
  168. while ($something = myFunction($foo)) {}
  169. ',
  170. '<?php
  171. /** @noinspection PhpAssignmentInConditionInspection */
  172. while ($something = myFunction($foo)) {}
  173. ',
  174. ['annotations' => ['noinspection']],
  175. ];
  176. yield [
  177. '<?php
  178. /**
  179. * @internal
  180. * @AuThOr Jane Doe
  181. */
  182. function foo() {}',
  183. '<?php
  184. /**
  185. * @internal
  186. * @author John Doe
  187. * @AuThOr Jane Doe
  188. */
  189. function foo() {}',
  190. ['annotations' => ['author'], 'case_sensitive' => true],
  191. ];
  192. yield [
  193. '<?php
  194. /**
  195. * @internal
  196. */
  197. function foo() {}',
  198. '<?php
  199. /**
  200. * @internal
  201. * @author John Doe
  202. * @AuThOr Jane Doe
  203. */
  204. function foo() {}',
  205. ['annotations' => ['author'], 'case_sensitive' => false],
  206. ];
  207. }
  208. }