Skip to content

Commit 3cea0fa

Browse files
committed
perf: skip known-used private methods early
1 parent 792291c commit 3cea0fa

2 files changed

Lines changed: 76 additions & 6 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class MixedProtectedAndUnusedMethods extends TestCase
11+
{
12+
#[DataProvider('provideValues')]
13+
public function testValue(int $value): void
14+
{
15+
self::assertSame($value, self::class::expectedValue());
16+
$this->locallyUsed();
17+
}
18+
19+
private static function provideValues(): array
20+
{
21+
return [[42]];
22+
}
23+
24+
private static function expectedValue(): int
25+
{
26+
return 42;
27+
}
28+
29+
private function unused(): void
30+
{
31+
}
32+
33+
private function locallyUsed(): void
34+
{
35+
}
36+
}
37+
-----
38+
<?php
39+
40+
declare(strict_types=1);
41+
42+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;
43+
44+
use PHPUnit\Framework\Attributes\DataProvider;
45+
use PHPUnit\Framework\TestCase;
46+
47+
final class MixedProtectedAndUnusedMethods extends TestCase
48+
{
49+
#[DataProvider('provideValues')]
50+
public function testValue(int $value): void
51+
{
52+
self::assertSame($value, self::class::expectedValue());
53+
$this->locallyUsed();
54+
}
55+
56+
private static function provideValues(): array
57+
{
58+
return [[42]];
59+
}
60+
61+
private static function expectedValue(): int
62+
{
63+
return 42;
64+
}
65+
66+
private function locallyUsed(): void
67+
{
68+
}
69+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function refactor(Node $node): ?Node
102102
// methods invoked via a class-string static call, e.g. self::class::sampleClass(),
103103
// are not seen by the usage analyzer
104104
$classStringCallMethodNames = $this->resolveClassStringStaticCallNames($node);
105+
$scope = null;
105106

106107
foreach ($node->stmts as $classStmtKey => $classStmt) {
107108
if (! $classStmt instanceof ClassMethod) {
@@ -113,24 +114,24 @@ public function refactor(Node $node): ?Node
113114
}
114115

115116
$classMethod = $classStmt;
116-
if ($this->hasDynamicMethodCallOnFetchThis($classStmt)) {
117+
if ($this->isNames($classMethod, $dataProviderMethodNames)) {
117118
continue;
118119
}
119120

120-
$scope = ScopeFetcher::fetch($node);
121-
if ($this->shouldSkip($classStmt, $classReflection)) {
121+
if ($this->isNames($classMethod, $classStringCallMethodNames)) {
122122
continue;
123123
}
124124

125-
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $classStmt, $scope)) {
125+
if ($this->hasDynamicMethodCallOnFetchThis($classStmt)) {
126126
continue;
127127
}
128128

129-
if ($this->isNames($classMethod, $dataProviderMethodNames)) {
129+
if ($this->shouldSkip($classStmt, $classReflection)) {
130130
continue;
131131
}
132132

133-
if ($this->isNames($classMethod, $classStringCallMethodNames)) {
133+
$scope ??= ScopeFetcher::fetch($node);
134+
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $classStmt, $scope)) {
134135
continue;
135136
}
136137

0 commit comments

Comments
 (0)