[EarlyReturn] Skip complex if cond and with ChangeAndIfToEarlyReturnRector and RemoveAlwaysElseRector (#654)

* [EarlyReturn] Skip complex if cond and with ChangeAndIfToEarlyReturnRector and RemoveAlwaysElseRector

* Fixed 🎉
This commit is contained in:
Abdul Malik Ikhsan 2021-08-12 20:01:47 +07:00 committed by GitHub
parent 906bb39c8d
commit 8e48e49823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 2 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Stmt\Continue_;
@ -84,6 +85,12 @@ CODE_SAMPLE
return null;
}
// to avoid repetitive flipped elseif above return when used along with ChangeAndIfReturnToEarlyReturnRector
// @see https://github.com/rectorphp/rector-src/pull/654
if ($node->cond instanceof BooleanAnd && count($node->elseifs) > 1) {
return null;
}
if ($node->elseifs !== []) {
$originalNode = clone $node;
$if = new If_($node->cond);

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndInsideCase\Fixture;
class SkipComplexIfCondAnd
{
public function run($a, $b, $c, $d, $e, $f, $g)
{
if ($b && $c) {
return true;
} elseif ($d && $e) {
return false;
} elseif ($f && $g) {
return 1;
}
return 0;
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class IssueReturnBeforeElseIfAndTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAnd');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_and.php';
}
}

View File

@ -8,7 +8,7 @@ use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class IssueReturnBeforeElseIfTest extends AbstractRectorTestCase
final class IssueReturnBeforeElseIfOrTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
@ -28,6 +28,6 @@ final class IssueReturnBeforeElseIfTest extends AbstractRectorTestCase
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
return __DIR__ . '/config/configured_rule_or.php';
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangeAndIfToEarlyReturnRector::class);
$services->set(RemoveAlwaysElseRector::class);
};