NoUselessElseFixerTest.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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\AbstractNoUselessElseFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\AbstractNoUselessElseFixer
  20. * @covers \PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer>
  23. */
  24. final class NoUselessElseFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideCloseTagCases
  28. */
  29. public function testCloseTag(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideCloseTagCases(): iterable
  37. {
  38. yield [
  39. '<?php
  40. if (true) {
  41. $b = $a > 2 ? "" : die
  42. ?>
  43. <?php
  44. } else {
  45. echo 798;
  46. }',
  47. ];
  48. yield [
  49. '<?php
  50. if (true) {
  51. $b = $a > 2 ? "" : die
  52. ?>
  53. <?php ; // useless semicolon case
  54. } else {
  55. echo 798;
  56. }',
  57. ];
  58. yield [
  59. '<?php
  60. if (true) {
  61. if($a) die
  62. ?>
  63. <?php ; // useless semicolon case
  64. } else {
  65. echo 798;
  66. }',
  67. ];
  68. yield [
  69. '<?php
  70. if (true) {
  71. echo 1;
  72. ?>
  73. <?php ; // useless semicolon case
  74. } else {
  75. echo 798;
  76. }',
  77. ];
  78. yield [
  79. '<?php
  80. if (true) {
  81. echo 777;
  82. if(false) die ?>
  83. <?php
  84. } else {
  85. echo 778;
  86. }',
  87. ];
  88. yield [
  89. '<?php
  90. if (true)
  91. echo 3;
  92. else {
  93. ?><?php
  94. echo 4;
  95. }
  96. ',
  97. ];
  98. yield [
  99. '<?php
  100. if (true)
  101. echo 3;
  102. '.'
  103. ?><?php
  104. echo 4;
  105. ',
  106. '<?php
  107. if (true)
  108. echo 3;
  109. else
  110. ?><?php
  111. echo 4;
  112. ',
  113. ];
  114. yield [
  115. '<?php
  116. if (true)
  117. echo 4;
  118. ?><?php echo 5;',
  119. '<?php
  120. if (true)
  121. echo 4;
  122. else?><?php echo 5;',
  123. ];
  124. }
  125. /**
  126. * @dataProvider provideFixIfElseIfElseCases
  127. */
  128. public function testFixIfElseIfElse(string $expected, ?string $input = null): void
  129. {
  130. $this->doTest($expected, $input);
  131. }
  132. /**
  133. * @return iterable<array{0: string, 1?: string}>
  134. */
  135. public static function provideFixIfElseIfElseCases(): iterable
  136. {
  137. $expected = '<?php
  138. while(true) {
  139. while(true) {
  140. if ($provideFixIfElseIfElseCases) {
  141. return;
  142. } elseif($a1) {
  143. if ($b) {echo 1; die;} echo 552;
  144. return 1;
  145. } elseif($b) {
  146. %s
  147. } '.'
  148. echo 662;
  149. '.'
  150. }
  151. }
  152. ';
  153. $input = '<?php
  154. while(true) {
  155. while(true) {
  156. if ($provideFixIfElseIfElseCases) {
  157. return;
  158. } elseif($a1) {
  159. if ($b) {echo 1; die;} else {echo 552;}
  160. return 1;
  161. } elseif($b) {
  162. %s
  163. } else {
  164. echo 662;
  165. }
  166. }
  167. }
  168. ';
  169. yield from self::generateCases($expected, $input);
  170. $expected = '<?php
  171. while(true) {
  172. while(true) {
  173. if($a) {
  174. echo 100;
  175. } elseif($b) {
  176. %s
  177. } else {
  178. echo 3;
  179. }
  180. }
  181. }
  182. ';
  183. yield from self::generateCases($expected);
  184. $expected = '<?php
  185. while(true) {
  186. while(true) {
  187. if ($a) {
  188. echo 100;
  189. } elseif ($a1) {
  190. echo 99887;
  191. } elseif ($b) {
  192. echo $b+1; //
  193. /* test */
  194. %s
  195. } else {
  196. echo 321;
  197. }
  198. }
  199. }
  200. ';
  201. yield from self::generateCases($expected);
  202. yield [
  203. '<?php
  204. if ($a)
  205. echo 1789;
  206. else if($b)
  207. echo 256;
  208. elseif($c)
  209. echo 3;
  210. if ($a) {
  211. }elseif($d) {
  212. return 1;
  213. }
  214. else
  215. echo 4;
  216. ',
  217. ];
  218. yield [
  219. '<?php
  220. if ($a)
  221. echo 1789;
  222. else if($b) {
  223. echo 256;
  224. } elseif($c) {
  225. echo 3;
  226. if ($d) {
  227. echo 4;
  228. } elseif($e)
  229. return 1;
  230. } else
  231. echo 4;
  232. ',
  233. ];
  234. }
  235. /**
  236. * @dataProvider provideFixIfElseCases
  237. */
  238. public function testFixIfElse(string $expected, ?string $input = null): void
  239. {
  240. $this->doTest($expected, $input);
  241. }
  242. /**
  243. * @return iterable<array{0: string, 1?: string}>
  244. */
  245. public static function provideFixIfElseCases(): iterable
  246. {
  247. $expected = '<?php
  248. while(true) {
  249. while(true) {
  250. if ($a) {
  251. %s
  252. } '.'
  253. echo 1;
  254. '.'
  255. }
  256. }
  257. ';
  258. $input = '<?php
  259. while(true) {
  260. while(true) {
  261. if ($a) {
  262. %s
  263. } else {
  264. echo 1;
  265. }
  266. }
  267. }
  268. ';
  269. yield from self::generateCases($expected, $input);
  270. yield [
  271. '<?php
  272. if ($a) {
  273. GOTO jump;
  274. } '.'
  275. echo 1789;
  276. '.'
  277. jump:
  278. ',
  279. '<?php
  280. if ($a) {
  281. GOTO jump;
  282. } else {
  283. echo 1789;
  284. }
  285. jump:
  286. ',
  287. ];
  288. }
  289. /**
  290. * @dataProvider provideFixNestedIfCases
  291. */
  292. public function testFixNestedIf(string $expected, ?string $input = null): void
  293. {
  294. $this->doTest($expected, $input);
  295. }
  296. /**
  297. * @return iterable<array{string, string}>
  298. */
  299. public static function provideFixNestedIfCases(): iterable
  300. {
  301. yield [
  302. '<?php
  303. if ($x) {
  304. if ($y) {
  305. return 1;
  306. } '.'
  307. return 2;
  308. '.'
  309. } '.'
  310. return 3;
  311. '.'
  312. ',
  313. '<?php
  314. if ($x) {
  315. if ($y) {
  316. return 1;
  317. } else {
  318. return 2;
  319. }
  320. } else {
  321. return 3;
  322. }
  323. ',
  324. ];
  325. }
  326. /**
  327. * @dataProvider provideFixEmptyElseCases
  328. */
  329. public function testFixEmptyElse(string $expected, ?string $input = null): void
  330. {
  331. $this->doTest($expected, $input);
  332. }
  333. /**
  334. * @return iterable<array{string, string}>
  335. */
  336. public static function provideFixEmptyElseCases(): iterable
  337. {
  338. yield [
  339. '<?php
  340. if (false)
  341. echo 1;
  342. '.'
  343. ',
  344. '<?php
  345. if (false)
  346. echo 1;
  347. else{}
  348. ',
  349. ];
  350. yield [
  351. '<?php if($a){}',
  352. '<?php if($a){}else{}',
  353. ];
  354. yield [
  355. '<?php if($a){ $a = ($b); } ',
  356. '<?php if($a){ $a = ($b); } else {}',
  357. ];
  358. yield [
  359. '<?php if ($a) {;} if ($a) {;} /**/ if($a){}',
  360. '<?php if ($a) {;} else {} if ($a) {;} else {/**/} if($a){}else{}',
  361. ];
  362. yield [
  363. '<?php
  364. if /**/($a) /**/{ //
  365. /**/
  366. /**/return/**/1/**/;
  367. //
  368. }/**/ /**/
  369. /**/
  370. //
  371. /**/
  372. ',
  373. '<?php
  374. if /**/($a) /**/{ //
  375. /**/
  376. /**/return/**/1/**/;
  377. //
  378. }/**/ else /**/{
  379. /**/
  380. //
  381. }/**/
  382. ',
  383. ];
  384. yield [
  385. '<?php
  386. if ($a) {
  387. if ($b) {
  388. if ($c) {
  389. } elseif ($d) {
  390. return;
  391. } //
  392. //
  393. return;
  394. } //
  395. //
  396. return;
  397. } //
  398. //
  399. ',
  400. '<?php
  401. if ($a) {
  402. if ($b) {
  403. if ($c) {
  404. } elseif ($d) {
  405. return;
  406. } else {//
  407. }//
  408. return;
  409. } else {//
  410. }//
  411. return;
  412. } else {//
  413. }//
  414. ',
  415. ];
  416. }
  417. /**
  418. * @dataProvider provideNegativeCases
  419. */
  420. public function testNegative(string $expected): void
  421. {
  422. $this->doTest($expected);
  423. }
  424. /**
  425. * @return iterable<array{string}>
  426. */
  427. public static function provideNegativeCases(): iterable
  428. {
  429. yield [
  430. '<?php
  431. if ($a0) {
  432. //
  433. } else {
  434. echo 0;
  435. }
  436. ',
  437. ];
  438. yield [
  439. '<?php
  440. if (false)
  441. echo "a";
  442. else
  443. echo "a";
  444. ',
  445. ];
  446. yield [
  447. '<?php if($a2){;} else {echo 27;}',
  448. ];
  449. yield [
  450. '<?php if ($a3) {test();} else {echo 3;}',
  451. ];
  452. yield [
  453. '<?php if ($a4) {$b = function () {};} else {echo 4;}',
  454. ];
  455. yield [
  456. '<?php if ($a5) {$b = function () use ($a){};} else {echo 5;}',
  457. ];
  458. yield [
  459. '<?php
  460. if ($a) {
  461. if ($b) return;
  462. } else {
  463. echo 1;
  464. }
  465. ',
  466. ];
  467. yield [
  468. '<?php
  469. if ($a) {
  470. if ($b) throw new \Exception();
  471. } else {
  472. echo 1;
  473. }
  474. ',
  475. ];
  476. yield [
  477. '<?php
  478. if ($a) {
  479. if ($b) { throw new \Exception(); }
  480. } else {
  481. echo 1;
  482. }
  483. ',
  484. ];
  485. yield [
  486. '<?php
  487. $a = true; // 6
  488. if (true === $a)
  489. $b = true === $a ? 1 : die;
  490. else
  491. echo 40;
  492. echo "end";
  493. ',
  494. ];
  495. yield [
  496. '<?php
  497. $a = true; // 6
  498. if (true === $a)
  499. $b = true === $a ? 1 : exit(1);
  500. else
  501. echo 40;
  502. echo "end";
  503. ',
  504. ];
  505. yield [
  506. '<?php
  507. $a = true; // 6
  508. if (true === $a)
  509. $b = true === $a ? 1 : exit(1);
  510. else
  511. echo 4;
  512. echo "end";
  513. ',
  514. ];
  515. yield [
  516. '<?php
  517. if (false)
  518. die;
  519. elseif (true)
  520. if(true)echo 777;else die;
  521. else if (true)
  522. die;
  523. elseif (false)
  524. die;
  525. else
  526. echo 7;
  527. ',
  528. ];
  529. yield [
  530. '<?php
  531. $tmp = function($b){$b();};
  532. $a =1;
  533. return $tmp(function () use ($a) {
  534. if ($a) {
  535. $a++;
  536. } else {
  537. $a--;
  538. }
  539. });
  540. ',
  541. ];
  542. yield [
  543. '<?php
  544. $tmp = function($b){$b();};
  545. $a =1;
  546. return $tmp(function () use ($a) {
  547. if ($a) {
  548. $a++;
  549. } elseif($a > 2) {
  550. return 1;
  551. } else {
  552. $a--;
  553. }
  554. });
  555. ',
  556. ];
  557. yield [
  558. '<?php
  559. return function() {
  560. if (false) {
  561. } elseif (3 > 2) {
  562. } else {
  563. echo 1;
  564. }
  565. };',
  566. ];
  567. yield [
  568. '<?php
  569. return function() {
  570. if (false) {
  571. return 1;
  572. } elseif (3 > 2) {
  573. } else {
  574. echo 1;
  575. }
  576. };',
  577. ];
  578. }
  579. /**
  580. * @dataProvider provideNegativePhp80Cases
  581. *
  582. * @requires PHP 8.0
  583. */
  584. public function testNegativePhp80(string $expected): void
  585. {
  586. $this->doTest($expected);
  587. }
  588. /**
  589. * @return iterable<string, array{string}>
  590. */
  591. public static function provideNegativePhp80Cases(): iterable
  592. {
  593. $cases = [
  594. '$bar = $foo1 ?? throw new \Exception($e);',
  595. '$callable = fn() => throw new Exception();',
  596. '$value = $falsableValue ?: throw new InvalidArgumentException();',
  597. '$value = !empty($array)
  598. ? reset($array)
  599. : throw new InvalidArgumentException();',
  600. '$a = $condition && throw new Exception();',
  601. '$a = $condition || throw new Exception();',
  602. '$a = $condition and throw new Exception();',
  603. '$a = $condition or throw new Exception();',
  604. ];
  605. $template = '<?php
  606. if ($foo) {
  607. %s
  608. } else {
  609. echo 123;
  610. }
  611. ';
  612. foreach ($cases as $index => $case) {
  613. yield \sprintf('PHP8 Negative case %d', $index) => [\sprintf($template, $case)];
  614. }
  615. }
  616. /**
  617. * @param list<int> $expected
  618. *
  619. * @dataProvider provideBlockDetectionCases
  620. */
  621. public function testBlockDetection(array $expected, string $source, int $index): void
  622. {
  623. Tokens::clearCache();
  624. $tokens = Tokens::fromCode($source);
  625. $result = \Closure::bind(static fn (AbstractNoUselessElseFixer $fixer): array => $fixer->getPreviousBlock($tokens, $index), null, AbstractNoUselessElseFixer::class)($this->fixer);
  626. self::assertSame($expected, $result);
  627. }
  628. public static function provideBlockDetectionCases(): iterable
  629. {
  630. $source = '<?php
  631. if ($a)
  632. echo 1;
  633. elseif ($a) ///
  634. echo 2;
  635. else if ($b) /**/ echo 3;
  636. else
  637. echo 4;
  638. ';
  639. yield [[2, 11], $source, 13];
  640. yield [[13, 24], $source, 26];
  641. yield [[26, 39], $source, 41];
  642. $source = '<?php
  643. if ($a) {
  644. if ($b) {
  645. }
  646. echo 1;
  647. } elseif (true) {
  648. echo 2;
  649. } else if (false) {
  650. echo 3;
  651. } elseif ($c) {
  652. echo 4;
  653. } else
  654. echo 1;
  655. ';
  656. yield [[2, 25], $source, 27];
  657. yield [[27, 40], $source, 42];
  658. yield [[59, 72], $source, 74];
  659. }
  660. /**
  661. * @dataProvider provideConditionsWithoutBracesCases
  662. */
  663. public function testConditionsWithoutBraces(string $expected, ?string $input = null): void
  664. {
  665. $this->doTest($expected, $input);
  666. }
  667. /**
  668. * @return iterable<array{0: string, 1?: string}>
  669. */
  670. public static function provideConditionsWithoutBracesCases(): iterable
  671. {
  672. $statements = [
  673. 'die;',
  674. 'throw new Exception($i);',
  675. 'while($i < 1) throw/*{}*/new Exception($i);',
  676. 'while($i < 1){throw new Exception($i);}',
  677. 'do{throw new Exception($i);}while($i < 1);',
  678. 'foreach($a as $b)throw new Exception($i);',
  679. 'foreach($a as $b){throw new Exception($i);}',
  680. ];
  681. foreach ($statements as $statement) {
  682. yield from self::generateConditionsWithoutBracesCase($statement);
  683. }
  684. yield [
  685. '<?php
  686. if ($a === false)
  687. {
  688. if ($v) { $ret = "foo"; if($d){return 1;}echo $a;}
  689. }
  690. else
  691. $ret .= $value;
  692. return $ret;',
  693. '<?php
  694. if ($a === false)
  695. {
  696. if ($v) { $ret = "foo"; if($d){return 1;}else{echo $a;}}
  697. }
  698. else
  699. $ret .= $value;
  700. return $ret;',
  701. ];
  702. yield from self::generateConditionsWithoutBracesCase('throw new class extends Exception{};');
  703. yield from self::generateConditionsWithoutBracesCase('throw new class ($a, 9) extends Exception{ public function z($a, $b){ echo 7;} };');
  704. }
  705. /**
  706. * @dataProvider provideConditionsWithoutBraces80Cases
  707. *
  708. * @requires PHP 8.0
  709. */
  710. public function testConditionsWithoutBraces80(string $expected): void
  711. {
  712. $this->doTest($expected);
  713. }
  714. /**
  715. * @return iterable<array{string}>
  716. */
  717. public static function provideConditionsWithoutBraces80Cases(): iterable
  718. {
  719. yield from self::generateConditionsWithoutBracesCase('$b = $a ?? throw new Exception($i);');
  720. }
  721. /**
  722. * @param array<int, bool> $indexes
  723. *
  724. * @dataProvider provideIsInConditionWithoutBracesCases
  725. */
  726. public function testIsInConditionWithoutBraces(array $indexes, string $input): void
  727. {
  728. $tokens = Tokens::fromCode($input);
  729. foreach ($indexes as $index => $expected) {
  730. self::assertSame(
  731. $expected,
  732. \Closure::bind(static fn (AbstractNoUselessElseFixer $fixer): bool => $fixer->isInConditionWithoutBraces($tokens, $index, 0), null, AbstractNoUselessElseFixer::class)($this->fixer),
  733. \sprintf('Failed in condition without braces check for index %d', $index)
  734. );
  735. }
  736. }
  737. public static function provideIsInConditionWithoutBracesCases(): iterable
  738. {
  739. yield [
  740. [
  741. 18 => false, // return
  742. 25 => false, // return
  743. 36 => false, // return
  744. ],
  745. '<?php
  746. if ($x) {
  747. if ($y) {
  748. return 1;
  749. }
  750. return 2;
  751. } else {
  752. return 3;
  753. }
  754. ',
  755. ];
  756. yield [
  757. [
  758. 0 => false,
  759. 29 => false, // throw
  760. ],
  761. '<?php
  762. if ($v) { $ret = "foo"; }
  763. else
  764. if($a){}else{throw new Exception($i);}
  765. ',
  766. ];
  767. yield [
  768. [
  769. 0 => false,
  770. 38 => true, // throw
  771. ],
  772. '<?php
  773. if ($v) { $ret = "foo"; }
  774. else
  775. for($i =0;$i < 1;++$i) throw new Exception($i);
  776. ',
  777. ];
  778. yield [
  779. [
  780. 0 => false,
  781. 26 => true, // throw
  782. 28 => true, // new
  783. 30 => true, // Exception
  784. ],
  785. '<?php
  786. if ($v) { $ret = "foo"; }
  787. else
  788. while(false){throw new Exception($i);}
  789. ',
  790. ];
  791. yield [
  792. [
  793. 0 => false,
  794. 30 => true, // throw
  795. 32 => true, // new
  796. 34 => true, // Exception
  797. ],
  798. '<?php
  799. if ($v) { $ret = "foo"; }
  800. else
  801. foreach($a as $b){throw new Exception($i);}
  802. ',
  803. ];
  804. yield [
  805. [
  806. 0 => false,
  807. 25 => true, // throw
  808. 27 => true, // new
  809. 29 => true, // Exception
  810. ],
  811. '<?php
  812. if ($v) { $ret = "foo"; }
  813. else
  814. while(false)throw new Exception($i);
  815. ',
  816. ];
  817. yield [
  818. [
  819. 26 => true, // throw
  820. ],
  821. '<?php
  822. if ($v) { $ret = "foo"; }
  823. elseif($a)
  824. do{throw new Exception($i);}while(false);
  825. ',
  826. ];
  827. yield [
  828. [
  829. 4 => false, // 1
  830. 13 => true, // if (2nd)
  831. 21 => true, // true
  832. 33 => true, // while
  833. 43 => false, // echo
  834. 45 => false, // 2
  835. 46 => false, // ;
  836. 51 => false, // echo (123)
  837. ],
  838. '<?php
  839. echo 1;
  840. if ($a) if ($a) while(true)echo 1;
  841. elseif($c) while(true){if($d){echo 2;}};
  842. echo 123;
  843. ',
  844. ];
  845. yield [
  846. [
  847. 2 => false, // echo
  848. 13 => true, // echo
  849. 15 => true, // 2
  850. 20 => true, // die
  851. 23 => false, // echo
  852. ],
  853. '<?php
  854. echo 1;
  855. if ($a) echo 2;
  856. else die; echo 3;
  857. ',
  858. ];
  859. yield [
  860. [
  861. 8 => true, // die
  862. 9 => true, // /**/
  863. 15 => true, // die
  864. ],
  865. '<?php
  866. if ($a)
  867. die/**/;
  868. else
  869. /**/die/**/;#
  870. ',
  871. ];
  872. yield [
  873. [
  874. 8 => true, // die
  875. 9 => true, // /**/
  876. 15 => true, // die
  877. ],
  878. '<?php
  879. if ($a)
  880. die/**/;
  881. else
  882. /**/die/**/?>
  883. ',
  884. ];
  885. }
  886. /**
  887. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  888. */
  889. private static function generateConditionsWithoutBracesCase(string $statement): iterable
  890. {
  891. $ifTemplate = '<?php
  892. if ($a === false)
  893. {
  894. if ($v) %s
  895. }
  896. else
  897. $ret .= $value;
  898. return $ret;';
  899. $ifElseIfTemplate = '<?php
  900. if ($a === false)
  901. {
  902. if ($v) { $ret = "foo"; }
  903. elseif($a)
  904. %s
  905. }
  906. else
  907. $ret .= $value;
  908. return $ret;';
  909. $ifElseTemplate = '<?php
  910. if ($a === false)
  911. {
  912. if ($v) { $ret = "foo"; }
  913. else
  914. %s
  915. }
  916. else
  917. $ret .= $value;
  918. return $ret;';
  919. yield [\sprintf($ifTemplate, $statement)];
  920. yield [\sprintf($ifElseTemplate, $statement)];
  921. yield [\sprintf($ifElseIfTemplate, $statement)];
  922. }
  923. /**
  924. * @return iterable<array{0: string, 1?: string}>
  925. */
  926. private static function generateCases(string $expected, ?string $input = null): iterable
  927. {
  928. $cases = [];
  929. foreach ([
  930. 'exit;',
  931. 'exit();',
  932. 'exit(1);',
  933. 'die;',
  934. 'die();',
  935. 'die(1);',
  936. 'break;',
  937. 'break 2;',
  938. 'break (2);',
  939. 'continue;',
  940. 'continue 2;',
  941. 'continue (2);',
  942. 'return;',
  943. 'return 1;',
  944. 'return (1);',
  945. 'return "a";',
  946. 'return 8+2;',
  947. 'return null;',
  948. 'return sum(1+8*6, 2);',
  949. 'throw $e;',
  950. 'throw ($e);',
  951. 'throw new \Exception;',
  952. 'throw new \Exception();',
  953. 'throw new \Exception((string)12+1);',
  954. ] as $case) {
  955. if (null === $input) {
  956. $cases[] = [\sprintf($expected, $case)];
  957. $cases[] = [\sprintf($expected, strtoupper($case))];
  958. if ($case !== strtolower($case)) {
  959. $cases[] = [\sprintf($expected, strtolower($case))];
  960. }
  961. } else {
  962. $cases[] = [\sprintf($expected, $case), \sprintf($input, $case)];
  963. $cases[] = [\sprintf($expected, strtoupper($case)), \sprintf($input, strtoupper($case))];
  964. if ($case !== strtolower($case)) {
  965. $cases[] = [\sprintf($expected, strtolower($case)), \sprintf($input, strtolower($case))];
  966. }
  967. }
  968. }
  969. return $cases;
  970. }
  971. }