Merge branch 'breaking_runnable_test_for__ChangeNestedForeachIfsToEarlyContinueRector'

This commit is contained in:
TomasVotruba 2020-06-24 23:39:40 +02:00
commit 9796048c8c
2 changed files with 75 additions and 4 deletions

View File

@ -139,11 +139,15 @@ PHP
// special case
if ($invertedCondition instanceof BooleanNot && $invertedCondition->expr instanceof BooleanAnd) {
$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->left));
$foreach->stmts[] = $booleanNotPartIf;
$leftExpr = $this->negateOrDeNegate($invertedCondition->expr->left);
$if = new If_($leftExpr);
$if->stmts[] = new Continue_();
$foreach->stmts[] = $if;
$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->right));
$foreach->stmts[] = $booleanNotPartIf;
$rightExpr = $this->negateOrDeNegate($invertedCondition->expr->right);
$if = new If_($rightExpr);
$if->stmts[] = new Continue_();
$foreach->stmts[] = $if;
return;
}
@ -155,4 +159,13 @@ PHP
$foreach->stmts[] = $nestedIfWithOnlyReturn;
}
private function negateOrDeNegate(Node\Expr $expr): Node\Expr
{
if ($expr instanceof BooleanNot) {
return $expr->expr;
}
return new BooleanNot($expr);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector\Fixture;
use Rector\Core\Testing\Contract\RunnableInterface;
class MultiExprsWithBoolOprnds implements RunnableInterface
{
public function run()
{
$partPackageList = [ new \StdClass(), 'string' ];
$payload = [];
foreach ($partPackageList as $partPackage) {
if (! is_null($partPackage) && ! is_object($partPackage)) {
if (1 == '1' || 2 == 't') {
$payload[] = $partPackage;
}
}
}
return json_encode($payload);
}
}
?>
-----
<?php
namespace Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector\Fixture;
use Rector\Core\Testing\Contract\RunnableInterface;
class MultiExprsWithBoolOprnds implements RunnableInterface
{
public function run()
{
$partPackageList = [ new \StdClass(), 'string' ];
$payload = [];
foreach ($partPackageList as $partPackage) {
if (is_null($partPackage)) {
continue;
}
if (is_object($partPackage)) {
continue;
}
if (1 == '1' && 2 == 't') {
continue;
}
$payload[] = $partPackage;
}
return json_encode($payload);
}
}
?>