diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php index 3f3960ae553..202d9bd9418 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php @@ -94,7 +94,17 @@ public function refactor(Node $node): ?Node return null; } + $classMethodNames = []; + foreach ($node->getMethods() as $classMethod) { + $classMethodNames[strtolower((string) $this->getName($classMethod))] = true; + } + foreach ($this->methodReturnTypes as $methodReturnType) { + // cheap name check before the expensive type resolution below + if (! isset($classMethodNames[strtolower($methodReturnType->getMethod())])) { + continue; + } + $objectType = $methodReturnType->getObjectType(); if (! $this->isObjectType($node, $objectType)) { continue;