Skip to content

Commit f5fdbcd

Browse files
marcospassospepakriz
authored andcommitted
Add support for detecting exceptions thrown by magic methods (#58)
1 parent b991540 commit f5fdbcd

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This extension provides following rules and features:
1010

1111
* Require `@throws` annotation when some checked exception is thrown ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/throws-annotations.php))
1212
* Exception propagation over:
13-
* function calls
14-
* dynamic and static method calls
13+
* Function calls
14+
* Magic, dynamic and static method calls
1515
* Iterable interface in foreach and in `iterator_*()` functions ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/iterators.php))
1616
* Countable interface combinated with `count()` function ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/countables.php))
1717
* JsonSerializable interface combinated with `json_encode()` function ([examples](https://github.com/pepakriz/phpstan-exception-rules/blob/master/tests/src/Rules/data/json-serializable.php))

src/Rules/ThrowsPhpDocRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ private function processMethodCall(MethodCall $node, Scope $scope): array
216216
try {
217217
$targetMethodReflection = $targetClassReflection->getMethod($methodName->toString(), $scope);
218218
} catch (MissingMethodFromReflectionException $e) {
219-
continue;
219+
try {
220+
$targetMethodReflection = $targetClassReflection->getMethod('__call', $scope);
221+
} catch (MissingMethodFromReflectionException $e) {
222+
continue;
223+
}
220224
}
221225

222226
$throwType = $this->dynamicThrowTypeService->getMethodThrowType($targetMethodReflection, $node, $scope);

tests/src/Rules/data/throws-annotations.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ function foo() {
2121
throw new RuntimeException();
2222
}
2323

24+
class MagicService
25+
{
26+
27+
/**
28+
* @throws CheckedException
29+
*/
30+
public function __call($name, $arguments): void
31+
{
32+
throw new CheckedException();
33+
}
34+
35+
}
36+
2437
class ThrowsAnnotationsClass
2538
{
2639

@@ -164,6 +177,11 @@ public function callStaticUnionContainsUnknown(): void
164177
$union::staticFoo(); // error: Missing @throws Pepakriz\PHPStanExceptionRules\Rules\Data\SomeRuntimeException annotation
165178
}
166179

180+
public function callMagicMethod(): void
181+
{
182+
(new MagicService())->foo(); // error: Missing @throws Pepakriz\PHPStanExceptionRules\Rules\Data\CheckedException annotation
183+
}
184+
167185
}
168186

169187
class ThrowInConstructor

0 commit comments

Comments
 (0)