diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index c41ad3087e4..fe03b9af361 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -81,12 +81,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->getMethods() === []) { + // unreliable to detect on anonymous class: doesn't make sense + if ($node->isAnonymous()) { return null; } - // unreliable to detect on anonymous class: doesn't make sense - if ($node->isAnonymous()) { + if (! $this->hasPrivateMethod($node)) { return null; } @@ -145,6 +145,11 @@ public function refactor(Node $node): ?Node return null; } + private function hasPrivateMethod(Class_ $class): bool + { + return array_any($class->getMethods(), fn(ClassMethod $classMethod): bool => $classMethod->isPrivate()); + } + private function shouldSkip(ClassMethod $classMethod, ClassReflection $classReflection): bool { // unreliable to detect trait, interface: doesn't make sense