SpaceAfterSemicolonFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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\Semicolon;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer
  22. */
  23. final class SpaceAfterSemicolonFixerTest 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. * @dataProvider provideFixCases
  34. */
  35. public function testFixWithSpacesInEmptyForExpressions(string $expected, ?string $input = null): void
  36. {
  37. $this->fixer->configure([
  38. 'remove_in_empty_for_expressions' => false,
  39. ]);
  40. $this->doTest($expected, $input);
  41. }
  42. /**
  43. * @return iterable<array{0: string, 1?: string}>
  44. */
  45. public static function provideFixCases(): iterable
  46. {
  47. yield [
  48. '<?php
  49. test1();
  50. $a; // test
  51. ',
  52. ];
  53. yield [
  54. '<?php test2();',
  55. ];
  56. yield [
  57. '<?php test3(); ',
  58. ];
  59. yield [
  60. '<?php test4(); ',
  61. ];
  62. yield [
  63. '<?php
  64. test5(); // test
  65. ',
  66. ];
  67. yield [
  68. '<?php test6(); /* */ //',
  69. ];
  70. yield [
  71. '<?php test7a(); /* */',
  72. '<?php test7a();/* */',
  73. ];
  74. yield [
  75. '<?php test7b(); /* *//**/',
  76. '<?php test7b();/* *//**/',
  77. ];
  78. yield [
  79. '<?php
  80. test8(); $a = 4;
  81. ',
  82. '<?php
  83. test8(); $a = 4;
  84. ',
  85. ];
  86. yield [
  87. '<?php
  88. test9(); $b = 7;
  89. ',
  90. '<?php
  91. test9();$b = 7;
  92. ',
  93. ];
  94. yield [
  95. '<?php
  96. for (; ;) {
  97. }
  98. ',
  99. '<?php
  100. for (;;) {
  101. }
  102. ',
  103. ];
  104. yield [
  105. '<?php
  106. for (; ; ++$u1) {
  107. }
  108. ',
  109. '<?php
  110. for (;;++$u1) {
  111. }
  112. ',
  113. ];
  114. yield [
  115. '<?php
  116. for (; $u2 < 0;) {
  117. }
  118. ',
  119. '<?php
  120. for (;$u2 < 0;) {
  121. }
  122. ',
  123. ];
  124. yield [
  125. '<?php
  126. for (; $u3 < 3; ++$u3) {
  127. }
  128. ',
  129. '<?php
  130. for (;$u3 < 3;++$u3) {
  131. }
  132. ',
  133. ];
  134. yield [
  135. '<?php
  136. for ($u4 = 0; ;) {
  137. }
  138. ',
  139. '<?php
  140. for ($u4 = 0;;) {
  141. }
  142. ',
  143. ];
  144. yield [
  145. '<?php
  146. for ($u5 = 0; ; ++$u5) {
  147. }
  148. ',
  149. '<?php
  150. for ($u5 = 0;;++$u5) {
  151. }
  152. ',
  153. ];
  154. yield [
  155. '<?php
  156. for ($u6 = 0; $u6 < 6;) {
  157. }
  158. ',
  159. '<?php
  160. for ($u6 = 0;$u6 < 6;) {
  161. }
  162. ',
  163. ];
  164. yield [
  165. '<?php
  166. for ($u7 = 0; $u7 < 7; ++$u7) {
  167. }
  168. ',
  169. '<?php
  170. for ($u7 = 0;$u7 < 7;++$u7) {
  171. }
  172. ',
  173. ];
  174. yield [
  175. '<?php
  176. for (; ; ) {
  177. }
  178. ',
  179. '<?php
  180. for (; ; ) {
  181. }
  182. ',
  183. ];
  184. yield [
  185. '<?php
  186. for (; ; ++$u1) {
  187. }
  188. ',
  189. '<?php
  190. for (; ; ++$u1) {
  191. }
  192. ',
  193. ];
  194. yield [
  195. '<?php
  196. for (; $u2 < 0; ) {
  197. }
  198. ',
  199. '<?php
  200. for (; $u2 < 0; ) {
  201. }
  202. ',
  203. ];
  204. yield [
  205. '<?php
  206. for (; $u3 < 3; ++$u3) {
  207. }
  208. ',
  209. '<?php
  210. for (; $u3 < 3; ++$u3) {
  211. }
  212. ',
  213. ];
  214. yield [
  215. '<?php
  216. for ($ui4 = 0; ; ) {
  217. }
  218. ',
  219. '<?php
  220. for ($ui4 = 0; ; ) {
  221. }
  222. ',
  223. ];
  224. yield [
  225. '<?php
  226. for ($u5 = 0; ; ++$u5) {
  227. }
  228. ',
  229. '<?php
  230. for ($u5 = 0; ; ++$u5) {
  231. }
  232. ',
  233. ];
  234. yield [
  235. '<?php
  236. for ($u6 = 0; $u6 < 6; ) {
  237. }
  238. ',
  239. '<?php
  240. for ($u6 = 0; $u6 < 6; ) {
  241. }
  242. ',
  243. ];
  244. yield [
  245. '<?php
  246. for ($u7 = 0; $u7 < 7; ++$u7) {
  247. }
  248. ',
  249. '<?php
  250. for ($u7 = 0; $u7 < 7; ++$u7) {
  251. }
  252. ',
  253. ];
  254. yield [
  255. '<?php if ($a):?>
  256. 1
  257. <?php endif; ?>
  258. <?php if ($b):?>
  259. 2
  260. <?php endif; ?>
  261. <?php if ($c):?>
  262. 3
  263. <?php endif; ?>',
  264. '<?php if ($a):?>
  265. 1
  266. <?php endif;?>
  267. <?php if ($b):?>
  268. 2
  269. <?php endif;?>
  270. <?php if ($c):?>
  271. 3
  272. <?php endif;?>',
  273. ];
  274. yield [
  275. '<?php echo 1; ; ; ; ; ; ; ; ;',
  276. '<?php echo 1;;;;;;;;;',
  277. ];
  278. }
  279. /**
  280. * @dataProvider provideFixWithoutSpacesInEmptyForExpressionsCases
  281. */
  282. public function testFixWithoutSpacesInEmptyForExpressions(string $expected, ?string $input = null): void
  283. {
  284. $this->fixer->configure([
  285. 'remove_in_empty_for_expressions' => true,
  286. ]);
  287. $this->doTest($expected, $input);
  288. }
  289. /**
  290. * @return iterable<array{0: string, 1?: string}>
  291. */
  292. public static function provideFixWithoutSpacesInEmptyForExpressionsCases(): iterable
  293. {
  294. yield [
  295. '<?php
  296. test1();
  297. $a; // test
  298. ',
  299. ];
  300. yield [
  301. '<?php test2();',
  302. ];
  303. yield [
  304. '<?php test3(); ',
  305. ];
  306. yield [
  307. '<?php test4(); ',
  308. ];
  309. yield [
  310. '<?php
  311. test5(); // test
  312. ',
  313. ];
  314. yield [
  315. '<?php test6(); /* */ //',
  316. ];
  317. yield [
  318. '<?php test7a(); /* */',
  319. '<?php test7a();/* */',
  320. ];
  321. yield [
  322. '<?php test7b(); /* *//**/',
  323. '<?php test7b();/* *//**/',
  324. ];
  325. yield [
  326. '<?php
  327. test8(); $a = 4;
  328. ',
  329. '<?php
  330. test8(); $a = 4;
  331. ',
  332. ];
  333. yield [
  334. '<?php
  335. test9(); $b = 7;
  336. ',
  337. '<?php
  338. test9();$b = 7;
  339. ',
  340. ];
  341. yield [
  342. '<?php
  343. for (;;) {
  344. }
  345. ',
  346. '<?php
  347. for (; ;) {
  348. }
  349. ',
  350. ];
  351. yield [
  352. '<?php
  353. for (;; ++$u1) {
  354. }
  355. ',
  356. '<?php
  357. for (;;++$u1) {
  358. }
  359. ',
  360. ];
  361. yield [
  362. '<?php
  363. for (; $u2 < 0;) {
  364. }
  365. ',
  366. '<?php
  367. for (;$u2 < 0;) {
  368. }
  369. ',
  370. ];
  371. yield [
  372. '<?php
  373. for (; $u3 < 3; ++$u3) {
  374. }
  375. ',
  376. '<?php
  377. for (;$u3 < 3;++$u3) {
  378. }
  379. ',
  380. ];
  381. yield [
  382. '<?php
  383. for ($u4 = 0;;) {
  384. }
  385. ',
  386. '<?php
  387. for ($u4 = 0; ;) {
  388. }
  389. ',
  390. ];
  391. yield [
  392. '<?php
  393. for ($u5 = 0;; ++$u5) {
  394. }
  395. ',
  396. '<?php
  397. for ($u5 = 0;;++$u5) {
  398. }
  399. ',
  400. ];
  401. yield [
  402. '<?php
  403. for ($u6 = 0; $u6 < 6;) {
  404. }
  405. ',
  406. '<?php
  407. for ($u6 = 0;$u6 < 6;) {
  408. }
  409. ',
  410. ];
  411. yield [
  412. '<?php
  413. for ($u7 = 0; $u7 < 7; ++$u7) {
  414. }
  415. ',
  416. '<?php
  417. for ($u7 = 0;$u7 < 7;++$u7) {
  418. }
  419. ',
  420. ];
  421. yield [
  422. '<?php
  423. for (;;) {
  424. }
  425. ',
  426. '<?php
  427. for (; ; ) {
  428. }
  429. ',
  430. ];
  431. yield [
  432. '<?php
  433. for (;; ++$u1) {
  434. }
  435. ',
  436. '<?php
  437. for (; ; ++$u1) {
  438. }
  439. ',
  440. ];
  441. yield [
  442. '<?php
  443. for (; $u2 < 0;) {
  444. }
  445. ',
  446. '<?php
  447. for (; $u2 < 0; ) {
  448. }
  449. ',
  450. ];
  451. yield [
  452. '<?php
  453. for (; $u3 < 3; ++$u3) {
  454. }
  455. ',
  456. '<?php
  457. for (; $u3 < 3; ++$u3) {
  458. }
  459. ',
  460. ];
  461. yield [
  462. '<?php
  463. for ($ui4 = 0;;) {
  464. }
  465. ',
  466. '<?php
  467. for ($ui4 = 0; ; ) {
  468. }
  469. ',
  470. ];
  471. yield [
  472. '<?php
  473. for ($u5 = 0;; ++$u5) {
  474. }
  475. ',
  476. '<?php
  477. for ($u5 = 0; ; ++$u5) {
  478. }
  479. ',
  480. ];
  481. yield [
  482. '<?php
  483. for ($u6 = 0; $u6 < 6;) {
  484. }
  485. ',
  486. '<?php
  487. for ($u6 = 0; $u6 < 6; ) {
  488. }
  489. ',
  490. ];
  491. yield [
  492. '<?php
  493. for ($u7 = 0; $u7 < 7; ++$u7) {
  494. }
  495. ',
  496. '<?php
  497. for ($u7 = 0; $u7 < 7; ++$u7) {
  498. }
  499. ',
  500. ];
  501. yield [
  502. '<?php
  503. for (
  504. $u7 = 0;
  505. ;
  506. ++$u7
  507. ) {
  508. }
  509. ',
  510. ];
  511. yield [
  512. '<?php for ( /* foo */ ; /* bar */ ; /* baz */ ) { }',
  513. ];
  514. }
  515. public function testHaltCompiler(): void
  516. {
  517. $this->doTest('<?php
  518. __HALT_COMPILER();
  519. ');
  520. }
  521. }