123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?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;
- /**
- * @author Filippo Tessarotto <zoeslam@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ControlStructure\SimplifiedIfReturnFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SimplifiedIfReturnFixer>
- */
- final class SimplifiedIfReturnFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield 'simple' => [
- '<?php return (bool) ($foo) ;',
- '<?php if ($foo) { return true; } return false;',
- ];
- yield 'simple-negative' => [
- '<?php return ! ($foo) ;',
- '<?php if ($foo) { return false; } return true;',
- ];
- yield 'simple-negative II' => [
- '<?php return ! (!$foo && $a()) ;',
- '<?php if (!$foo && $a()) { return false; } return true;',
- ];
- yield 'simple-braceless' => [
- '<?php return (bool) ($foo) ;',
- '<?php if ($foo) return true; return false;',
- ];
- yield 'simple-braceless-negative' => [
- '<?php return ! ($foo) ;',
- '<?php if ($foo) return false; return true;',
- ];
- yield 'bug-consecutive-ifs' => [
- '<?php if ($bar) { return 1; } return (bool) ($foo) ;',
- '<?php if ($bar) { return 1; } if ($foo) { return true; } return false;',
- ];
- yield 'bug-consecutive-ifs-negative' => [
- '<?php if ($bar) { return 1; } return ! ($foo) ;',
- '<?php if ($bar) { return 1; } if ($foo) { return false; } return true;',
- ];
- yield 'bug-consecutive-ifs-braceless' => [
- '<?php if ($bar) return 1; return (bool) ($foo) ;',
- '<?php if ($bar) return 1; if ($foo) return true; return false;',
- ];
- yield 'bug-consecutive-ifs-braceless-negative' => [
- '<?php if ($bar) return 1; return ! ($foo) ;',
- '<?php if ($bar) return 1; if ($foo) return false; return true;',
- ];
- yield [
- <<<'EOT'
- <?php
- function f1() { return (bool) ($f1) ; }
- function f2() { return true; } return false;
- function f3() { return (bool) ($f3) ; }
- function f4() { return true; } return false;
- function f5() { return (bool) ($f5) ; }
- function f6() { return false; } return true;
- function f7() { return ! ($f7) ; }
- function f8() { return false; } return true;
- function f9() { return ! ($f9) ; }
- EOT,
- <<<'EOT'
- <?php
- function f1() { if ($f1) { return true; } return false; }
- function f2() { return true; } return false;
- function f3() { if ($f3) { return true; } return false; }
- function f4() { return true; } return false;
- function f5() { if ($f5) { return true; } return false; }
- function f6() { return false; } return true;
- function f7() { if ($f7) { return false; } return true; }
- function f8() { return false; } return true;
- function f9() { if ($f9) { return false; } return true; }
- EOT,
- ];
- yield 'preserve-comments' => [
- <<<'EOT'
- <?php
- // C1
- return (bool)
- # C2
- (
- /* C3 */
- $foo
- /** C4 */
- )
- // C5
- # C6
- // C7
- # C8
- /* C9 */
- /** C10 */
- // C11
- # C12
- ;
- /* C13 */
- EOT,
- <<<'EOT'
- <?php
- // C1
- if
- # C2
- (
- /* C3 */
- $foo
- /** C4 */
- )
- // C5
- {
- # C6
- return
- // C7
- true
- # C8
- ;
- /* C9 */
- }
- /** C10 */
- return
- // C11
- false
- # C12
- ;
- /* C13 */
- EOT,
- ];
- yield 'preserve-comments-braceless' => [
- <<<'EOT'
- <?php
- // C1
- return (bool)
- # C2
- (
- /* C3 */
- $foo
- /** C4 */
- )
- // C5
- # C6
- // C7
- # C8
- /* C9 */
- /** C10 */
- // C11
- # C12
- ;
- /* C13 */
- EOT,
- <<<'EOT'
- <?php
- // C1
- if
- # C2
- (
- /* C3 */
- $foo
- /** C4 */
- )
- // C5
- # C6
- return
- // C7
- true
- # C8
- ;
- /* C9 */
- /** C10 */
- return
- // C11
- false
- # C12
- ;
- /* C13 */
- EOT,
- ];
- yield 'else-if' => [
- '<?php if ($bar) { return $bar; } else return (bool) ($foo) ;',
- '<?php if ($bar) { return $bar; } else if ($foo) { return true; } return false;',
- ];
- yield 'else-if-negative' => [
- '<?php if ($bar) { return $bar; } else return ! ($foo) ;',
- '<?php if ($bar) { return $bar; } else if ($foo) { return false; } return true;',
- ];
- yield 'else-if-braceless' => [
- '<?php if ($bar) return $bar; else return (bool) ($foo) ;',
- '<?php if ($bar) return $bar; else if ($foo) return true; return false;',
- ];
- yield 'else-if-braceless-negative' => [
- '<?php if ($bar) return $bar; else return ! ($foo) ;',
- '<?php if ($bar) return $bar; else if ($foo) return false; return true;',
- ];
- yield 'elseif' => [
- '<?php if ($bar) { return $bar; } return (bool) ($foo) ;',
- '<?php if ($bar) { return $bar; } elseif ($foo) { return true; } return false;',
- ];
- yield 'elseif-negative' => [
- '<?php if ($bar) { return $bar; } return ! ($foo) ;',
- '<?php if ($bar) { return $bar; } elseif ($foo) { return false; } return true;',
- ];
- yield 'elseif-braceless' => [
- '<?php if ($bar) return $bar; return (bool) ($foo) ;',
- '<?php if ($bar) return $bar; elseif ($foo) return true; return false;',
- ];
- yield 'elseif-braceless-negative' => [
- '<?php if ($bar) return $bar; return ! ($foo) ;',
- '<?php if ($bar) return $bar; elseif ($foo) return false; return true;',
- ];
- yield 'no braces loops' => [
- '<?php
- function foo1(string $str, array $letters): bool
- {
- foreach ($letters as $letter)
- if ($str === $letter)
- return true;
- return false;
- }
- function foo2(int $z): bool
- {
- for ($i = 0; $i < 3; ++$i)
- if ($i === $z)
- return true;
- return false;
- }
- function foo3($y): bool
- {
- while ($x = bar())
- if ($x === $z)
- return true;
- return false;
- }
- ',
- ];
- yield 'alternative syntax not supported' => [
- '<?php
- if ($foo):
- return true;
- else:
- return false;
- endif;
- ',
- ];
- }
- }
|