Skip to content

Commit

Permalink
Add patch ensure if only return with no expr
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 13, 2023
1 parent 2f298aa commit 6da8fc1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rules/DeadCode/UselessIfCondBeforeForeachDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
Expand Down Expand Up @@ -41,7 +42,20 @@ public function isMatchingEmptyAndForeachedExpr(If_ $if, Expr $foreachExpr): boo
/** @var Empty_ $empty */
$empty = $if->cond;

return $this->nodeComparator->areNodesEqual($empty->expr, $foreachExpr);
if (! $this->nodeComparator->areNodesEqual($empty->expr, $foreachExpr)) {
return false;
}

if ($if->stmts === []) {
return true;
}

if (count($if->stmts) !== 1) {
return false;
}

$stmt = $if->stmts[0];
return $stmt instanceof Return_ && ! $stmt->expr instanceof Expr;
}

/**
Expand Down

0 comments on commit 6da8fc1

Please sign in to comment.