-
-
Notifications
You must be signed in to change notification settings - Fork 934
Description
Bug report
It looks like using array_key_exists on an array variable makes PHPStan assume incorrect things about the other keys in the array.
Here I’m executing some code only if the someParam key is present in the array. The code retrieves all the keys matching a certain pattern (which has nothing to do with the key mentioned before). PHPStan seems to be certain that there will be no keys returned by this array_filter call, and tells me "Comparison operation ">" between 0 and 0 is always false."
If I simply comment out the wrapping if, it doesn’t produce an error.
Code snippet that reproduces the problem
https://phpstan.org/r/72a84125-8383-4cb7-9b90-9018859e3fc9
public function f($body)
{
if (array_key_exists("someParam", $body)) {
$someKeys = array_filter(
array_keys($body),
fn ($key) => preg_match("/^somePattern[0-9]+$/", $key)
);
if (count($someKeys) > 0) {
return 1;
}
return 0;
}
}
Expected output
There should be no PHPStan error (at least for level 4), if I’m not mistaken.
The function should return 1 for this input:
[
"someParam" => "auiea",
"somePattern43" => "TTT",
"somePattern111" => "98A0E",
]
]
The function should return 0 for this input:
[
"someParam" => "auiea",
"blah43" => "TTT",
"blah111" => "98A0E",
]
Did PHPStan help you today? Did it make you happy in any way?
Really happy today and everyday I’m working at fixing things reported by PHPStan. I really love the feeling of upgrading your quality level one by one. Trying to get from 3 to 4 today!