phpstan
phpstan copied to clipboard
"Variable might not be defined" when always assigning variable in match arm
Bug report
When a variable is assigned in each arm of a match statement, it will still be reported as "Variable $foo might not be defined". The code can be rewritten to be easier to read, but this still looks like a bug.
Code snippet that reproduces the problem
https://phpstan.org/r/b99db06d-4934-4b9d-8862-778f00bee23b
Expected output
Variable $foo should always be considered defined when it is assigned in each match arm.
Did PHPStan help you today? Did it make you happy in any way?
I love PHPStan, level 9 and strict rules all the way!
As a workaround you can do https://phpstan.org/r/63e2f182-9ed9-4536-8d24-1d75821d9064 which makes far more sense for a match expression :)
@ondrejmirtes Exactly what we did! But it made sense to still report this in case someone else does more assigning properties in match arms?
I marked this as a bug report, it's a valid bug :)