Skip to content

Commit 9e9d657

Browse files
committed
[perf] Skip reflection in RemoveUnusedPrivateMethodRector when class has no private method
1 parent cd819fc commit 9e9d657

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public function getNodeTypes(): array
8181
*/
8282
public function refactor(Node $node): ?Node
8383
{
84-
if ($node->getMethods() === []) {
84+
// unreliable to detect on anonymous class: doesn't make sense
85+
if ($node->isAnonymous()) {
8586
return null;
8687
}
8788

88-
// unreliable to detect on anonymous class: doesn't make sense
89-
if ($node->isAnonymous()) {
89+
if (! $this->hasPrivateMethod($node)) {
9090
return null;
9191
}
9292

@@ -145,6 +145,17 @@ public function refactor(Node $node): ?Node
145145
return null;
146146
}
147147

148+
private function hasPrivateMethod(Class_ $class): bool
149+
{
150+
foreach ($class->getMethods() as $classMethod) {
151+
if ($classMethod->isPrivate()) {
152+
return true;
153+
}
154+
}
155+
156+
return false;
157+
}
158+
148159
private function shouldSkip(ClassMethod $classMethod, ClassReflection $classReflection): bool
149160
{
150161
// unreliable to detect trait, interface: doesn't make sense

0 commit comments

Comments
 (0)