phpstan / phpstan

PHP Static Analysis Tool - discover bugs in your code without running it!
https://phpstan.org/
MIT License
12.87k stars 877 forks source link

Cannot access offset '...' on mixed. #7080

Closed ddebin closed 2 years ago

ddebin commented 2 years ago

Bug report

Get an error if I check for the presence of an element in a deep (multidimensional) array.

Code snippet that reproduces the problem

https://phpstan.org/r/e05834cf-4147-493a-976f-ecad5bddec7b

Expected output

No error. As it is the correct way suggested in PHP doc ("Checking deeper array values") https://www.php.net/manual/en/function.isset.php

ondrejmirtes commented 2 years ago

This is an expected level 9 error (you get no error on level 8). Because this is a potentially dangerous operation: https://3v4l.org/RegCt

ddebin commented 2 years ago

Thanks for your fast reply @ondrejmirtes. Very useful tool btw :-)

ondrejmirtes commented 2 years ago

Thanks :) You can use any of the type-narrowing techniques to get rid of the error: https://phpstan.org/writing-php-code/narrowing-types (but a more specific PHPDoc instead of mixed[] would be best, like an array shape: https://phpstan.org/writing-php-code/phpdoc-types#array-shapes)

github-actions[bot] commented 2 years ago

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.