BlankLineBeforeStatementFixerTest.php 23 KB

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