phpstan-exception-rules icon indicating copy to clipboard operation
phpstan-exception-rules copied to clipboard

Exceptions thrown by callables

Open marcospassos opened this issue 6 years ago • 2 comments

Currently, wrapping a callable call into a try catch block always results in an error:

try {
    $function();
} catch (\Throwable $exception) {
}
 Throwable is never thrown in the corresponding try block 

In fact, we have no ways to detect which exception can be possibly thrown by an unknown callable. However, we could detect calls to callables inside try blocks and whitelist any exception in the catch.

marcospassos avatar Mar 24 '19 21:03 marcospassos

@pepakriz what are your thoughts on this?

marcospassos avatar Mar 24 '19 21:03 marcospassos

It's known issue: https://github.com/pepakriz/phpstan-exception-rules#anonymous-functions-are-analyzed-at-the-same-place-they-are-declared Previously, I've tried to solve it in this branch https://github.com/pepakriz/phpstan-exception-rules/tree/catch-and-throw , but the code is unnecessarily complicated and full of black magic.

As you say - detecting callable calls is maybe a good solution until native support for @throws in phpstan-core.

pepakriz avatar Mar 29 '19 10:03 pepakriz