1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- <?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
- */
- final class NoUselessElseFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideCloseTagCases
- */
- public function testCloseTag(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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 [[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);
- }
- 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);
- }
- 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 array<array<string>>
- */
- private static function generateCases(string $expected, ?string $input = null): array
- {
- $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))];
- $cases[] = [sprintf($expected, strtolower($case))];
- } else {
- $cases[] = [sprintf($expected, $case), sprintf($input, $case)];
- $cases[] = [sprintf($expected, strtoupper($case)), sprintf($input, strtoupper($case))];
- $cases[] = [sprintf($expected, strtolower($case)), sprintf($input, strtolower($case))];
- }
- }
- return $cases;
- }
- }
|