From 45149879da2670820fbe16ef57990b44b5bf461d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 16 Oct 2026 00:48:45 +0200 Subject: [PATCH] [perf] Speed up legacy Php rules with cheap guards before reflection and type lookup --- .../ThisCallOnStaticMethodToStaticCallRector.php | 6 +++--- rules/Php71/Rector/BooleanOr/IsIterableRector.php | 4 ++-- .../Rector/FuncCall/RemoveExtraParametersRector.php | 7 ++++--- .../Rector/ConstFetch/SensitiveConstantNameRector.php | 10 +++++----- .../StaticCall/ExportToReflectionFunctionRector.php | 8 ++++---- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index abe2019ecc4..d7efe8e4e10 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -147,12 +147,12 @@ private function processThisToStatic(Class_ $class, ClassReflection $classReflec return null; } - $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $class); - if (! $isStaticMethod) { + if ($subNode->isFirstClassCallable()) { return null; } - if ($subNode->isFirstClassCallable()) { + $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName, $class); + if (! $isStaticMethod) { return null; } diff --git a/rules/Php71/Rector/BooleanOr/IsIterableRector.php b/rules/Php71/Rector/BooleanOr/IsIterableRector.php index 72796b7f145..1ab116169c6 100644 --- a/rules/Php71/Rector/BooleanOr/IsIterableRector.php +++ b/rules/Php71/Rector/BooleanOr/IsIterableRector.php @@ -63,10 +63,10 @@ public function refactor(Node $node): ?Node private function shouldSkip(): bool { - if ($this->reflectionProvider->hasFunction(new Name('is_iterable'), null)) { + if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::IS_ITERABLE)) { return false; } - return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::IS_ITERABLE); + return ! $this->reflectionProvider->hasFunction(new Name('is_iterable'), null); } } diff --git a/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php b/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php index 682f6f77939..27e682a4082 100644 --- a/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php +++ b/rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php @@ -63,6 +63,10 @@ public function refactor(Node $node): ?Node return null; } + if ($node->isFirstClassCallable()) { + return null; + } + // unreliable count of arguments $functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); if ($functionLikeReflection instanceof UnionTypeMethodReflection) { @@ -90,9 +94,6 @@ public function refactor(Node $node): ?Node } $maximumAllowedParameterCount = $this->resolveMaximumAllowedParameterCount($functionLikeReflection); - if ($node->isFirstClassCallable()) { - return null; - } if ($this->shouldSkipFunctionReflection($functionLikeReflection)) { return null; diff --git a/rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php b/rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php index b3badd7bf17..371bb8e79ca 100644 --- a/rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php +++ b/rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php @@ -140,11 +140,6 @@ public function refactor(Node $node): ?Node return null; } - // constant is defined in current lower/upper case - if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) { - return null; - } - // is uppercase, all good if ($constantName === $uppercasedConstantName) { return null; @@ -158,6 +153,11 @@ public function refactor(Node $node): ?Node return null; } + // constant is defined in current lower/upper case + if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) { + return null; + } + $node->name = new FullyQualified($uppercasedConstantName); return $node; diff --git a/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php b/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php index 3b26e10200d..f61c9391f0c 100644 --- a/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php +++ b/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php @@ -78,13 +78,13 @@ public function refactor(Node $node): ?Node return null; } - $callerType = $this->getType($node->class); - if (! $callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) { + $firstArg = $node->getArgs()[0] ?? null; + if (! $firstArg instanceof Arg) { return null; } - $firstArg = $node->getArgs()[0] ?? null; - if (! $firstArg instanceof Arg) { + $callerType = $this->getType($node->class); + if (! $callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) { return null; }