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

False positive in Property Class::$property is never read, only written-rule when using empty() #5971

Closed
jaylinski opened this issue Nov 10, 2021 · 6 comments · Fixed by phpstan/phpstan-src#1174
Labels
Milestone

Comments

@jaylinski
Copy link

Bug report

Rule Property Class::$property is never read, only written reports a false-positive when the class-property is "read" with empty().

Code snippet that reproduces the problem

https://phpstan.org/r/89c4e6d0-5ef4-492b-95fb-f0b7ed51a8ca

Expected output

There is no error when a class-property is "read" via empty().

Did PHPStan help you today? Did it make you happy in any way?

PHPStan v1 new rules are awesome. 👍

@herndlm
Copy link
Contributor

herndlm commented Nov 11, 2021

Same thing with isset. I think this might be on purpose, at least I think it's helpful as in your case this could also be something like a bool property? Debatable though I guess ;)
And checking uninitialised typed properties with isset and empty does not fail in PHP. Really accessing it directly though would through a fatal error AFAIK

@jaylinski
Copy link
Author

jaylinski commented Nov 11, 2021

I think this might be on purpose, at least I think it's helpful as in your case this could also be something like a bool property

That's true. Using a non-boolean property for boolean checks is not really "good/clean" code.

An alternative error like Property Class::$property is non-boolean but only used in a boolean way (or something like this 😅) would be nice.

Also: this error doesn't happen when using is_array() or is_null().

@paranoiq
Copy link
Sponsor Contributor

the same problem when using null coalesce (??) https://phpstan.org/r/7804477b-a7d9-4132-a5fd-3e409ac82ace

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 1, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 3, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 3, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 4, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 5, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 5, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 5, 2022
rajyan added a commit to rajyan/phpstan-src that referenced this issue Apr 5, 2022
@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#1174

@jaylinski
Copy link
Author

Thank you! 🥇

@github-actions
Copy link

github-actions bot commented May 7, 2022

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

Successfully merging a pull request may close this issue.

4 participants