From 263571b18f425634780e8058a420a44ef2243b5d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 15 Jan 2026 11:15:16 +0100 Subject: [PATCH] [perf] optimize ParamTypeByMethodCallTypeRector for speed with early checks --- .../ShortNameResolverTest.php | 1 - .../ParamTypeByMethodCallTypeRector.php | 36 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php b/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php index d1e105b9fe9..1931d75e3ff 100644 --- a/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php +++ b/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php @@ -18,7 +18,6 @@ final class ShortNameResolverTest extends AbstractLazyTestCase protected function setUp(): void { - // @todo let dynamic source locator know about parsed files parent::setUp(); $this->shortNameResolver = $this->make(ShortNameResolver::class); diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index 3e19e31f613..73db69cfc6c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -106,6 +106,15 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if ($node->params === []) { + return null; + } + + // has params with at least one missing type + if (! $this->hasAtLeastOneParamWithoutType($node)) { + return null; + } + if ($node instanceof ClassMethod && $this->shouldSkipClassMethod($node)) { return null; } @@ -116,7 +125,17 @@ public function refactor(Node $node): ?Node [StaticCall::class, MethodCall::class, FuncCall::class] ); - $hasChanged = $this->refactorFunctionLike($node, $callers); + // keep only callers with args + $callersWithArgs = array_filter( + $callers, + fn (StaticCall|MethodCall|FuncCall $caller): bool => $caller->args !== [] + ); + + if ($callersWithArgs === []) { + return null; + } + + $hasChanged = $this->refactorFunctionLike($node, $callersWithArgs); if ($hasChanged) { return $node; } @@ -126,10 +145,6 @@ public function refactor(Node $node): ?Node private function shouldSkipClassMethod(ClassMethod $classMethod): bool { - if ($classMethod->params === []) { - return true; - } - $isMissingParameterTypes = false; foreach ($classMethod->params as $param) { if ($param->type instanceof Node) { @@ -218,4 +233,15 @@ private function refactorFunctionLike( return $hasChanged; } + + private function hasAtLeastOneParamWithoutType(ClassMethod|Function_|Closure|ArrowFunction $functionLike): bool + { + foreach ($functionLike->params as $param) { + if (! $param->type instanceof Node) { + return true; + } + } + + return false; + } }