NoEmptyCommentFixerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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\Fixer\Comment\NoEmptyCommentFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer>
  22. */
  23. final class NoEmptyCommentFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. // fix cases
  38. yield [
  39. '<?php
  40. echo 0;
  41. echo 1;
  42. ',
  43. '<?php
  44. echo 0;//
  45. echo 1;
  46. ',
  47. ];
  48. yield [
  49. '<?php
  50. echo 0;
  51. echo 1;
  52. ',
  53. '<?php
  54. echo 0;//
  55. echo 1;
  56. ',
  57. ];
  58. yield [
  59. '<?php
  60. echo 1;
  61. ',
  62. '<?php
  63. echo 1;//
  64. ',
  65. ];
  66. yield [
  67. '<?php
  68. echo 2;
  69. '.'
  70. echo 1;
  71. ',
  72. '<?php
  73. echo 2;
  74. //
  75. echo 1;
  76. ',
  77. ];
  78. yield [
  79. '<?php
  80. ?>',
  81. '<?php
  82. //?>',
  83. ];
  84. yield [
  85. '<?php
  86. '.'
  87. ',
  88. '<?php
  89. //
  90. ',
  91. ];
  92. yield [
  93. '<?php
  94. '.'
  95. ',
  96. '<?php
  97. #
  98. ',
  99. ];
  100. yield [
  101. '<?php
  102. '.'
  103. ',
  104. '<?php
  105. /**/
  106. ',
  107. ];
  108. yield [
  109. '<?php
  110. echo 0;echo 1;
  111. ',
  112. '<?php
  113. echo 0;/**/echo 1;
  114. ',
  115. ];
  116. yield [
  117. '<?php
  118. echo 0;echo 1;
  119. ',
  120. '<?php
  121. echo 0;/**//**//**/echo 1/**/;
  122. ',
  123. ];
  124. yield [
  125. '<?php
  126. ',
  127. '<?php
  128. //',
  129. ];
  130. yield [
  131. '<?php
  132. ',
  133. '<?php
  134. /*
  135. */',
  136. ];
  137. yield [
  138. "<?php\n \n \n \n \n ",
  139. "<?php\n //\n //\n //\n /**///\n ",
  140. ];
  141. yield [
  142. "<?php\r \r \r \r \r ",
  143. "<?php\r //\r //\r //\r /**///\r ",
  144. ];
  145. yield [
  146. "<?php\r\n \r\n \r\n \r\n \r\n ",
  147. "<?php\r\n //\r\n //\r\n //\r\n /**///\r\n ",
  148. ];
  149. yield [
  150. "<?php\necho 1;\r\recho 2;",
  151. "<?php\necho 1;\r//\recho 2;",
  152. ];
  153. // do not fix cases
  154. yield [
  155. '<?php
  156. // a
  157. // /**/
  158. // #
  159. /* b */ // s
  160. # c',
  161. ];
  162. yield [
  163. '<?php
  164. // This comment could be nicely formatted.
  165. //
  166. //
  167. // For that, it could have some empty comment lines inside.
  168. //
  169. ## A 1
  170. ##
  171. ##
  172. ## A 2
  173. ##
  174. // B 1
  175. //
  176. // B 2
  177. ## C 1
  178. ##
  179. ## C 2
  180. $foo = 1;
  181. //
  182. // a
  183. //
  184. $bar = 2;
  185. ',
  186. ];
  187. yield [
  188. '<?php
  189. '.'
  190. ',
  191. '<?php
  192. /*
  193. *
  194. */
  195. ',
  196. ];
  197. yield [
  198. '<?php
  199. '.'
  200. ',
  201. '<?php
  202. /********
  203. *
  204. ********/
  205. ',
  206. ];
  207. yield [
  208. '<?php /* a */',
  209. '<?php /* *//* a *//* */',
  210. ];
  211. yield [
  212. '<?php
  213. '.'
  214. /* a */
  215. '.'
  216. ',
  217. '<?php
  218. //
  219. /* a */
  220. //
  221. ',
  222. ];
  223. }
  224. /**
  225. * @param string $source valid PHP source code
  226. * @param int $startIndex start index of the comment block
  227. * @param int $endIndex expected index of the last token of the block
  228. * @param bool $isEmpty expected value of empty flag returned
  229. *
  230. * @dataProvider provideGetCommentBlockCases
  231. */
  232. public function testGetCommentBlock(string $source, int $startIndex, int $endIndex, bool $isEmpty): void
  233. {
  234. Tokens::clearCache();
  235. $tokens = Tokens::fromCode($source);
  236. self::assertTrue($tokens[$startIndex]->isComment(), \sprintf('Misconfiguration of test, expected comment token at index "%d".', $startIndex));
  237. $foundInfo = \Closure::bind(static fn (NoEmptyCommentFixer $fixer): array => $fixer->getCommentBlock($tokens, $startIndex), null, NoEmptyCommentFixer::class)($this->fixer);
  238. self::assertSame($startIndex, $foundInfo['blockStart'], 'Find start index of block failed.');
  239. self::assertSame($endIndex, $foundInfo['blockEnd'], 'Find end index of block failed.');
  240. self::assertSame($isEmpty, $foundInfo['isEmpty'], 'Is empty comment block detection failed.');
  241. }
  242. /**
  243. * @return iterable<array{string, int, int, bool}>
  244. */
  245. public static function provideGetCommentBlockCases(): iterable
  246. {
  247. yield [
  248. '<?php // a',
  249. 1,
  250. 1,
  251. false,
  252. ];
  253. yield [
  254. '<?php
  255. // This comment could be nicely formatted.
  256. //
  257. //
  258. // For that, it could have some empty comment lines inside.
  259. // ',
  260. 2,
  261. 11,
  262. false,
  263. ];
  264. yield [
  265. '<?php
  266. /**///',
  267. 1,
  268. 1,
  269. true,
  270. ];
  271. yield [
  272. '<?php
  273. //
  274. //
  275. #
  276. #
  277. ',
  278. 5,
  279. 8,
  280. true,
  281. ];
  282. yield [
  283. '<?php
  284. //
  285. //
  286. //
  287. //
  288. ',
  289. 5,
  290. 8,
  291. true,
  292. ];
  293. yield [
  294. '<?php
  295. //
  296. //
  297. //
  298. //
  299. ',
  300. 1,
  301. 3,
  302. true,
  303. ];
  304. yield [
  305. str_replace("\n", "\r", "<?php\n//\n//\n\n//\n//\n"),
  306. 1,
  307. 3,
  308. true,
  309. ];
  310. yield [
  311. str_replace("\n", "\r\n", "<?php\n//\n//\n\n//\n//\n"),
  312. 1,
  313. 3,
  314. true,
  315. ];
  316. yield [
  317. '<?php
  318. //
  319. //
  320. ',
  321. 1,
  322. 1,
  323. true,
  324. ];
  325. yield [
  326. '<?php
  327. //
  328. //
  329. $a; ',
  330. 1,
  331. 4,
  332. true,
  333. ];
  334. yield [
  335. '<?php
  336. //',
  337. 1,
  338. 1,
  339. true,
  340. ];
  341. $src = '<?php
  342. // a2
  343. // /*4*/
  344. // #6
  345. /* b8 */ // s10
  346. # c12';
  347. foreach ([2, 4, 6] as $i) {
  348. yield [$src, $i, 7, false];
  349. }
  350. yield [$src, 8, 8, false];
  351. yield [$src, 10, 11, false];
  352. yield [$src, 12, 12, false];
  353. }
  354. }