Skip to content

Commit 429255c

Browse files
committed
[DeadCode] Keep private method called via self::class static call
A private method invoked through a class-string static call, e.g. self::class::sampleClass(), is not detected by the usage analyzer and would be wrongly removed. Keep such methods.
1 parent 0df45b4 commit 429255c

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;
4+
5+
// a private method called through a class-string static call, self::class::sampleClass(),
6+
// is not detected by the usage analyzer - keep it
7+
final class KeepSelfClassStaticCall
8+
{
9+
public function run(): object
10+
{
11+
return self::class::sampleClass();
12+
}
13+
14+
private function sampleClass(): object
15+
{
16+
return new \stdClass();
17+
}
18+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace Rector\DeadCode\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ClassConstFetch;
89
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\StaticCall;
911
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Identifier;
1013
use PhpParser\Node\Stmt\Class_;
1114
use PhpParser\Node\Stmt\ClassMethod;
1215
use PHPStan\Reflection\ClassReflection;
@@ -91,6 +94,10 @@ public function refactor(Node $node): ?Node
9194

9295
$dataProviderMethodNames = $this->resolveDataProviderMethodNames($node);
9396

97+
// methods invoked via a class-string static call, e.g. self::class::sampleClass(),
98+
// are not seen by the usage analyzer
99+
$classStringCallMethodNames = $this->resolveClassStringStaticCallNames($node);
100+
94101
foreach ($node->stmts as $classStmtKey => $classStmt) {
95102
if (! $classStmt instanceof ClassMethod) {
96103
continue;
@@ -118,6 +125,10 @@ public function refactor(Node $node): ?Node
118125
continue;
119126
}
120127

128+
if ($this->isNames($classMethod, $classStringCallMethodNames)) {
129+
continue;
130+
}
131+
121132
unset($node->stmts[$classStmtKey]);
122133
$hasChanged = true;
123134
}
@@ -153,6 +164,35 @@ private function shouldSkip(ClassMethod $classMethod, ClassReflection $classRefl
153164
return $classReflection->hasMethod(MethodName::CALL);
154165
}
155166

167+
/**
168+
* @return string[]
169+
*/
170+
private function resolveClassStringStaticCallNames(Class_ $class): array
171+
{
172+
$methodNames = [];
173+
174+
/** @var StaticCall[] $staticCalls */
175+
$staticCalls = $this->betterNodeFinder->findInstanceOf($class->stmts, StaticCall::class);
176+
foreach ($staticCalls as $staticCall) {
177+
// e.g. self::class::sampleClass() - the called class is a ::class expression
178+
if (! $staticCall->class instanceof ClassConstFetch) {
179+
continue;
180+
}
181+
182+
if (! $this->isName($staticCall->class->name, 'class')) {
183+
continue;
184+
}
185+
186+
if (! $staticCall->name instanceof Identifier) {
187+
continue;
188+
}
189+
190+
$methodNames[] = $staticCall->name->toString();
191+
}
192+
193+
return $methodNames;
194+
}
195+
156196
private function hasDynamicMethodCallOnFetchThis(ClassMethod $classMethod): bool
157197
{
158198
return (bool) $this->betterNodeFinder->findFirst(

0 commit comments

Comments
 (0)