Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan does not expect an exception in method call in a try block. #4879

Closed
petaak opened this issue Apr 20, 2021 · 6 comments
Closed

PHPStan does not expect an exception in method call in a try block. #4879

petaak opened this issue Apr 20, 2021 · 6 comments

Comments

@petaak
Copy link
Contributor

petaak commented Apr 20, 2021

Bug report

PHPStan does not expect an exception in method call in a try block. In the code snippet below, PHPStan should expect that the "test" method call can throw an exception.

Code snippet that reproduces the problem

https://phpstan.org/r/9e470be5-ea3b-461d-9a38-7a1ca66e526b

Expected output

No errors.

@ondrejmirtes
Copy link
Member

Why it currently doesn't work: PHPStan differentiates between explicit throw points ("throw" keyword, function calls that have @throws in the declaration etc.), and implicit throw points (function calls that do not have any @throws above them and MIGHT throw an exception).

But I found out that the analysis works best if the explicit throw points have priority. My thinking is that if that if you throw something explicitly, then all the throw points in the try block should be explicit.

See the following examples that work correctly:

I'll fix your example for the Throwable catch block. Analysis of more specific catch blocks will not be changed.

@mrrafaj
Copy link

mrrafaj commented Apr 23, 2021

Hi, I have a similar problem which is difficult for me to solve. We often on entities call "get" methods after exception to get an identifier, see .. https://phpstan.org/r/995e1b4f-bdd6-4c7b-84aa-42ecfd51760d
..any ideas ? thank you

@ondrejmirtes
Copy link
Member

@mrrafaj Your issue is most likely unrelated to this one, so pleaase open a separate issue, and always provide more accurate example. In this case PHPStan is correct. You might find out it's not a false positive and that PHPStan is in fact correct, see for example #4851 :)

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@54a204e

@petaak
Copy link
Contributor Author

petaak commented Apr 24, 2021

Thanks!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants