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

Null coalesce operator on nested array returns unexpected error #8069

Open
alexander-schranz opened this issue Sep 26, 2022 · 4 comments
Open
Labels
Milestone

Comments

@alexander-schranz
Copy link

Bug report

The ?? usage on a nested array which parent key does not exist throwns an error but I would not expect it e.g.:

/** @var mixed[] $test */
$test = [];

var_dump($test['hello'][0] ?? 'world');

Code snippet that reproduces the problem

https://phpstan.org/r/42618f11-503b-4ad6-ae43-0a2d642deff8

https://3v4l.org/hdYaB

Its a similar issue to isset but instead the ?? (Null coalesce operator) is used: #8068

Expected output

No error should be shown

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

We are shipping phpstan as part of our Sulu CMS skeleton since last 2.5 minor version to improve with rector the upgrade process and code quality of all Sulu based projects.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Sep 26, 2022
@ondrejmirtes
Copy link
Member

Thanks :) BTW Has Sulu considered PHPStan open-source sponsorship (https://github.com/sponsors/ondrejmirtes/), or PHPStan Pro team subscription (https://phpstan.org/blog/introducing-phpstan-pro)? If you're getting value out of PHPStan, I'd really appreciate the support :)

@rajyan
Copy link
Contributor

rajyan commented Nov 2, 2022

This is an error caused by explicit mixed, because offset access to explicit mixed is not allowed.
The linked issue has a slightly different situation, It had offset access (false)no | (constant array)yes.

The only permitted operation for explicit mixed is passing to another mixed, so the solution is

change the doc as mixed[][]
https://phpstan.org/r/e872f721-8881-4e42-a901-84a7667afd28

or if it’s not possible, you need an explicit is_array check
https://phpstan.org/r/c6e34e77-b303-4677-b6c9-ce8a0cdef0aa

@rajyan
Copy link
Contributor

rajyan commented Nov 2, 2022

but I think there were about two related issues before, and maybe supporting isset, coalesce with explicit mixed is reasonable.

@alexander-schranz
Copy link
Author

@rajyan I understand thank you for the hint. So atleast I can go with mixed[][] now to fix my issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants