CommentToPhpdocFixerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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\Comment;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Kuba Werłos <werlos@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer
  24. */
  25. final class CommentToPhpdocFixerTest 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 [
  40. '<?php /* header comment */ $foo = true; /* string $bar */ $bar = "baz";',
  41. ];
  42. yield [
  43. '<?php /* header comment */ $foo = true; /* $yoda string @var */',
  44. ];
  45. yield [
  46. '<?php /* header comment */ $foo = true; /* $yoda @var string */',
  47. ];
  48. yield [
  49. '<?php /* header comment */ $foo = true; /** @var string $bar */ $bar = "baz";',
  50. '<?php /* header comment */ $foo = true; /* @var string $bar */ $bar = "baz";',
  51. ];
  52. yield [
  53. '<?php /* header comment */ $foo = true; /** @var string $bar */ $bar = "baz";',
  54. '<?php /* header comment */ $foo = true; /*@var string $bar */ $bar = "baz";',
  55. ];
  56. yield [
  57. '<?php /* header comment */ $foo = true;
  58. /** @var string $bar */
  59. $bar = "baz";
  60. ',
  61. '<?php /* header comment */ $foo = true;
  62. /*** @var string $bar */
  63. $bar = "baz";
  64. ',
  65. ];
  66. yield [
  67. '<?php /* header comment */ $foo = true;
  68. /** @var string $bar */
  69. $bar = "baz";
  70. ',
  71. '<?php /* header comment */ $foo = true;
  72. // @var string $bar
  73. $bar = "baz";
  74. ',
  75. ];
  76. yield [
  77. '<?php /* header comment */ $foo = true;
  78. /** @var string $bar */
  79. $bar = "baz";
  80. ',
  81. '<?php /* header comment */ $foo = true;
  82. //@var string $bar
  83. $bar = "baz";
  84. ',
  85. ];
  86. yield [
  87. '<?php /* header comment */ $foo = true;
  88. /** @var string $bar */
  89. $bar = "baz";
  90. ',
  91. '<?php /* header comment */ $foo = true;
  92. # @var string $bar
  93. $bar = "baz";
  94. ',
  95. ];
  96. yield [
  97. '<?php /* header comment */ $foo = true;
  98. /** @var string $bar */
  99. $bar = "baz";
  100. ',
  101. '<?php /* header comment */ $foo = true;
  102. #@var string $bar
  103. $bar = "baz";
  104. ',
  105. ];
  106. yield [
  107. <<<'EOT'
  108. <?php /* header comment */ $foo = true;
  109. /**
  110. * @var string $bar
  111. */
  112. $bar = "baz";
  113. EOT,
  114. <<<'EOT'
  115. <?php /* header comment */ $foo = true;
  116. /*
  117. * @var string $bar
  118. */
  119. $bar = "baz";
  120. EOT,
  121. ];
  122. yield [
  123. <<<'EOT'
  124. <?php /* header comment */ $foo = true;
  125. /**
  126. * This is my var
  127. * @var string $foo
  128. * stop using it
  129. * @deprecated since 1.2
  130. */
  131. $foo = 1;
  132. EOT,
  133. <<<'EOT'
  134. <?php /* header comment */ $foo = true;
  135. // This is my var
  136. // @var string $foo
  137. // stop using it
  138. // @deprecated since 1.2
  139. $foo = 1;
  140. EOT,
  141. ];
  142. yield [
  143. <<<'EOT'
  144. <?php /* header comment */ $foo = true;
  145. for (;;) {
  146. /**
  147. * This is my var
  148. * @var string $foo
  149. */
  150. $foo = someValue();
  151. }
  152. EOT,
  153. <<<'EOT'
  154. <?php /* header comment */ $foo = true;
  155. for (;;) {
  156. // This is my var
  157. // @var string $foo
  158. $foo = someValue();
  159. }
  160. EOT,
  161. ];
  162. yield [
  163. <<<'EOT'
  164. <?php /* header comment */ $foo = true;
  165. /**
  166. * This is my var
  167. * @var string $foo
  168. * stop using it
  169. * @deprecated since 1.3
  170. */
  171. $foo = 1;
  172. EOT,
  173. <<<'EOT'
  174. <?php /* header comment */ $foo = true;
  175. # This is my var
  176. # @var string $foo
  177. # stop using it
  178. # @deprecated since 1.3
  179. $foo = 1;
  180. EOT,
  181. ];
  182. yield [
  183. <<<'EOT'
  184. <?php /* header comment */ $foo = true;
  185. /**
  186. * @Column(type="string", length=32, unique=true, nullable=false)
  187. */
  188. $bar = 'baz';
  189. EOT,
  190. <<<'EOT'
  191. <?php /* header comment */ $foo = true;
  192. /*
  193. * @Column(type="string", length=32, unique=true, nullable=false)
  194. */
  195. $bar = 'baz';
  196. EOT,
  197. ];
  198. yield [
  199. <<<'EOT'
  200. <?php /* header comment */ $foo = true;
  201. /**
  202. * @ORM\Column(name="id", type="integer")
  203. */
  204. $bar = 42;
  205. EOT,
  206. <<<'EOT'
  207. <?php /* header comment */ $foo = true;
  208. /*
  209. * @ORM\Column(name="id", type="integer")
  210. */
  211. $bar = 42;
  212. EOT,
  213. ];
  214. yield [
  215. <<<'EOT'
  216. <?php /* header comment */ $foo = true;
  217. // This is my var
  218. // /** @var string $foo */
  219. $foo = 1;
  220. EOT,
  221. ];
  222. yield [
  223. <<<'EOT'
  224. <?php /* header comment */ $foo = true;
  225. // @todo do something later
  226. $foo = 1;
  227. EOT,
  228. null,
  229. ['ignored_tags' => ['todo']],
  230. ];
  231. yield [
  232. <<<'EOT'
  233. <?php /* header comment */ $foo = true;
  234. // @TODO do something later
  235. $foo = 1;
  236. EOT,
  237. null,
  238. ['ignored_tags' => ['todo']],
  239. ];
  240. yield [
  241. <<<'EOT'
  242. <?php /* header comment */ $foo = true;
  243. /**
  244. * @todo do something later
  245. * @var int $foo
  246. */
  247. $foo = 1;
  248. EOT,
  249. <<<'EOT'
  250. <?php /* header comment */ $foo = true;
  251. // @todo do something later
  252. // @var int $foo
  253. $foo = 1;
  254. EOT,
  255. ['ignored_tags' => ['todo']],
  256. ];
  257. yield [
  258. <<<'EOT'
  259. <?php /* header comment */ $foo = true;
  260. /**
  261. * @var int $foo
  262. * @todo do something later
  263. */
  264. $foo = 1;
  265. EOT,
  266. <<<'EOT'
  267. <?php /* header comment */ $foo = true;
  268. // @var int $foo
  269. // @todo do something later
  270. $foo = 1;
  271. EOT,
  272. ['ignored_tags' => ['todo']],
  273. ];
  274. yield [
  275. '<?php // header
  276. /** /@foo */
  277. namespace Foo\Bar;
  278. ',
  279. '<?php // header
  280. ///@foo
  281. namespace Foo\Bar;
  282. ',
  283. ];
  284. yield [
  285. '<?php // header
  286. /**
  287. * / @foo
  288. * / @bar
  289. */
  290. namespace Foo\Bar;
  291. ',
  292. '<?php // header
  293. /// @foo
  294. /// @bar
  295. namespace Foo\Bar;
  296. ',
  297. ];
  298. yield [
  299. '<?php /* header comment */ $foo = true; class Foo { /** @phpstan-use Bar<Baz> $bar */ use Bar; }',
  300. '<?php /* header comment */ $foo = true; class Foo { /* @phpstan-use Bar<Baz> $bar */ use Bar; }',
  301. ];
  302. }
  303. }