From d21bce2f06931dd8cb84a437ba186458e92ad108 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 29 Jul 2026 18:32:59 +0200 Subject: [PATCH] [CodingStyle] Use native php-parser node API over class reflection isAnonymous() and isFinalByKeyword() on the node own class reflection answer questions the Class_ node already answers via isAnonymous() and isFinal(). Read them off the node instead, and hoist the anonymous-class checks above the reflection lookup so it is skipped entirely. --- ...akeInheritedMethodVisibilitySameAsParentRector.php | 6 +++--- .../ClassMethod/RemoveUnusedPrivateMethodRector.php | 11 ++++++----- .../ThisCallOnStaticMethodToStaticCallRector.php | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php b/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php index 0d65d43f471..d187ed6d46a 100644 --- a/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php @@ -78,12 +78,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $classReflection = $this->reflectionResolver->resolveClassReflection($node); - if (! $classReflection instanceof ClassReflection) { + if ($node->isAnonymous()) { return null; } - if ($classReflection->isAnonymous()) { + $classReflection = $this->reflectionResolver->resolveClassReflection($node); + if (! $classReflection instanceof ClassReflection) { return null; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index 784b9730507..4474ac9e592 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -85,6 +85,11 @@ public function refactor(Node $node): ?Node return null; } + // unreliable to detect on anonymous class: doesn't make sense + if ($node->isAnonymous()) { + return null; + } + $hasChanged = false; $classReflection = $this->reflectionResolver->resolveClassReflection($node); @@ -143,7 +148,7 @@ public function refactor(Node $node): ?Node private function shouldSkip(ClassMethod $classMethod, ClassReflection $classReflection): bool { - // unreliable to detect trait, interface, anonymous class: doesn't make sense + // unreliable to detect trait, interface: doesn't make sense if ($classReflection->isTrait()) { return true; } @@ -152,10 +157,6 @@ private function shouldSkip(ClassMethod $classMethod, ClassReflection $classRefl return true; } - if ($classReflection->isAnonymous()) { - return true; - } - // skip magic methods - @see https://www.php.net/manual/en/language.oop5.magic.php if ($classMethod->isMagic()) { return true; diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index 5f99652cd7f..dcf02d789b8 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -158,7 +158,7 @@ private function processThisToStatic(Class_ $class, ClassReflection $classReflec $this->hasChanged = true; - $objectReference = $this->resolveClassSelf($classReflection, $subNode); + $objectReference = $this->resolveClassSelf($class, $subNode); return $this->nodeFactory->createStaticCall($objectReference, $methodName, $subNode->args); }); } @@ -166,9 +166,9 @@ private function processThisToStatic(Class_ $class, ClassReflection $classReflec /** * @return ObjectReference::STATIC|ObjectReference::SELF */ - private function resolveClassSelf(ClassReflection $classReflection, MethodCall $methodCall): string + private function resolveClassSelf(Class_ $class, MethodCall $methodCall): string { - if ($classReflection->isFinalByKeyword()) { + if ($class->isFinal()) { return ObjectReference::SELF; }