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

isset($foo['bar']) should narrow down $foo to array|object&ArrayAccess #9991

Closed
fluffycondor opened this issue Oct 8, 2023 · 4 comments
Closed

Comments

@fluffycondor
Copy link
Contributor

Bug report

isset($foo['bar']) should narrow down $foo to array|(object&ArrayAccess).

Code snippet that reproduces the problem

https://phpstan.org/r/341ecc77-27a6-4459-bb59-4e5983393db1

Expected output

No errors.

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

💖

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@6fbd6e4

The error in isset($data['title']) will stay because it's not always safe to do that on mixed: https://3v4l.org/UoG6Y

I don't want to change the type of $foo after isset($foo['bar']) because that would mean that many errors currently reported on level 9 would be reported sooner on lower levels, and that's counter-productive.

@fluffycondor
Copy link
Contributor Author

Thank you for the fast fix! I really appreciate it.

The error in isset($data['title']) will stay because it's not always safe to do that on mixed

But it's not really a mixed, it's mixed~stdClass, and there's no way to have an object there:
https://phpstan.org/r/c6c7a62e-fb22-4f25-9ca3-c47f0ee9e697
So it's perfectly safe to call isset there.
(Btw, may I fix the ReturnTypeExtension to have mixed~object there? It'd make more sense.)

I read a discussion about narrowing json_decode type and I understand your point, but I'd be really nice to consider narrowing json_decode return type starting from any major PHPStan version. Because here's one of the pitfalls having it mixed while it actually isn't.

@ondrejmirtes
Copy link
Member

may I fix the ReturnTypeExtension to have mixed~object there? It'd make more sense

Yeah, sure, please do :)

The problem with narrowing json_decode to say scalar|array type is that errors that are currently reported on level 9 because of mixed would be reported on a lower level, annoying many more people.

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 Nov 13, 2023
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

2 participants