NoEmptyCommentFixerTest.php 8.5 KB

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