BlankLineBeforeStatementFixerTest.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. }