NoBreakCommentFixerTest.php 24 KB

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