phpstan
phpstan copied to clipboard
PHPStan insists a `use` variable by reference cannot change types
Bug report
When writing a Closure with a use reference variable, PHPStan assumes the Type of that variable is always what it was at the place where the code is written.
Code snippet that reproduces the problem
$foo = false;
$test = function() use (&$foo) {
if (is_array($foo)) {
echo 'array';
}
else {
echo 'not array';
}
};
$foo = [];
$test();
The code above actually prints array, even though PHPStan insists that:
Call to function is_array() with false will always evaluate to false.
https://phpstan.org/r/6967bdde-8c8f-476d-adcd-c94db723cd8b
Expected output
No reported error.
Did PHPStan help you today? Did it make you happy in any way?
Yes! Love having a another pair of eyes on my code!
References are tricky for static analysis. Use this workaround: https://phpstan.org/r/d0a6e763-62c0-42f2-a99f-6817809609bd