NoBreakCommentFixerTest.php 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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\ControlStructure;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer
  20. */
  21. final class NoBreakCommentFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFixWithExplicitDefaultConfiguration(string $expected, ?string $input = null): void
  34. {
  35. $this->fixer->configure([
  36. 'comment_text' => 'no break',
  37. ]);
  38. $this->doTest($expected, $input);
  39. }
  40. public function provideFixCases(): array
  41. {
  42. return [
  43. [
  44. '<?php
  45. switch ($foo) {
  46. case 1:
  47. foo();
  48. // no break
  49. case 2:
  50. bar();
  51. // no break
  52. default:
  53. baz();
  54. }',
  55. '<?php
  56. switch ($foo) {
  57. case 1:
  58. foo();
  59. case 2:
  60. bar();
  61. default:
  62. baz();
  63. }',
  64. ],
  65. [
  66. '<?php
  67. switch ($foo) {
  68. case 1:
  69. foo();
  70. // no break
  71. case 2:
  72. bar();
  73. // no break
  74. default:
  75. baz();
  76. }',
  77. '<?php
  78. switch ($foo) {
  79. case 1:
  80. foo();
  81. case 2:
  82. bar();
  83. default:
  84. baz();
  85. }',
  86. ],
  87. [
  88. '<?php
  89. switch ($foo) {
  90. case 1:
  91. foo();
  92. // no break
  93. case 2:
  94. bar();
  95. // no break
  96. default:
  97. baz();
  98. }',
  99. '<?php
  100. switch ($foo) {
  101. case 1:
  102. foo(); // no break
  103. case 2:
  104. bar(); // no break
  105. default:
  106. baz();
  107. }',
  108. ],
  109. [
  110. '<?php
  111. switch ($foo) {
  112. case 1;
  113. foo();
  114. // no break
  115. case 2;
  116. bar();
  117. // no break
  118. default;
  119. baz();
  120. }',
  121. '<?php
  122. switch ($foo) {
  123. case 1;
  124. foo();
  125. case 2;
  126. bar();
  127. default;
  128. baz();
  129. }',
  130. ],
  131. [
  132. '<?php
  133. switch ($foo) {
  134. case 1:
  135. foo();
  136. // foo
  137. // no break
  138. case 2:
  139. bar();
  140. }',
  141. '<?php
  142. switch ($foo) {
  143. case 1:
  144. foo();
  145. // foo
  146. case 2:
  147. bar();
  148. }',
  149. ],
  150. [
  151. '<?php
  152. switch ($foo) { case 1: foo();
  153. // no break
  154. case 2: bar(); }',
  155. '<?php
  156. switch ($foo) { case 1: foo(); case 2: bar(); }',
  157. ],
  158. [
  159. '<?php
  160. switch ($foo) { case 1: foo();
  161. // no break
  162. case 2: bar(); }',
  163. '<?php
  164. switch ($foo) { case 1: foo();case 2: bar(); }',
  165. ],
  166. [
  167. '<?php
  168. switch ($foo) {
  169. case 1;
  170. foreach ($bar as $baz) {
  171. break;
  172. }
  173. // no break
  174. case 2;
  175. bar();
  176. }',
  177. '<?php
  178. switch ($foo) {
  179. case 1;
  180. foreach ($bar as $baz) {
  181. break;
  182. }
  183. case 2;
  184. bar();
  185. }',
  186. ],
  187. [
  188. '<?php
  189. switch ($foo) {
  190. case 1;
  191. for ($i = 0; $i < 1; ++$i) {
  192. break;
  193. }
  194. // no break
  195. case 2;
  196. bar();
  197. }',
  198. '<?php
  199. switch ($foo) {
  200. case 1;
  201. for ($i = 0; $i < 1; ++$i) {
  202. break;
  203. }
  204. case 2;
  205. bar();
  206. }',
  207. ],
  208. [
  209. '<?php
  210. switch ($foo) {
  211. case 1;
  212. foreach ($bar as $baz) {
  213. break;
  214. }
  215. // no break
  216. case 2;
  217. bar();
  218. }',
  219. '<?php
  220. switch ($foo) {
  221. case 1;
  222. foreach ($bar as $baz) {
  223. break;
  224. }
  225. case 2;
  226. bar();
  227. }',
  228. ],
  229. [
  230. '<?php
  231. switch ($foo) {
  232. case 1;
  233. if ($foo) {
  234. break;
  235. }
  236. // no break
  237. case 2;
  238. bar();
  239. }',
  240. '<?php
  241. switch ($foo) {
  242. case 1;
  243. if ($foo) {
  244. break;
  245. }
  246. case 2;
  247. bar();
  248. }',
  249. ],
  250. [
  251. '<?php
  252. switch ($foo) {
  253. case 1;
  254. do {
  255. break;
  256. } while ($bar);
  257. // no break
  258. case 2;
  259. bar();
  260. }',
  261. '<?php
  262. switch ($foo) {
  263. case 1;
  264. do {
  265. break;
  266. } while ($bar);
  267. case 2;
  268. bar();
  269. }',
  270. ],
  271. [
  272. '<?php
  273. switch ($foo) {
  274. case 1;
  275. $foo = function ($bar) {
  276. foreach ($bar as $baz) {
  277. break;
  278. }
  279. };
  280. // no break
  281. case 2;
  282. bar();
  283. }',
  284. '<?php
  285. switch ($foo) {
  286. case 1;
  287. $foo = function ($bar) {
  288. foreach ($bar as $baz) {
  289. break;
  290. }
  291. };
  292. case 2;
  293. bar();
  294. }',
  295. ],
  296. [
  297. '<?php
  298. switch ($foo) {
  299. case 1:
  300. switch ($bar) {
  301. case 1:
  302. foo();
  303. // no break
  304. case 2:
  305. bar();
  306. }
  307. break;
  308. case 2:
  309. switch ($bar) {
  310. case 1:
  311. bar();
  312. // no break
  313. case 2:
  314. foo();
  315. }
  316. }',
  317. '<?php
  318. switch ($foo) {
  319. case 1:
  320. switch ($bar) {
  321. case 1:
  322. foo();
  323. case 2:
  324. bar();
  325. }
  326. break;
  327. case 2:
  328. switch ($bar) {
  329. case 1:
  330. bar();
  331. case 2:
  332. foo();
  333. }
  334. }',
  335. ],
  336. [
  337. '<?php
  338. switch ($foo) {
  339. case 1:
  340. switch ($bar):
  341. case 1:
  342. foo();
  343. // no break
  344. case 2:
  345. bar();
  346. endswitch;
  347. break;
  348. case 2:
  349. switch ($bar):
  350. case 1:
  351. bar();
  352. // no break
  353. case 2:
  354. foo();
  355. endswitch;
  356. }',
  357. '<?php
  358. switch ($foo) {
  359. case 1:
  360. switch ($bar):
  361. case 1:
  362. foo();
  363. case 2:
  364. bar();
  365. endswitch;
  366. break;
  367. case 2:
  368. switch ($bar):
  369. case 1:
  370. bar();
  371. case 2:
  372. foo();
  373. endswitch;
  374. }',
  375. ],
  376. [
  377. '<?php
  378. switch ($foo) {
  379. case 1:
  380. foo();
  381. continue;
  382. case 2:
  383. bar();
  384. continue;
  385. default:
  386. baz();
  387. }',
  388. '<?php
  389. switch ($foo) {
  390. case 1:
  391. foo();
  392. // no break
  393. continue;
  394. case 2:
  395. bar();
  396. // no break
  397. continue;
  398. default:
  399. baz();
  400. }',
  401. ],
  402. [
  403. '<?php
  404. switch ($foo) {
  405. case 1:
  406. return foo();
  407. case 2:
  408. return bar();
  409. default:
  410. return baz();
  411. }',
  412. '<?php
  413. switch ($foo) {
  414. case 1:
  415. return foo();
  416. // no break
  417. case 2:
  418. return bar();
  419. // no break
  420. default:
  421. return baz();
  422. }',
  423. ],
  424. [
  425. '<?php
  426. switch ($foo) {
  427. case 1:
  428. return foo();
  429. case 2:
  430. return bar();
  431. default:
  432. return baz();
  433. }',
  434. '<?php
  435. switch ($foo) {
  436. case 1:
  437. // no break
  438. return foo();
  439. case 2:
  440. // no break
  441. return bar();
  442. default:
  443. return baz();
  444. }',
  445. ],
  446. [
  447. '<?php
  448. switch ($foo) {
  449. case 1:
  450. foo();
  451. break;
  452. case 2:
  453. bar();
  454. break;
  455. default:
  456. baz();
  457. }',
  458. '<?php
  459. switch ($foo) {
  460. case 1:
  461. foo();
  462. // no break
  463. break;
  464. case 2:
  465. bar();
  466. // no break
  467. break;
  468. default:
  469. baz();
  470. }',
  471. ],
  472. [
  473. '<?php
  474. switch ($foo) {
  475. case 1:
  476. foo();
  477. break;
  478. case 2:
  479. bar();
  480. break;
  481. case 21:
  482. bar();
  483. break;
  484. case 22:
  485. bar();
  486. break;
  487. case 23:
  488. bar();
  489. break;
  490. case 24:
  491. bar();
  492. break;
  493. case 3:
  494. baz();
  495. break;
  496. default:
  497. qux();
  498. }',
  499. '<?php
  500. switch ($foo) {
  501. case 1:
  502. foo();
  503. # no break
  504. break;
  505. case 2:
  506. bar();
  507. /* no break */
  508. break;
  509. case 21:
  510. bar();
  511. /*no break*/
  512. break;
  513. case 22:
  514. bar();
  515. /* no break */
  516. break;
  517. case 23:
  518. bar();
  519. /*no break */
  520. break;
  521. case 24:
  522. bar();
  523. /* no break*/
  524. break;
  525. case 3:
  526. baz();
  527. /** no break */
  528. break;
  529. default:
  530. qux();
  531. }',
  532. ],
  533. [
  534. '<?php
  535. switch ($foo) {
  536. case 1:
  537. case 2:
  538. bar();
  539. break;
  540. default:
  541. baz();
  542. }',
  543. '<?php
  544. switch ($foo) {
  545. case 1:
  546. // no break
  547. case 2:
  548. bar();
  549. // no break
  550. break;
  551. default:
  552. baz();
  553. }',
  554. ],
  555. [
  556. '<?php
  557. switch ($foo) {
  558. case 1:
  559. foo();
  560. }',
  561. '<?php
  562. switch ($foo) {
  563. case 1:
  564. foo();
  565. // no break
  566. }',
  567. ],
  568. [
  569. '<?php
  570. switch ($foo) {
  571. default:
  572. foo();
  573. }',
  574. '<?php
  575. switch ($foo) {
  576. default:
  577. foo();
  578. // no break
  579. }',
  580. ],
  581. [
  582. '<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1;
  583. // no break
  584. case 2: $foo = 2; }
  585. // no break
  586. case 2: switch ($baz) { case 1: $foo = 3;
  587. // no break
  588. case 2: $foo = 4; } }
  589. // no break
  590. case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5;
  591. // no break
  592. case 2: $foo = 6; }
  593. // no break
  594. case 2: switch ($baz) { case 1: $foo = 7;
  595. // no break
  596. case 2: $foo = 8; } } }',
  597. '<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1; case 2: $foo = 2; } case 2: switch ($baz) { case 1: $foo = 3; case 2: $foo = 4; } } case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5; case 2: $foo = 6; } case 2: switch ($baz) { case 1: $foo = 7; case 2: $foo = 8; } } }',
  598. ],
  599. [
  600. '<?php
  601. switch ($foo):
  602. case 1:
  603. foo();
  604. // no break
  605. case 2:
  606. bar();
  607. endswitch;',
  608. '<?php
  609. switch ($foo):
  610. case 1:
  611. foo();
  612. case 2:
  613. bar();
  614. // no break
  615. endswitch;',
  616. ],
  617. [
  618. '<?php
  619. switch ($foo) {
  620. case 1:
  621. ?>foo<?php
  622. // no break
  623. case 2:
  624. ?>bar<?php
  625. break;
  626. }',
  627. '<?php
  628. switch ($foo) {
  629. case 1:
  630. ?>foo<?php
  631. case 2:
  632. ?>bar<?php
  633. // no break
  634. break;
  635. }',
  636. ],
  637. [
  638. '<?php
  639. switch ($foo) {
  640. case 1:
  641. ?>foo<?php
  642. // no break
  643. case 2:
  644. ?>bar<?php
  645. break;
  646. }',
  647. '<?php
  648. switch ($foo) {
  649. case 1:
  650. ?>foo<?php
  651. case 2:
  652. ?>bar<?php
  653. // no break
  654. break;
  655. }',
  656. ],
  657. [
  658. '<?php
  659. switch ($foo) {
  660. case 1:
  661. ?>foo<?php // foo
  662. // no break
  663. case 2:
  664. ?>bar<?php // bar
  665. break;
  666. }',
  667. '<?php
  668. switch ($foo) {
  669. case 1:
  670. ?>foo<?php // foo
  671. case 2:
  672. ?>bar<?php // bar
  673. // no break
  674. break;
  675. }',
  676. ],
  677. [
  678. '<?php
  679. switch ($foo) {
  680. case 1:
  681. foo();
  682. // no break
  683. case 2:
  684. bar();
  685. // no break
  686. default:
  687. baz();
  688. }',
  689. '<?php
  690. switch ($foo) {
  691. case 1:
  692. // no break
  693. foo();
  694. case 2:
  695. // no break
  696. bar();
  697. default:
  698. baz();
  699. }',
  700. ],
  701. [
  702. '<?php
  703. switch ($foo) {
  704. case 1:
  705. die;
  706. case 2:
  707. exit;
  708. default:
  709. die;
  710. }',
  711. '<?php
  712. switch ($foo) {
  713. case 1:
  714. // no break
  715. die;
  716. case 2:
  717. // no break
  718. exit;
  719. default:
  720. die;
  721. }',
  722. ],
  723. [
  724. '<?php
  725. switch ($foo) {
  726. case 1: {
  727. throw new \Exception();
  728. }
  729. case 2:
  730. ?>
  731. <?php
  732. throw new \Exception();
  733. default:
  734. throw new \Exception();
  735. }',
  736. '<?php
  737. switch ($foo) {
  738. case 1: {
  739. // no break
  740. throw new \Exception();
  741. }
  742. case 2:
  743. ?>
  744. <?php
  745. // no break
  746. throw new \Exception();
  747. default:
  748. throw new \Exception();
  749. }',
  750. ],
  751. [
  752. '<?php
  753. switch ($foo) {
  754. case 1:
  755. goto a;
  756. case 2:
  757. goto a;
  758. default:
  759. goto a;
  760. }
  761. a:
  762. echo \'foo\';',
  763. '<?php
  764. switch ($foo) {
  765. case 1:
  766. // no break
  767. goto a;
  768. case 2:
  769. // no break
  770. goto a;
  771. default:
  772. goto a;
  773. }
  774. a:
  775. echo \'foo\';',
  776. ],
  777. [
  778. '<?php
  779. switch ($foo) {
  780. case "bar":
  781. if (1) {
  782. } else {
  783. }
  784. $aaa = new Bar();
  785. break;
  786. default:
  787. $aaa = new Baz();
  788. }',
  789. ],
  790. [
  791. '<?php
  792. switch ($foo) {
  793. case 1:
  794. ?>
  795. <?php
  796. // no break
  797. default:
  798. ?>
  799. <?php
  800. }',
  801. '<?php
  802. switch ($foo) {
  803. case 1:
  804. ?>
  805. <?php
  806. default:
  807. ?>
  808. <?php
  809. }',
  810. ],
  811. [
  812. '<?php
  813. switch ($foo) {
  814. case 1:
  815. ?>
  816. <?php
  817. // no break
  818. default:
  819. ?>
  820. <?php }',
  821. '<?php
  822. switch ($foo) {
  823. case 1:
  824. ?>
  825. <?php default:
  826. ?>
  827. <?php }',
  828. ],
  829. [
  830. '<?php
  831. switch ($foo) {
  832. case 1:
  833. foo();
  834. // no break
  835. case 2:
  836. bar();
  837. }',
  838. '<?php
  839. switch ($foo) {
  840. case 1:
  841. foo();
  842. // No break
  843. case 2:
  844. bar();
  845. }',
  846. ],
  847. [
  848. '<?php
  849. switch ($a) {
  850. case 1:
  851. throw new \Exception("");
  852. case 2;
  853. throw new \Exception("");
  854. case 3:
  855. throw new \Exception("");
  856. case 4;
  857. throw new \Exception("");
  858. case 5:
  859. throw new \Exception("");
  860. case 6;
  861. throw new \Exception("");
  862. }
  863. ',
  864. ],
  865. [
  866. '<?php
  867. switch ($f) {
  868. case 1:
  869. if ($a) {
  870. return "";
  871. }
  872. throw new $f();
  873. case Z:
  874. break;
  875. }',
  876. ],
  877. [
  878. '<?php
  879. switch ($foo) {
  880. case 1;
  881. $foo = new class {
  882. public function foo($bar)
  883. {
  884. foreach ($bar as $baz) {
  885. break;
  886. }
  887. }
  888. };
  889. // no break
  890. case 2;
  891. bar();
  892. }',
  893. '<?php
  894. switch ($foo) {
  895. case 1;
  896. $foo = new class {
  897. public function foo($bar)
  898. {
  899. foreach ($bar as $baz) {
  900. break;
  901. }
  902. }
  903. };
  904. case 2;
  905. bar();
  906. }',
  907. ],
  908. [
  909. '<?php
  910. switch ($foo) {
  911. case 1;
  912. $foo = new class(1) {
  913. public function foo($bar)
  914. {
  915. foreach ($bar as $baz) {
  916. break;
  917. }
  918. }
  919. };
  920. // no break
  921. case 2;
  922. bar();
  923. }',
  924. '<?php
  925. switch ($foo) {
  926. case 1;
  927. $foo = new class(1) {
  928. public function foo($bar)
  929. {
  930. foreach ($bar as $baz) {
  931. break;
  932. }
  933. }
  934. };
  935. case 2;
  936. bar();
  937. }',
  938. ],
  939. [
  940. '<?php
  941. switch($a) {
  942. case 1:
  943. $a = function () { throw new \Exception(""); };
  944. // no break
  945. case 2:
  946. $a = new class(){
  947. public function foo () { throw new \Exception(""); }
  948. };
  949. // no break
  950. case 3:
  951. echo 5;
  952. // no break
  953. default:
  954. echo 1;
  955. }
  956. ',
  957. '<?php
  958. switch($a) {
  959. case 1:
  960. $a = function () { throw new \Exception(""); };
  961. case 2:
  962. $a = new class(){
  963. public function foo () { throw new \Exception(""); }
  964. };
  965. case 3:
  966. echo 5;
  967. default:
  968. echo 1;
  969. }
  970. ',
  971. ],
  972. [
  973. '<?php
  974. switch ($foo) {
  975. case 10:
  976. echo 1;
  977. /* no break because of some more details stated here */
  978. case 22:
  979. break;
  980. }',
  981. ],
  982. [
  983. '<?php
  984. switch ($foo) {
  985. case 10:
  986. echo 1;
  987. # no break because of some more details stated here */
  988. case 22:
  989. break;
  990. }',
  991. ],
  992. [
  993. '<?php
  994. switch ($foo) {
  995. case 100:
  996. echo 10;
  997. /* no breaking windows please */
  998. // no break
  999. case 220:
  1000. break;
  1001. }',
  1002. '<?php
  1003. switch ($foo) {
  1004. case 100:
  1005. echo 10;
  1006. /* no breaking windows please */
  1007. case 220:
  1008. break;
  1009. }',
  1010. ],
  1011. ];
  1012. }
  1013. /**
  1014. * @dataProvider provideTestFixWithDifferentCommentTextCases
  1015. */
  1016. public function testFixWithDifferentCommentText(string $expected, ?string $input = null): void
  1017. {
  1018. $this->fixer->configure([
  1019. 'comment_text' => 'fall-through case!',
  1020. ]);
  1021. $this->doTest($expected, $input);
  1022. }
  1023. public function provideTestFixWithDifferentCommentTextCases(): array
  1024. {
  1025. $cases = $this->provideFixCases();
  1026. $replaceCommentText = static function (string $php): string {
  1027. return strtr($php, [
  1028. 'No break' => 'Fall-through case!',
  1029. 'no break' => 'fall-through case!',
  1030. ]);
  1031. };
  1032. foreach ($cases as &$case) {
  1033. $case[0] = $replaceCommentText($case[0]);
  1034. if (isset($case[1])) {
  1035. $case[1] = $replaceCommentText($case[1]);
  1036. }
  1037. }
  1038. return array_merge($cases, [
  1039. [
  1040. '<?php
  1041. switch ($foo) {
  1042. case 1:
  1043. foo();
  1044. // no break
  1045. // fall-through case!
  1046. case 2:
  1047. bar();
  1048. // no break
  1049. // fall-through case!
  1050. default:
  1051. baz();
  1052. }',
  1053. '<?php
  1054. switch ($foo) {
  1055. case 1:
  1056. foo();
  1057. // no break
  1058. case 2:
  1059. bar();
  1060. // no break
  1061. default:
  1062. baz();
  1063. }',
  1064. ],
  1065. ]);
  1066. }
  1067. /**
  1068. * @dataProvider provideTestFixWithDifferentLineEndingCases
  1069. */
  1070. public function testFixWithDifferentLineEnding(string $expected, ?string $input = null): void
  1071. {
  1072. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n"));
  1073. $this->doTest($expected, $input);
  1074. }
  1075. public function provideTestFixWithDifferentLineEndingCases(): \Generator
  1076. {
  1077. foreach ($this->provideFixCases() as $case) {
  1078. $case[0] = str_replace("\n", "\r\n", $case[0]);
  1079. if (isset($case[1])) {
  1080. $case[1] = str_replace("\n", "\r\n", $case[1]);
  1081. }
  1082. yield $case;
  1083. }
  1084. }
  1085. public function testFixWithCommentTextWithSpecialRegexpCharacters(): void
  1086. {
  1087. $this->fixer->configure([
  1088. 'comment_text' => '~***(//[No break here.]\\\\)***~',
  1089. ]);
  1090. $this->doTest(
  1091. '<?php
  1092. switch ($foo) {
  1093. case 1:
  1094. foo();
  1095. // ~***(//[No break here.]\\\\)***~
  1096. case 2:
  1097. bar();
  1098. // ~***(//[No break here.]\\\\)***~
  1099. default:
  1100. baz();
  1101. }',
  1102. '<?php
  1103. switch ($foo) {
  1104. case 1:
  1105. foo();
  1106. // ~***(//[No break here.]\\\\)***~
  1107. case 2:
  1108. bar();
  1109. default:
  1110. baz();
  1111. }'
  1112. );
  1113. }
  1114. public function testFixWithCommentTextWithTrailingSpaces(): void
  1115. {
  1116. $this->fixer->configure([
  1117. 'comment_text' => 'no break ',
  1118. ]);
  1119. $this->doTest(
  1120. '<?php
  1121. switch ($foo) {
  1122. case 1:
  1123. foo();
  1124. // no break
  1125. default:
  1126. baz();
  1127. }',
  1128. '<?php
  1129. switch ($foo) {
  1130. case 1:
  1131. foo();
  1132. default:
  1133. baz();
  1134. }'
  1135. );
  1136. }
  1137. /**
  1138. * @dataProvider provideFixWithCommentTextContainingNewLinesCases
  1139. */
  1140. public function testFixWithCommentTextContainingNewLines(string $text): void
  1141. {
  1142. $this->expectException(InvalidFixerConfigurationException::class);
  1143. $this->expectExceptionMessageMatches('/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/');
  1144. $this->fixer->configure([
  1145. 'comment_text' => $text,
  1146. ]);
  1147. }
  1148. public function provideFixWithCommentTextContainingNewLinesCases(): array
  1149. {
  1150. return [
  1151. ["No\nbreak"],
  1152. ["No\r\nbreak"],
  1153. ["No\rbreak"],
  1154. ];
  1155. }
  1156. public function testConfigureWithInvalidOptions(): void
  1157. {
  1158. $this->expectException(InvalidFixerConfigurationException::class);
  1159. $this->expectExceptionMessageMatches('/^\[no_break_comment\] Invalid configuration: The option "foo" does not exist\. Defined options are: "comment_text"\.$/');
  1160. $this->fixer->configure(['foo' => true]);
  1161. }
  1162. /**
  1163. * @dataProvider provideFix80Cases
  1164. * @requires PHP 8.0
  1165. */
  1166. public function testFix80(string $expected, ?string $input = null): void
  1167. {
  1168. $this->doTest($expected, $input);
  1169. }
  1170. public function provideFix80Cases(): \Generator
  1171. {
  1172. yield [
  1173. '<?php
  1174. switch ($foo) {
  1175. case 1:
  1176. foo() ?? throw new \Exception();
  1177. // no break
  1178. case 2:
  1179. $a = $condition and throw new Exception();
  1180. // no break
  1181. case 3:
  1182. $callable = fn() => throw new Exception();
  1183. // no break
  1184. case 4:
  1185. $value = $falsableValue ?: throw new InvalidArgumentException();
  1186. // no break
  1187. default:
  1188. echo "PHP8";
  1189. }
  1190. ',
  1191. '<?php
  1192. switch ($foo) {
  1193. case 1:
  1194. foo() ?? throw new \Exception();
  1195. case 2:
  1196. $a = $condition and throw new Exception();
  1197. case 3:
  1198. $callable = fn() => throw new Exception();
  1199. case 4:
  1200. $value = $falsableValue ?: throw new InvalidArgumentException();
  1201. default:
  1202. echo "PHP8";
  1203. }
  1204. ',
  1205. ];
  1206. yield [
  1207. '<?php
  1208. match ($foo) {
  1209. 1 => "a",
  1210. default => "b"
  1211. };
  1212. match ($bar) {
  1213. 2 => "c",
  1214. default => "d"
  1215. };
  1216. match ($baz) {
  1217. 3 => "e",
  1218. default => "f"
  1219. };
  1220. ',
  1221. ];
  1222. }
  1223. /**
  1224. * @dataProvider provideFix81Cases
  1225. * @requires PHP 8.1
  1226. */
  1227. public function testFix81(string $expected, ?string $input = null): void
  1228. {
  1229. $this->doTest($expected, $input);
  1230. }
  1231. public function provideFix81Cases(): \Generator
  1232. {
  1233. yield 'enums' => [
  1234. '<?php
  1235. enum Suit {
  1236. case Hearts;
  1237. case Diamonds;
  1238. case Clubs;
  1239. case Spades;
  1240. }
  1241. enum UserStatus: string {
  1242. case Pending = \'P\';
  1243. case Active = \'A\';
  1244. case Suspended = \'S\';
  1245. case CanceledByUser = \'C\';
  1246. }
  1247. switch($a) { // pass the `is candidate` check
  1248. case 1:
  1249. echo 1;
  1250. break;
  1251. }
  1252. ',
  1253. ];
  1254. }
  1255. }