Skip to content

Commit c40ecff

Browse files
authored
[Performance] Hoist loop-invariant resolve+continue guards above foreach (#8761)
1 parent f954e0c commit c40ecff

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

‎rules/Transform/Rector/StaticCall/StaticCallToNewRector.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ public function refactor(Node $node): ?Node
7272
return null;
7373
}
7474

75+
$class = $this->getName($node->class);
76+
if ($class === null) {
77+
return null;
78+
}
79+
7580
foreach ($this->staticCallsToNewsByMethod[strtolower($methodName)] ?? [] as $staticCallToNew) {
7681
if (! $this->isName($node->class, $staticCallToNew->getClass())) {
7782
continue;
7883
}
7984

80-
$class = $this->getName($node->class);
81-
if ($class === null) {
82-
continue;
83-
}
84-
8585
return new New_(new FullyQualified($class), $node->args);
8686
}
8787

‎rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public function refactor(Node $node): ?Node
9595

9696
$dimFetchVariableNames = $this->resolveDimFetchVariableNames($closureExpr);
9797

98+
// the items type does not change per closure param, resolve it once
99+
$type = $this->getType($closureItems);
100+
if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) {
101+
continue;
102+
}
103+
98104
foreach ($closureExpr->getParams() as $closureParam) {
99105
if ($closureParam->type instanceof Node) {
100106
// param is known already
@@ -110,11 +116,6 @@ public function refactor(Node $node): ?Node
110116
continue;
111117
}
112118

113-
$type = $this->getType($closureItems);
114-
if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) {
115-
continue;
116-
}
117-
118119
$hasChanged = true;
119120
$closureParam->type = new Identifier('array');
120121
}

0 commit comments

Comments
 (0)