phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

"Variable might not be defined" when always assigning variable in match arm

Open PrinsFrank opened this issue 1 year ago • 3 comments

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!

PrinsFrank avatar Apr 19 '24 08:04 PrinsFrank

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 avatar Apr 19 '24 08:04 ondrejmirtes

@ondrejmirtes Exactly what we did! But it made sense to still report this in case someone else does more assigning properties in match arms?

PrinsFrank avatar Apr 19 '24 08:04 PrinsFrank

I marked this as a bug report, it's a valid bug :)

ondrejmirtes avatar Apr 19 '24 09:04 ondrejmirtes