Skip to content

Commit 5550d1c

Browse files
authored
Trait entrypoint check in context of users (#54)
1 parent 923ac93 commit 5550d1c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/Collector/MethodDefinitionCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private function getDeclaringTraitDefinition(
133133
return null;
134134
}
135135

136-
if ($realDeclaringClass->isTrait()) {
136+
if ($realDeclaringClass->isTrait() && $realDeclaringClass->getName() !== $classReflection->getName()) {
137137
return new MethodDefinition(
138138
$realDeclaringClass->getName(),
139139
$realName,

src/Rule/DeadMethodRule.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,19 @@ private function isEntryPoint(MethodDefinition $methodDefinition): bool
215215
return false;
216216
}
217217

218-
$methodReflection = $this->reflectionProvider // @phpstan-ignore missingType.checkedException (method should exist)
219-
->getClass($methodDefinition->className)
218+
$reflection = $this->reflectionProvider->getClass($methodDefinition->className);
219+
220+
// if trait has users, we need to check entrypoint even from their context
221+
if ($reflection->isTrait()) {
222+
foreach ($this->classHierarchy->getMethodTraitUsages($methodDefinition) as $traitUsage) {
223+
if ($this->isEntryPoint($traitUsage)) {
224+
return true;
225+
}
226+
}
227+
}
228+
229+
// @phpstan-ignore missingType.checkedException (method should exist)
230+
$methodReflection = $reflection
220231
->getNativeReflection()
221232
->getMethod($methodDefinition->methodName);
222233

tests/Rule/data/DeadMethodRule/providers/phpunit.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
use PHPUnit\Framework\Attributes\Test;
88
use PHPUnit\Framework\TestCase;
99

10+
trait TraitTestCase {
11+
#[Before] // need to be checked in context of user, not in context of trait
12+
public function callBefore(): void
13+
{
14+
}
15+
}
16+
1017
class SomeTest extends TestCase
1118
{
19+
use TraitTestCase;
1220

1321
#[DataProvider('provideFromAttribute')]
1422
public function testFoo(string $arg): void

0 commit comments

Comments
 (0)