Skip to content

Commit 6921204

Browse files
marcospassospepakriz
authored andcommitted
Improve annotation reader to traverse class hierarchy (#94)
* Improve annotation reader consider traverse class hierarchy * Catch reflection exception
1 parent 5a2f110 commit 6921204

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ parameters:
2121
- PHPUnit\Framework\Exception
2222
- Nette\DI\MissingServiceException
2323
methodThrowTypeDeclarations:
24+
ReflectionMethod:
25+
getPrototype:
26+
- ReflectionException
2427
PHPStan\Broker\Broker:
2528
getClass:
2629
- PHPStan\Broker\ClassNotFoundException

src/ThrowsAnnotationReader.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,19 @@ private function getDocblock($reflection): ?string
130130
{
131131
if ($reflection instanceof MethodReflection) {
132132
$declaringClass = $reflection->getDeclaringClass();
133-
$nativeClassReflection = $declaringClass->getNativeReflection();
134-
$nativeMethodReflection = $nativeClassReflection->getMethod($reflection->getName());
135-
$docBlock = $nativeMethodReflection->getDocComment();
133+
$classReflection = $declaringClass->getNativeReflection();
134+
$methodReflection = $classReflection->getMethod($reflection->getName());
135+
$docBlock = $methodReflection->getDocComment();
136+
137+
while ($docBlock === false) {
138+
try {
139+
$methodReflection = $methodReflection->getPrototype();
140+
} catch (ReflectionException $exception) {
141+
return null;
142+
}
143+
144+
$docBlock = $methodReflection->getDocComment();
145+
}
136146

137147
return $docBlock !== false ? $docBlock : null;
138148
}

tests/src/Rules/data/unused-descriptive-throws.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,22 @@ function descriptiveAnnotationAlias(): void
5757
throw new LogicException();
5858
}
5959

60-
class UnusedThrows
60+
interface Foo {
61+
62+
/**
63+
* @throws LogicException Description.
64+
*/
65+
public function uncheckedException(): void;
66+
}
67+
68+
class UnusedThrows implements Foo
6169
{
6270

71+
public function uncheckedException() : void
72+
{
73+
throw new LogicException();
74+
}
75+
6376
/**
6477
* @throws RuntimeException
6578
* @throws RuntimeException Description.

0 commit comments

Comments
 (0)