You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong inferred type after variable assignment in ternary if statements #13334
I have a validation method that checks if the value provided is a string with a zero-padded integer larger than 0 (like '00123').
I use ternary ifs to narrow down the type. If 'strict mode' is used, the value must be a string, otherwise I use a helper function to ensure it's a string. Then finally, it checks the format using a preg_match call.
Issue 1 - Wrong inferred type
PHPStan gives an error on the $subject parameter of preg_match, saying its type is not string.
Inline example: Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.
Using functions example: Parameter #2 $subject of function preg_match expects string, mixed given.
However, this is wrong.
Because of the && operators, the call to preg_matchonly happens if the value is a string at that point. So it should not be an issue, right?
Issue 2 - Inconsistent inferred type
Funny enough the inferred type of the value is different when the code is not using functions.
@@ @@-PHP 8.0 – 8.5 (2 errors)+PHP 8.0 – 8.5 (3 errors)
==========
+13: No error to ignore is reported on line 13.
31: Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.
52: Parameter #2 $subject of function preg_match expects string, mixed given.
-PHP 7.2 – 7.4 (8 errors)+PHP 7.2 – 7.4 (9 errors)
==========
+13: No error to ignore is reported on line 13.
31: Parameter #2 $subject of function preg_match expects string, bool|int|string|null given.
37: Parameter $value of function ensureString() has invalid type mixed.
40: Call to function is_numeric() with mixed will always evaluate to false.
Full report
PHP 8.0 – 8.5 (3 errors)
Line
Error
13
No error to ignore is reported on line 13.
31
`Parameter #2 $subject of function preg_match expects string, bool
52
Parameter #2 $subject of function preg_match expects string, mixed given.
PHP 7.2 – 7.4 (9 errors)
Line
Error
13
No error to ignore is reported on line 13.
31
`Parameter #2 $subject of function preg_match expects string, bool
37
Parameter $value of function ensureString() has invalid type mixed.
40
Call to function is_numeric() with mixed will always evaluate to false.
40
Casting to *NEVER* something that's already *NEVER*.
40
Variable $value in empty() always exists and is not falsy.
43
Parameter $value of function isNonZeroPaddedString() has invalid type mixed.
46
Call to function is_string() with mixed will always evaluate to false.
52
`Parameter #2 $subject of function preg_match expects string, mixed
Activity
phpstan-bot commentedon Jul 31, 2025
@vHeemstra After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.5 (3 errors)
No error to ignore is reported on line 13.Parameter #2 $subject of function preg_match expects string, mixed given.PHP 7.2 – 7.4 (9 errors)
No error to ignore is reported on line 13.Parameter $value of function ensureString() has invalid type mixed.Call to function is_numeric() with mixed will always evaluate to false.Casting to *NEVER* something that's already *NEVER*.Variable $value in empty() always exists and is not falsy.Parameter $value of function isNonZeroPaddedString() has invalid type mixed.Call to function is_string() with mixed will always evaluate to false.Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
Add regression tests for #14999 and #13334
github-actions commentedon Oct 4, 2026
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.