Skip to content

Commit

Permalink
[Performance]EarlyReturn] Reduce repetitive findFirst() on IfAndAnaly…
Browse files Browse the repository at this point in the history
…zer::isIfStmtExprUsedInNextReturn() (#5709)

* [Performance]EarlyReturn] Reduce repetitive findFirst() on IfAndAnalyzer::isIfStmtExprUsedInNextReturn()

* check empty stmts early

* clean up
  • Loading branch information
samsonasik committed Mar 11, 2024
1 parent 5789430 commit 2adeacc
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php
Expand Up @@ -38,16 +38,17 @@ public function isIfStmtExprUsedInNextReturn(If_ $if, Return_ $return): bool
}

$ifExprs = $this->betterNodeFinder->findInstanceOf($if->stmts, Expr::class);
foreach ($ifExprs as $ifExpr) {
$isExprFoundInReturn = (bool) $this->betterNodeFinder->findFirst(
$return->expr,
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $ifExpr)
);
if ($isExprFoundInReturn) {
return true;
return (bool) $this->betterNodeFinder->findFirst(
$return->expr,
function (Node $node) use ($ifExprs): bool {
foreach ($ifExprs as $ifExpr) {
if ($this->nodeComparator->areNodesEqual($node, $ifExpr)) {
return true;
}
}

return false;
}
}

return false;
);
}
}

0 comments on commit 2adeacc

Please sign in to comment.