phpstan / phpstan

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

False positives with unpacked non-null arguments in object #9334

Open suehiro24 opened 1 year ago

suehiro24 commented 1 year ago

Bug report

When casting an object with guaranteed non-null properties to an array, unpacking it, and passing it as arguments to a function, an error like the following occurs.

Parameter $buz of closure expects string, string|null given.

Code snippet that reproduces the problem

https://phpstan.org/r/6dccf8af-8bff-4139-8571-2d11d6e75684

No errors occur when non-null is guaranteed for elements of a cast array.

https://phpstan.org/r/29ceea2b-d167-4eb5-8a20-041c221f32a6

Expected output

No errors.

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

Thanks to PHPStan, I can proceed with coding while minimizing my worries. I always appreciate it!

phpstan-bot commented 2 weeks ago

@suehiro24 After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@

 17: Parameter $buz of closure expects string, string|null given.

-PHP 7.4 (3 errors)
+PHP 7.2 – 7.4 (3 errors)
 ==========

  7: Promoted properties are supported only on PHP 8.0 and later.
 17: Named arguments are supported only on PHP 8.0 and later.
-17: Parameter $buz of closure expects string, string|null given.
-
-PHP 7.2 – 7.3 (1 error)
-==========
-
-14: Syntax error, unexpected T_VARIABLE, expecting ')' on line 14
+17: Parameter $buz of closure expects string, string|null given.
Full report PHP 8.0 – 8.2 (1 error) ----------- | Line | Error | |---|---| | 17 | `Parameter $buz of closure expects string, string|null given.` | PHP 7.2 – 7.4 (3 errors) ----------- | Line | Error | |---|---| | 7 | `Promoted properties are supported only on PHP 8.0 and later.` | | 17 | `Named arguments are supported only on PHP 8.0 and later.` | | 17 | `Parameter $buz of closure expects string, string|null given.` |
phpstan-bot commented 2 weeks ago

@suehiro24 After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@

 No errors

-PHP 7.4 (2 errors)
+PHP 7.2 – 7.4 (2 errors)
 ==========

  7: Promoted properties are supported only on PHP 8.0 and later.
-19: Named arguments are supported only on PHP 8.0 and later.
-
-PHP 7.2 – 7.3 (1 error)
-==========
-
-14: Syntax error, unexpected T_VARIABLE, expecting ')' on line 14
+19: Named arguments are supported only on PHP 8.0 and later.
Full report PHP 8.0 – 8.2 ----------- No errors PHP 7.2 – 7.4 (2 errors) ----------- | Line | Error | |---|---| | 7 | `Promoted properties are supported only on PHP 8.0 and later.` | | 19 | `Named arguments are supported only on PHP 8.0 and later.` |