From 45de81a9615855c3c65f8c5ae90a9821e26ff3d6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 10:23:45 +0200 Subject: [PATCH] [Performance] Hoist loop-invariant resolve+continue guards above foreach In StaticCallToNewRector and AddArrowFunctionParamArrayWhereDimFetchRector a value resolved from a loop-invariant node was fetched inside a foreach, then a continue guard repeated it every iteration. Hoist the resolve above the loop and bail once. --- .../Rector/StaticCall/StaticCallToNewRector.php | 10 +++++----- .../AddArrowFunctionParamArrayWhereDimFetchRector.php | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/rules/Transform/Rector/StaticCall/StaticCallToNewRector.php b/rules/Transform/Rector/StaticCall/StaticCallToNewRector.php index fa53d580fe5..e4ac0190472 100644 --- a/rules/Transform/Rector/StaticCall/StaticCallToNewRector.php +++ b/rules/Transform/Rector/StaticCall/StaticCallToNewRector.php @@ -72,16 +72,16 @@ public function refactor(Node $node): ?Node return null; } + $class = $this->getName($node->class); + if ($class === null) { + return null; + } + foreach ($this->staticCallsToNewsByMethod[strtolower($methodName)] ?? [] as $staticCallToNew) { if (! $this->isName($node->class, $staticCallToNew->getClass())) { continue; } - $class = $this->getName($node->class); - if ($class === null) { - continue; - } - return new New_(new FullyQualified($class), $node->args); } diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 0b463ada4e6..e71b8facb79 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -95,6 +95,12 @@ public function refactor(Node $node): ?Node $dimFetchVariableNames = $this->resolveDimFetchVariableNames($closureExpr); + // the items type does not change per closure param, resolve it once + $type = $this->getType($closureItems); + if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) { + continue; + } + foreach ($closureExpr->getParams() as $closureParam) { if ($closureParam->type instanceof Node) { // param is known already @@ -110,11 +116,6 @@ public function refactor(Node $node): ?Node continue; } - $type = $this->getType($closureItems); - if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) { - continue; - } - $hasChanged = true; $closureParam->type = new Identifier('array'); }