BlankLineBeforeStatementFixerTest.php 27 KB

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