Skip to content

Commit cab8dab

Browse files
committed
Follow calls from anonymous class
1 parent 40b7c71 commit cab8dab

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/Rule/DeadMethodRule.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public function processNode(
130130
foreach ($calls as $callString) {
131131
$call = Call::fromString($callString);
132132

133-
if ($this->containsAnonymousClass($call)) {
134-
continue;
135-
}
136-
137-
$callerKey = $call->caller === null ? '' : $call->caller->toString();
138-
$isWhite = $call->caller === null || Method::isUnsupported($call->caller->methodName);
133+
$callerKey = $call->caller === null || $this->isAnonymousClass($call->caller->className)
134+
? ''
135+
: $call->caller->toString();
136+
$isWhite = $call->caller === null
137+
|| $this->isAnonymousClass($call->caller->className)
138+
|| Method::isUnsupported($call->caller->methodName);
139139

140140
foreach ($this->getAlternativeCalleeKeys($call) as $possibleCalleeKey) {
141141
$this->callGraph[$callerKey][] = $possibleCalleeKey;
@@ -243,14 +243,10 @@ private function fillClassHierarchy(string $typeName, array $ancestorNames): voi
243243
}
244244
}
245245

246-
private function containsAnonymousClass(Call $call): bool
246+
private function isAnonymousClass(string $className): bool
247247
{
248-
$callerClassName = $call->caller === null ? '' : $call->caller->className;
249-
$calleeClassName = $call->callee->className;
250-
251248
// https://github.com/phpstan/phpstan/issues/8410 workaround, ideally this should not be ignored
252-
return strpos($callerClassName, 'AnonymousClass') === 0
253-
|| strpos($calleeClassName, 'AnonymousClass') === 0;
249+
return strpos($className, 'AnonymousClass') === 0;
254250
}
255251

256252
/**

tests/Rule/DeadMethodRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public function testGrouping(): void
127127
*/
128128
public static function provideFiles(): iterable
129129
{
130+
yield 'anonym' => [__DIR__ . '/data/DeadMethodRule/anonym.php'];
130131
yield 'enum' => [__DIR__ . '/data/DeadMethodRule/enum.php', 8_01_00];
131132
yield 'code' => [__DIR__ . '/data/DeadMethodRule/basic.php'];
132133
yield 'ctor' => [__DIR__ . '/data/DeadMethodRule/ctor.php'];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Anonym;
4+
5+
class Other
6+
{
7+
public static function bar(): void
8+
{
9+
}
10+
}
11+
12+
new class {
13+
public function __construct()
14+
{
15+
Other::bar();
16+
}
17+
};

0 commit comments

Comments
 (0)