-
-
Notifications
You must be signed in to change notification settings - Fork 921
Accessing ::class or calling get_class() on nullable types does not lead to a warning #13069
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
Comments
You can use https://phpstan.org/r/6d9edb50-77bf-4997-b737-ac4a2251026d Your analysis looks correct to me for the cases given |
Indeed, using $ php -r "\$foo = null; var_dump(get_class(\$foo));"
PHP Fatal error: Uncaught TypeError: get_class(): Argument #1 ($object) must be of type object, null given in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1 While I said the issues is PhpStan not complaining on accessing |
@driehle You're doing your tests on level 6. Union types (except null) are reported level 7 This is a level 8 error https://phpstan.org/r/349f2d3d-a0d6-4f93-9837-1a0e20da486d |
Ok, I think I got that. So it is then just a difference in how |
I tried a fix phpstan/phpstan-src#4011 |
Bug report
I noticed that the following code produces four warnings:
Code snippet that reproduces the problem
https://phpstan.org/r/f9c2c069-07b2-46a3-b83c-837205e6581f
Expected output
The errors surprised me, as the type is actually checked and, hence, the code reported by PhpStan will not produce any errors during runtime. The feature of checking types by
switch
was covered in #745, #4896.I found, however, that the code above seems to be related to
$object
being nullable. If you change the method signature from?ResourceInterface $object = null
toResourceInterface $object
, the errors are suddenly gone!Next, I discovered that accessing
::class
on null actually throws an error in PHP 8.4.7 (and probably all PHP 8 versions):So PhpStan is actually right in reporting something, though I think the reported errors are incorect. In my opinion, PhpStan should complain about accessing
::class
on nullable types.Did PHPStan help you today? Did it make you happy in any way?
No response
The text was updated successfully, but these errors were encountered: