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

"expects float, float|null given" error not triggered when it should #4789

Closed
keksa opened this issue Apr 1, 2021 · 6 comments
Closed

"expects float, float|null given" error not triggered when it should #4789

keksa opened this issue Apr 1, 2021 · 6 comments

Comments

@keksa
Copy link

keksa commented Apr 1, 2021

Bug report

In case method returns float|null and you check that the result is not null and then use the method again, phpstan should display an error. IIRC this was working properly in past versions.

Code snippet that reproduces the problem

https://phpstan.org/r/8b9b18a1-5a4f-4cc2-a35b-96615bd0c6eb

Expected output

Line 32 correctly triggers Parameter #1 $total of method Invoice::checkTotal() expects float, float|null given., but the same error should be triggered on lines 34 and 37.

@ondrejmirtes
Copy link
Member

Hi, so you expect errors on lines 34 and 37? FYI this change has not been released yet, it's only in dev-master, and you can force the old behaviour by using @phpstan-impure above getTotal: https://phpstan.org/r/b035ac0b-6aef-43b5-b5b1-00ca04078fa9

@enumag
Copy link
Contributor

enumag commented Apr 1, 2021

@ondrejmirtes Wait... new phpstan assumes methods to be pure? Why? 🤔

@ondrejmirtes
Copy link
Member

It's only in bleeding edge, see the difference here:

https://phpstan.org/r/29da7f5c-ca5d-45b7-bb95-c7e9df41b348 vs. https://phpstan.org/r/6744da50-73f8-45d4-9d90-341c02b9486f

It eliminates a lot of false-positives, the list of fixed issues is long:

Closes #4588
Closes #4091
Closes #3382
Closes #4177
Closes #2288
Closes #1157

The state of object is forgotten if you call an impure method on it, or pass it to an impure method or function. void methods are impure by default. If you want to force the other behaviour, you can use @phpstan-pure or @phpstan-impure above a method.

There's going to be an article about this published before the next release is out.

@keksa
Copy link
Author

keksa commented Apr 1, 2021

@ondrejmirtes Thanks for the explanation 👍

@enumag
Copy link
Contributor

enumag commented Apr 1, 2021

I see. Thanks @ondrejmirtes!

@github-actions
Copy link

github-actions bot commented May 3, 2021

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 3, 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