NoEmptyCommentFixerTest.php 8.6 KB

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