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 assumes function/method returns value even if it can throw an exception #3710

Closed
DaveLiddament opened this issue Aug 4, 2020 · 12 comments

Comments

@DaveLiddament
Copy link

Bug report

Referring the the code snippet... It appears that PHPStan assumes that $person is always set. Given the method createPersonButCouldFail can throw an exception (and is annotated with @throws), this will not always be the case.

Code snippet that reproduces the problem

https://phpstan.org/r/671b1297-7856-4edf-871f-8f9e1d4f7169

Expected output

Expect 2 outcomes:

  1. Issue currently reported on line 22 (If condition is always true) is not reported
  2. An issue is reported on line 27 (Cannot call method aMethod() on Person|null.)
@DaveLiddament
Copy link
Author

A better code snippet might be this: https://phpstan.org/r/cbd2c0cb-5b13-44e8-af1d-ce3bc6984456
In this case createPersonButCouldFail will create a Person object sometimes and throw exceptions other times.

Also if you look at this snippet (https://phpstan.org/r/ee4105dd-a1a1-45a8-9f44-ed1dd0cd03d9) it looks like PHPStan is half right. It knows that person2 is always null in the finally block. But not outside.

@ondrejmirtes
Copy link
Member

Similar problems: #2969, #3004

@ondrejmirtes ondrejmirtes added this to the NodeScopeResolver bugs milestone Aug 4, 2020
@muglug
Copy link
Contributor

muglug commented Aug 11, 2020

An issue is reported on line 27 (Cannot call method aMethod() on Person|null.)

This is wrong – that line won't be reached if the try block fails

@DaveLiddament
Copy link
Author

I agree with @muglug, I am wrong about line 27. Please ignore that part of bug report. Sorry about about that.

However line 22 is still an issue: https://phpstan.org/r/fc66daf3-efad-4edf-8a96-72dbd56e81b9

@scaytrase
Copy link

https://phpstan.org/r/b3328236-bda4-4ebb-92c3-7ada868b1d84 I bring my own example too.

@phpstan-bot
Copy link
Contributor

@DaveLiddament After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-22: If condition is always true.
+No errors

@phpstan-bot
Copy link
Contributor

@DaveLiddament After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-26: If condition is always true.
+No errors

@phpstan-bot
Copy link
Contributor

@DaveLiddament After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
 27: Unreachable statement - code above always terminates.
-29: If condition is always true.
 32: If condition is always false.
 38: Unreachable statement - code above always terminates.
Full report
Line Error
27 Unreachable statement - code above always terminates.
32 If condition is always false.
38 Unreachable statement - code above always terminates.

@phpstan-bot
Copy link
Contributor

@DaveLiddament After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-22: If condition is always true.
+No errors

@phpstan-bot
Copy link
Contributor

@scaytrase After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-21: Else branch is unreachable because ternary operator condition is always true.
-21: Strict comparison using !== between string and null will always evaluate to true.
+No errors

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#481

@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 Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants