From 2adeacc410de9304137574d2e80cf97c9f67dfa5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 11 Mar 2024 09:10:24 +0700 Subject: [PATCH] [Performance]EarlyReturn] Reduce repetitive findFirst() on IfAndAnalyzer::isIfStmtExprUsedInNextReturn() (#5709) * [Performance]EarlyReturn] Reduce repetitive findFirst() on IfAndAnalyzer::isIfStmtExprUsedInNextReturn() * check empty stmts early * clean up --- .../NodeAnalyzer/IfAndAnalyzer.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php b/rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php index c9830fdf4f6..a0c4ea3917e 100644 --- a/rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php +++ b/rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php @@ -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; + ); } }