Skip to content

Commit 5acf474

Browse files
authored
Fix disabling dead methods detection (#257)
1 parent 40ab442 commit 5acf474

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

rules.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ services:
128128
tags:
129129
- phpstan.collector
130130
arguments:
131-
detectDeadMethods: %shipmonkDeadCode.detect.deadMethods%
132131
detectDeadConstants: %shipmonkDeadCode.detect.deadConstants%
133132
detectDeadEnumCases: %shipmonkDeadCode.detect.deadEnumCases%
134133

@@ -146,6 +145,7 @@ services:
146145
- phpstan.rules.rule
147146
- phpstan.diagnoseExtension
148147
arguments:
148+
detectDeadMethods: %shipmonkDeadCode.detect.deadMethods%
149149
reportTransitivelyDeadMethodAsSeparateError: %shipmonkDeadCode.reportTransitivelyDeadMethodAsSeparateError%
150150

151151
-

src/Collector/ClassDefinitionCollector.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,17 @@ class ClassDefinitionCollector implements Collector
3939

4040
private ReflectionProvider $reflectionProvider;
4141

42-
private bool $detectDeadMethods;
43-
4442
private bool $detectDeadConstants;
4543

4644
private bool $detectDeadEnumCases;
4745

4846
public function __construct(
4947
ReflectionProvider $reflectionProvider,
50-
bool $detectDeadMethods,
5148
bool $detectDeadConstants,
5249
bool $detectDeadEnumCases
5350
)
5451
{
5552
$this->reflectionProvider = $reflectionProvider;
56-
$this->detectDeadMethods = $detectDeadMethods;
5753
$this->detectDeadConstants = $detectDeadConstants;
5854
$this->detectDeadEnumCases = $detectDeadEnumCases;
5955
}
@@ -93,15 +89,13 @@ public function processNode(
9389
$constants = [];
9490
$cases = [];
9591

96-
if ($this->detectDeadMethods) {
97-
foreach ($node->getMethods() as $method) {
98-
$methods[$method->name->toString()] = [
99-
'line' => $method->name->getStartLine(),
100-
'params' => count($method->params),
101-
'abstract' => $method->isAbstract() || $node instanceof Interface_,
102-
'visibility' => $method->flags & (Visibility::PUBLIC | Visibility::PROTECTED | Visibility::PRIVATE),
103-
];
104-
}
92+
foreach ($node->getMethods() as $method) {
93+
$methods[$method->name->toString()] = [
94+
'line' => $method->name->getStartLine(),
95+
'params' => count($method->params),
96+
'abstract' => $method->isAbstract() || $node instanceof Interface_,
97+
'visibility' => $method->flags & (Visibility::PUBLIC | Visibility::PROTECTED | Visibility::PRIVATE),
98+
];
10599
}
106100

107101
if ($this->detectDeadConstants) {

src/Rule/DeadCodeRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class DeadCodeRule implements Rule, DiagnoseExtension
7575

7676
private ClassHierarchy $classHierarchy;
7777

78+
private bool $detectDeadMethods;
79+
7880
/**
7981
* typename => data
8082
*
@@ -130,12 +132,14 @@ class DeadCodeRule implements Rule, DiagnoseExtension
130132
public function __construct(
131133
DebugUsagePrinter $debugUsagePrinter,
132134
ClassHierarchy $classHierarchy,
135+
bool $detectDeadMethods,
133136
bool $reportTransitivelyDeadMethodAsSeparateError,
134137
BackwardCompatibilityChecker $checker
135138
)
136139
{
137140
$this->debugUsagePrinter = $debugUsagePrinter;
138141
$this->classHierarchy = $classHierarchy;
142+
$this->detectDeadMethods = $detectDeadMethods;
139143
$this->reportTransitivelyDeadAsSeparateError = $reportTransitivelyDeadMethodAsSeparateError;
140144

141145
$checker->check();
@@ -311,6 +315,10 @@ public function processNode(
311315

312316
unset($this->blackMembers[$blackMemberKey]);
313317
}
318+
319+
if (!$this->detectDeadMethods && $blackMember->getMember() instanceof ClassMethodRef) {
320+
unset($this->blackMembers[$blackMemberKey]);
321+
}
314322
}
315323

316324
foreach ($excludedMemberUsages as $excludedMemberUsage) {

tests/Rule/DeadCodeRuleTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ protected function getRule(): DeadCodeRule
117117
self::createReflectionProvider(),
118118
),
119119
new ClassHierarchy(),
120+
$this->detectDeadMethods,
120121
!$this->emitErrorsInGroups,
121122
new BackwardCompatibilityChecker([], null),
122123
);
@@ -134,7 +135,7 @@ protected function getCollectors(): array
134135

135136
return [
136137
new ProvidedUsagesCollector($reflectionProvider, $this->getMemberUsageProviders(), $this->getMemberUsageExcluders()),
137-
new ClassDefinitionCollector($reflectionProvider, $this->detectDeadMethods, $this->detectDeadConstants, $this->detectDeadEnumCases),
138+
new ClassDefinitionCollector($reflectionProvider, $this->detectDeadConstants, $this->detectDeadEnumCases),
138139
new MethodCallCollector($this->getMemberUsageExcluders()),
139140
new ConstantFetchCollector($reflectionProvider, $this->getMemberUsageExcluders()),
140141
];
@@ -490,7 +491,7 @@ public function testMethodDetectionCanBeDisabled(): void
490491
$this->detectDeadMethods = false;
491492
$this->analyse([__DIR__ . '/data/other/member-types.php'], [
492493
['Unused MemberTypes\Clazz::CONSTANT', 7],
493-
['Unused MemberTypes\MyEnum::EnumCase', 13],
494+
['Unused MemberTypes\MyEnum::EnumCase', 25],
494495
]);
495496
}
496497

@@ -502,8 +503,8 @@ public function testConstantDetectionCanBeDisabled(): void
502503

503504
$this->detectDeadConstants = false;
504505
$this->analyse([__DIR__ . '/data/other/member-types.php'], [
505-
['Unused MemberTypes\MyEnum::EnumCase', 13],
506-
['Unused MemberTypes\Clazz::method', 8],
506+
['Unused MemberTypes\MyEnum::EnumCase', 25],
507+
['Unused MemberTypes\Clazz::method', 10],
507508
]);
508509
}
509510

@@ -516,7 +517,7 @@ public function testEnumCaseDetectionCanBeDisabled(): void
516517
$this->detectDeadEnumCases = false;
517518
$this->analyse([__DIR__ . '/data/other/member-types.php'], [
518519
['Unused MemberTypes\Clazz::CONSTANT', 7],
519-
['Unused MemberTypes\Clazz::method', 8],
520+
['Unused MemberTypes\Clazz::method', 10],
520521
]);
521522
}
522523

tests/Rule/data/other/member-types.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55
class Clazz
66
{
77
public const CONSTANT = 1; // error: Unused MemberTypes\Clazz::CONSTANT
8+
public const CONSTANT_USED = 1;
9+
810
public function method(): void {} // error: Unused MemberTypes\Clazz::method
11+
12+
public function methodUsed(): void {
13+
$this->transitivity();
14+
}
15+
16+
private function transitivity(): void
17+
{
18+
echo MyEnum::EnumCaseUsed->name;
19+
echo self::CONSTANT_USED;
20+
}
921
}
1022

1123
enum MyEnum
1224
{
1325
case EnumCase; // error: Unused MemberTypes\MyEnum::EnumCase
26+
case EnumCaseUsed;
1427
}
28+
29+
(new Clazz())->methodUsed();

0 commit comments

Comments
 (0)