12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ControlStructure;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\Tokenizer\Tokens;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\AbstractNoUselessElseFixer
- * @covers \PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer>
- */
- final class NoUselessElseFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideCloseTagCases
- */
- public function testCloseTag(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideCloseTagCases(): iterable
- {
- yield [
- '<?php
- if (true) {
- $b = $a > 2 ? "" : die
- ?>
- <?php
- } else {
- echo 798;
- }',
- ];
- yield [
- '<?php
- if (true) {
- $b = $a > 2 ? "" : die
- ?>
- <?php ; // useless semicolon case
- } else {
- echo 798;
- }',
- ];
- yield [
- '<?php
- if (true) {
- if($a) die
- ?>
- <?php ; // useless semicolon case
- } else {
- echo 798;
- }',
- ];
- yield [
- '<?php
- if (true) {
- echo 1;
- ?>
- <?php ; // useless semicolon case
- } else {
- echo 798;
- }',
- ];
- yield [
- '<?php
- if (true) {
- echo 777;
- if(false) die ?>
- <?php
- } else {
- echo 778;
- }',
- ];
- yield [
- '<?php
- if (true)
- echo 3;
- else {
- ?><?php
- echo 4;
- }
- ',
- ];
- yield [
- '<?php
- if (true)
- echo 3;
- '.'
- ?><?php
- echo 4;
- ',
- '<?php
- if (true)
- echo 3;
- else
- ?><?php
- echo 4;
- ',
- ];
- yield [
- '<?php
- if (true)
- echo 4;
- ?><?php echo 5;',
- '<?php
- if (true)
- echo 4;
- else?><?php echo 5;',
- ];
- }
- /**
- * @dataProvider provideFixIfElseIfElseCases
- */
- public function testFixIfElseIfElse(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixIfElseIfElseCases(): iterable
- {
- $expected =
- '<?php
- while(true) {
- while(true) {
- if ($provideFixIfElseIfElseCases) {
- return;
- } elseif($a1) {
- if ($b) {echo 1; die;} echo 552;
- return 1;
- } elseif($b) {
- %s
- } '.'
- echo 662;
- '.'
- }
- }
- ';
- $input =
- '<?php
- while(true) {
- while(true) {
- if ($provideFixIfElseIfElseCases) {
- return;
- } elseif($a1) {
- if ($b) {echo 1; die;} else {echo 552;}
- return 1;
- } elseif($b) {
- %s
- } else {
- echo 662;
- }
- }
- }
- ';
- yield from self::generateCases($expected, $input);
- $expected =
- '<?php
- while(true) {
- while(true) {
- if($a) {
- echo 100;
- } elseif($b) {
- %s
- } else {
- echo 3;
- }
- }
- }
- ';
- yield from self::generateCases($expected);
- $expected =
- '<?php
- while(true) {
- while(true) {
- if ($a) {
- echo 100;
- } elseif ($a1) {
- echo 99887;
- } elseif ($b) {
- echo $b+1; //
- /* test */
- %s
- } else {
- echo 321;
- }
- }
- }
- ';
- yield from self::generateCases($expected);
- yield [
- '<?php
- if ($a)
- echo 1789;
- else if($b)
- echo 256;
- elseif($c)
- echo 3;
- if ($a) {
- }elseif($d) {
- return 1;
- }
- else
- echo 4;
- ',
- ];
- yield [
- '<?php
- if ($a)
- echo 1789;
- else if($b) {
- echo 256;
- } elseif($c) {
- echo 3;
- if ($d) {
- echo 4;
- } elseif($e)
- return 1;
- } else
- echo 4;
- ',
- ];
- }
- /**
- * @dataProvider provideFixIfElseCases
- */
- public function testFixIfElse(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixIfElseCases(): iterable
- {
- $expected = '<?php
- while(true) {
- while(true) {
- if ($a) {
- %s
- } '.'
- echo 1;
- '.'
- }
- }
- ';
- $input = '<?php
- while(true) {
- while(true) {
- if ($a) {
- %s
- } else {
- echo 1;
- }
- }
- }
- ';
- yield from self::generateCases($expected, $input);
- yield [
- '<?php
- if ($a) {
- GOTO jump;
- } '.'
- echo 1789;
- '.'
- jump:
- ',
- '<?php
- if ($a) {
- GOTO jump;
- } else {
- echo 1789;
- }
- jump:
- ',
- ];
- }
- /**
- * @dataProvider provideFixNestedIfCases
- */
- public function testFixNestedIf(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixNestedIfCases(): iterable
- {
- yield [
- '<?php
- if ($x) {
- if ($y) {
- return 1;
- } '.'
- return 2;
- '.'
- } '.'
- return 3;
- '.'
- ',
- '<?php
- if ($x) {
- if ($y) {
- return 1;
- } else {
- return 2;
- }
- } else {
- return 3;
- }
- ',
- ];
- }
- /**
- * @dataProvider provideFixEmptyElseCases
- */
- public function testFixEmptyElse(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixEmptyElseCases(): iterable
- {
- yield [
- '<?php
- if (false)
- echo 1;
- '.'
- ',
- '<?php
- if (false)
- echo 1;
- else{}
- ',
- ];
- yield [
- '<?php if($a){}',
- '<?php if($a){}else{}',
- ];
- yield [
- '<?php if($a){ $a = ($b); } ',
- '<?php if($a){ $a = ($b); } else {}',
- ];
- yield [
- '<?php if ($a) {;} if ($a) {;} /**/ if($a){}',
- '<?php if ($a) {;} else {} if ($a) {;} else {/**/} if($a){}else{}',
- ];
- yield [
- '<?php
- if /**/($a) /**/{ //
- /**/
- /**/return/**/1/**/;
- //
- }/**/ /**/
- /**/
- //
- /**/
- ',
- '<?php
- if /**/($a) /**/{ //
- /**/
- /**/return/**/1/**/;
- //
- }/**/ else /**/{
- /**/
- //
- }/**/
- ',
- ];
- yield [
- '<?php
- if ($a) {
- if ($b) {
- if ($c) {
- } elseif ($d) {
- return;
- } //
- //
- return;
- } //
- //
- return;
- } //
- //
- ',
- '<?php
- if ($a) {
- if ($b) {
- if ($c) {
- } elseif ($d) {
- return;
- } else {//
- }//
- return;
- } else {//
- }//
- return;
- } else {//
- }//
- ',
- ];
- }
- /**
- * @dataProvider provideNegativeCases
- */
- public function testNegative(string $expected): void
- {
- $this->doTest($expected);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideNegativeCases(): iterable
- {
- yield [
- '<?php
- if ($a0) {
- //
- } else {
- echo 0;
- }
- ',
- ];
- yield [
- '<?php
- if (false)
- echo "a";
- else
- echo "a";
- ',
- ];
- yield [
- '<?php if($a2){;} else {echo 27;}',
- ];
- yield [
- '<?php if ($a3) {test();} else {echo 3;}',
- ];
- yield [
- '<?php if ($a4) {$b = function () {};} else {echo 4;}',
- ];
- yield [
- '<?php if ($a5) {$b = function () use ($a){};} else {echo 5;}',
- ];
- yield [
- '<?php
- if ($a) {
- if ($b) return;
- } else {
- echo 1;
- }
- ',
- ];
- yield [
- '<?php
- if ($a) {
- if ($b) throw new \Exception();
- } else {
- echo 1;
- }
- ',
- ];
- yield [
- '<?php
- if ($a) {
- if ($b) { throw new \Exception(); }
- } else {
- echo 1;
- }
- ',
- ];
- yield [
- '<?php
- $a = true; // 6
- if (true === $a)
- $b = true === $a ? 1 : die;
- else
- echo 40;
- echo "end";
- ',
- ];
- yield [
- '<?php
- $a = true; // 6
- if (true === $a)
- $b = true === $a ? 1 : exit(1);
- else
- echo 40;
- echo "end";
- ',
- ];
- yield [
- '<?php
- $a = true; // 6
- if (true === $a)
- $b = true === $a ? 1 : exit(1);
- else
- echo 4;
- echo "end";
- ',
- ];
- yield [
- '<?php
- if (false)
- die;
- elseif (true)
- if(true)echo 777;else die;
- else if (true)
- die;
- elseif (false)
- die;
- else
- echo 7;
- ',
- ];
- yield [
- '<?php
- $tmp = function($b){$b();};
- $a =1;
- return $tmp(function () use ($a) {
- if ($a) {
- $a++;
- } else {
- $a--;
- }
- });
- ',
- ];
- yield [
- '<?php
- $tmp = function($b){$b();};
- $a =1;
- return $tmp(function () use ($a) {
- if ($a) {
- $a++;
- } elseif($a > 2) {
- return 1;
- } else {
- $a--;
- }
- });
- ',
- ];
- yield [
- '<?php
- return function() {
- if (false) {
- } elseif (3 > 2) {
- } else {
- echo 1;
- }
- };',
- ];
- yield [
- '<?php
- return function() {
- if (false) {
- return 1;
- } elseif (3 > 2) {
- } else {
- echo 1;
- }
- };',
- ];
- }
- /**
- * @dataProvider provideNegativePhp80Cases
- *
- * @requires PHP 8.0
- */
- public function testNegativePhp80(string $expected): void
- {
- $this->doTest($expected);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideNegativePhp80Cases(): iterable
- {
- $cases = [
- '$bar = $foo1 ?? throw new \Exception($e);',
- '$callable = fn() => throw new Exception();',
- '$value = $falsableValue ?: throw new InvalidArgumentException();',
- '$value = !empty($array)
- ? reset($array)
- : throw new InvalidArgumentException();',
- '$a = $condition && throw new Exception();',
- '$a = $condition || throw new Exception();',
- '$a = $condition and throw new Exception();',
- '$a = $condition or throw new Exception();',
- ];
- $template = '<?php
- if ($foo) {
- %s
- } else {
- echo 123;
- }
- ';
- foreach ($cases as $index => $case) {
- yield \sprintf('PHP8 Negative case %d', $index) => [\sprintf($template, $case)];
- }
- }
- /**
- * @param list<int> $expected
- *
- * @dataProvider provideBlockDetectionCases
- */
- public function testBlockDetection(array $expected, string $source, int $index): void
- {
- Tokens::clearCache();
- $tokens = Tokens::fromCode($source);
- $method = new \ReflectionMethod(get_parent_class($this->fixer), 'getPreviousBlock');
- $method->setAccessible(true);
- $result = $method->invoke($this->fixer, $tokens, $index);
- self::assertSame($expected, $result);
- }
- public static function provideBlockDetectionCases(): iterable
- {
- $source = '<?php
- if ($a)
- echo 1;
- elseif ($a) ///
- echo 2;
- else if ($b) /**/ echo 3;
- else
- echo 4;
- ';
- yield [[2, 11], $source, 13];
- yield [[13, 24], $source, 26];
- yield [[26, 39], $source, 41];
- $source = '<?php
- if ($a) {
- if ($b) {
- }
- echo 1;
- } elseif (true) {
- echo 2;
- } else if (false) {
- echo 3;
- } elseif ($c) {
- echo 4;
- } else
- echo 1;
- ';
- yield [[2, 25], $source, 27];
- yield [[27, 40], $source, 42];
- yield [[59, 72], $source, 74];
- }
- /**
- * @dataProvider provideConditionsWithoutBracesCases
- */
- public function testConditionsWithoutBraces(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideConditionsWithoutBracesCases(): iterable
- {
- $statements = [
- 'die;',
- 'throw new Exception($i);',
- 'while($i < 1) throw/*{}*/new Exception($i);',
- 'while($i < 1){throw new Exception($i);}',
- 'do{throw new Exception($i);}while($i < 1);',
- 'foreach($a as $b)throw new Exception($i);',
- 'foreach($a as $b){throw new Exception($i);}',
- ];
- foreach ($statements as $statement) {
- yield from self::generateConditionsWithoutBracesCase($statement);
- }
- yield [
- '<?php
- if ($a === false)
- {
- if ($v) { $ret = "foo"; if($d){return 1;}echo $a;}
- }
- else
- $ret .= $value;
- return $ret;',
- '<?php
- if ($a === false)
- {
- if ($v) { $ret = "foo"; if($d){return 1;}else{echo $a;}}
- }
- else
- $ret .= $value;
- return $ret;',
- ];
- yield from self::generateConditionsWithoutBracesCase('throw new class extends Exception{};');
- yield from self::generateConditionsWithoutBracesCase('throw new class ($a, 9) extends Exception{ public function z($a, $b){ echo 7;} };');
- }
- /**
- * @dataProvider provideConditionsWithoutBraces80Cases
- *
- * @requires PHP 8.0
- */
- public function testConditionsWithoutBraces80(string $expected): void
- {
- $this->doTest($expected);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideConditionsWithoutBraces80Cases(): iterable
- {
- yield from self::generateConditionsWithoutBracesCase('$b = $a ?? throw new Exception($i);');
- }
- /**
- * @param array<int, bool> $indexes
- *
- * @dataProvider provideIsInConditionWithoutBracesCases
- */
- public function testIsInConditionWithoutBraces(array $indexes, string $input): void
- {
- $reflection = new \ReflectionObject($this->fixer);
- $method = $reflection->getMethod('isInConditionWithoutBraces');
- $method->setAccessible(true);
- $tokens = Tokens::fromCode($input);
- foreach ($indexes as $index => $expected) {
- self::assertSame(
- $expected,
- $method->invoke($this->fixer, $tokens, $index, 0),
- \sprintf('Failed in condition without braces check for index %d', $index)
- );
- }
- }
- public static function provideIsInConditionWithoutBracesCases(): iterable
- {
- yield [
- [
- 18 => false, // return
- 25 => false, // return
- 36 => false, // return
- ],
- '<?php
- if ($x) {
- if ($y) {
- return 1;
- }
- return 2;
- } else {
- return 3;
- }
- ',
- ];
- yield [
- [
- 0 => false,
- 29 => false, // throw
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- else
- if($a){}else{throw new Exception($i);}
- ',
- ];
- yield [
- [
- 0 => false,
- 38 => true, // throw
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- else
- for($i =0;$i < 1;++$i) throw new Exception($i);
- ',
- ];
- yield [
- [
- 0 => false,
- 26 => true, // throw
- 28 => true, // new
- 30 => true, // Exception
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- else
- while(false){throw new Exception($i);}
- ',
- ];
- yield [
- [
- 0 => false,
- 30 => true, // throw
- 32 => true, // new
- 34 => true, // Exception
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- else
- foreach($a as $b){throw new Exception($i);}
- ',
- ];
- yield [
- [
- 0 => false,
- 25 => true, // throw
- 27 => true, // new
- 29 => true, // Exception
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- else
- while(false)throw new Exception($i);
- ',
- ];
- yield [
- [
- 26 => true, // throw
- ],
- '<?php
- if ($v) { $ret = "foo"; }
- elseif($a)
- do{throw new Exception($i);}while(false);
- ',
- ];
- yield [
- [
- 4 => false, // 1
- 13 => true, // if (2nd)
- 21 => true, // true
- 33 => true, // while
- 43 => false, // echo
- 45 => false, // 2
- 46 => false, // ;
- 51 => false, // echo (123)
- ],
- '<?php
- echo 1;
- if ($a) if ($a) while(true)echo 1;
- elseif($c) while(true){if($d){echo 2;}};
- echo 123;
- ',
- ];
- yield [
- [
- 2 => false, // echo
- 13 => true, // echo
- 15 => true, // 2
- 20 => true, // die
- 23 => false, // echo
- ],
- '<?php
- echo 1;
- if ($a) echo 2;
- else die; echo 3;
- ',
- ];
- yield [
- [
- 8 => true, // die
- 9 => true, // /**/
- 15 => true, // die
- ],
- '<?php
- if ($a)
- die/**/;
- else
- /**/die/**/;#
- ',
- ];
- yield [
- [
- 8 => true, // die
- 9 => true, // /**/
- 15 => true, // die
- ],
- '<?php
- if ($a)
- die/**/;
- else
- /**/die/**/?>
- ',
- ];
- }
- /**
- * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
- */
- private static function generateConditionsWithoutBracesCase(string $statement): iterable
- {
- $ifTemplate = '<?php
- if ($a === false)
- {
- if ($v) %s
- }
- else
- $ret .= $value;
- return $ret;';
- $ifElseIfTemplate = '<?php
- if ($a === false)
- {
- if ($v) { $ret = "foo"; }
- elseif($a)
- %s
- }
- else
- $ret .= $value;
- return $ret;';
- $ifElseTemplate = '<?php
- if ($a === false)
- {
- if ($v) { $ret = "foo"; }
- else
- %s
- }
- else
- $ret .= $value;
- return $ret;';
- yield [\sprintf($ifTemplate, $statement)];
- yield [\sprintf($ifElseTemplate, $statement)];
- yield [\sprintf($ifElseIfTemplate, $statement)];
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- private static function generateCases(string $expected, ?string $input = null): iterable
- {
- $cases = [];
- foreach ([
- 'exit;',
- 'exit();',
- 'exit(1);',
- 'die;',
- 'die();',
- 'die(1);',
- 'break;',
- 'break 2;',
- 'break (2);',
- 'continue;',
- 'continue 2;',
- 'continue (2);',
- 'return;',
- 'return 1;',
- 'return (1);',
- 'return "a";',
- 'return 8+2;',
- 'return null;',
- 'return sum(1+8*6, 2);',
- 'throw $e;',
- 'throw ($e);',
- 'throw new \Exception;',
- 'throw new \Exception();',
- 'throw new \Exception((string)12+1);',
- ] as $case) {
- if (null === $input) {
- $cases[] = [\sprintf($expected, $case)];
- $cases[] = [\sprintf($expected, strtoupper($case))];
- if ($case !== strtolower($case)) {
- $cases[] = [\sprintf($expected, strtolower($case))];
- }
- } else {
- $cases[] = [\sprintf($expected, $case), \sprintf($input, $case)];
- $cases[] = [\sprintf($expected, strtoupper($case)), \sprintf($input, strtoupper($case))];
- if ($case !== strtolower($case)) {
- $cases[] = [\sprintf($expected, strtolower($case)), \sprintf($input, strtolower($case))];
- }
- }
- }
- return $cases;
- }
- }
|