Skip to content

Commit 81b5d9f

Browse files
authored
[perf] Speed up legacy Php rules with cheap guards before reflection and type lookup (#8672)
1 parent 323fb23 commit 81b5d9f

5 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ private function processThisToStatic(Class_ $class, ClassReflection $classReflec
147147
return null;
148148
}
149149

150-
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $class);
151-
if (! $isStaticMethod) {
150+
if ($subNode->isFirstClassCallable()) {
152151
return null;
153152
}
154153

155-
if ($subNode->isFirstClassCallable()) {
154+
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $class);
155+
if (! $isStaticMethod) {
156156
return null;
157157
}
158158

‎rules/Php71/Rector/BooleanOr/IsIterableRector.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public function refactor(Node $node): ?Node
6363

6464
private function shouldSkip(): bool
6565
{
66-
if ($this->reflectionProvider->hasFunction(new Name('is_iterable'), null)) {
66+
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::IS_ITERABLE)) {
6767
return false;
6868
}
6969

70-
return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::IS_ITERABLE);
70+
return ! $this->reflectionProvider->hasFunction(new Name('is_iterable'), null);
7171
}
7272
}

‎rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function refactor(Node $node): ?Node
6363
return null;
6464
}
6565

66+
if ($node->isFirstClassCallable()) {
67+
return null;
68+
}
69+
6670
// unreliable count of arguments
6771
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
6872
if ($functionLikeReflection instanceof UnionTypeMethodReflection) {
@@ -90,9 +94,6 @@ public function refactor(Node $node): ?Node
9094
}
9195

9296
$maximumAllowedParameterCount = $this->resolveMaximumAllowedParameterCount($functionLikeReflection);
93-
if ($node->isFirstClassCallable()) {
94-
return null;
95-
}
9697

9798
if ($this->shouldSkipFunctionReflection($functionLikeReflection)) {
9899
return null;

‎rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ public function refactor(Node $node): ?Node
140140
return null;
141141
}
142142

143-
// constant is defined in current lower/upper case
144-
if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) {
145-
return null;
146-
}
147-
148143
// is uppercase, all good
149144
if ($constantName === $uppercasedConstantName) {
150145
return null;
@@ -158,6 +153,11 @@ public function refactor(Node $node): ?Node
158153
return null;
159154
}
160155

156+
// constant is defined in current lower/upper case
157+
if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) {
158+
return null;
159+
}
160+
161161
$node->name = new FullyQualified($uppercasedConstantName);
162162

163163
return $node;

‎rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public function refactor(Node $node): ?Node
7878
return null;
7979
}
8080

81-
$callerType = $this->getType($node->class);
82-
if (! $callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) {
81+
$firstArg = $node->getArgs()[0] ?? null;
82+
if (! $firstArg instanceof Arg) {
8383
return null;
8484
}
8585

86-
$firstArg = $node->getArgs()[0] ?? null;
87-
if (! $firstArg instanceof Arg) {
86+
$callerType = $this->getType($node->class);
87+
if (! $callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) {
8888
return null;
8989
}
9090

0 commit comments

Comments
 (0)