Skip to content

Commit 2e47eb5

Browse files
stofondrejmirtes
authored andcommitted
Use case insensitive matching to detect usages of private methods
1 parent d1860cd commit 2e47eb5

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/Rules/DeadCode/UnusedPrivateMethodRule.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
5656
if (strtolower($methodName) === '__clone') {
5757
continue;
5858
}
59-
$methods[$methodName] = $method;
59+
$methods[strtolower($methodName)] = $method;
6060
}
6161

6262
$arrayCalls = [];
@@ -105,7 +105,7 @@ public function processNode(Node $node, Scope $scope): array
105105
if ($inMethod->getName() === $methodName) {
106106
continue;
107107
}
108-
unset($methods[$methodName]);
108+
unset($methods[strtolower($methodName)]);
109109
}
110110
}
111111

@@ -144,26 +144,27 @@ public function processNode(Node $node, Scope $scope): array
144144
if ($inMethod->getName() === $typeAndMethod->getMethod()) {
145145
continue;
146146
}
147-
unset($methods[$typeAndMethod->getMethod()]);
147+
unset($methods[strtolower($typeAndMethod->getMethod())]);
148148
}
149149
}
150150
}
151151
}
152152

153153
$errors = [];
154-
foreach ($methods as $methodName => $method) {
154+
foreach ($methods as $method) {
155+
$originalMethodName = $method->getNode()->name->toString();
155156
$methodType = 'Method';
156157
if ($method->getNode()->isStatic()) {
157158
$methodType = 'Static method';
158159
}
159-
$errors[] = RuleErrorBuilder::message(sprintf('%s %s::%s() is unused.', $methodType, $classReflection->getDisplayName(), $methodName))
160+
$errors[] = RuleErrorBuilder::message(sprintf('%s %s::%s() is unused.', $methodType, $classReflection->getDisplayName(), $originalMethodName))
160161
->line($method->getNode()->getLine())
161162
->identifier('deadCode.unusedMethod')
162163
->metadata([
163164
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
164165
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
165166
'classStartLine' => $node->getClass()->getStartLine(),
166-
'methodName' => $methodName,
167+
'methodName' => $originalMethodName,
167168
])
168169
->build();
169170
}

tests/PHPStan/Rules/DeadCode/UnusedPrivateMethodRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public function testBug7389(): void
9090
]);
9191
}
9292

93+
public function testBug8346(): void
94+
{
95+
$this->analyse([__DIR__ . '/data/bug-8346.php'], []);
96+
}
97+
9398
public function testFalsePositiveWithTraitUse(): void
9499
{
95100
if (PHP_VERSION_ID < 80100) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug8346;
4+
5+
class HelloWorld
6+
{
7+
public function speak(): string
8+
{
9+
return $this->sayhello('world');
10+
}
11+
12+
private function sayHello(string $name): string
13+
{
14+
return 'Hello ' . $name;
15+
}
16+
}

0 commit comments

Comments
 (0)