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 with isset on nullable object's array property key #1884

Closed
ghost opened this issue Feb 8, 2019 · 5 comments · Fixed by phpstan/phpstan-src#345
Closed

False positive with isset on nullable object's array property key #1884

ghost opened this issue Feb 8, 2019 · 5 comments · Fixed by phpstan/phpstan-src#345

Comments

@ghost
Copy link

ghost commented Feb 8, 2019

Summary of a problem or a feature request

Checking a nullable object's array property key with isset() yields the following error:

Cannot access property $arrayProperty on Foo|null

Code snippet that reproduces the problem

isset($nullableObject->arrayProperty['key'])

https://phpstan.org/r/7ec5332d-70c0-4307-aa4e-b28b69d712d4

Expected output

No errors.

If $nullableObject is NULL, isset() returns FALSE without any errors.

Changing the condition to isset($nullableObject) && isset($nullableObject->arrayProperty['key']) works, but it's a pointless extra check I'd rather avoid 🙂

@ondrejmirtes
Copy link
Member

This is a feature. With isset($nullableObject->arrayProperty['key']), you're expressing that you're looking for whether the key exists there or not. See #1261.

So the code isset($nullableObject) && isset($nullableObject->arrayProperty['key']) is actually what you should use IMHO to express the right intent.

@ghost
Copy link
Author

ghost commented Feb 8, 2019

I understand. Should isset($nullableObject, $nullableObject->arrayProperty['key']) work then? It fails with the same error.

https://phpstan.org/r/436fbd52-13ed-4a63-a62c-0e387ad48a34

@ondrejmirtes
Copy link
Member

Yeah, probably.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#345

@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 10, 2021
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.

1 participant