-
-
Notifications
You must be signed in to change notification settings - Fork 946
Closed
phpstan/phpstan-src
#4888Labels
Milestone
Description
Bug report
Code flow analyze for match incomplete. Code below triggers Variable $pow might not be defined.
Code snippet that reproduces the problem
playground: https://phpstan.org/r/e7f37746-e049-4d3b-a02f-986ce8e06e64
<?php declare(strict_types = 1);
function foo(int $value, ?string $suffix): int {
match ($suffix) {
'k', 'K' => $pow = 1,
'm', 'M' => $pow = 2,
'g', 'G' => $pow = 3,
default => $pow = 0
};
return $value * (1024 ** $pow); // False positive error: Variable $pow might not be defined.
}UPD. I know that it's not recommended to use match operator in this way and this code should be rewritten like below. It's just for example purpose
$pow = match ($suffix) {
'k', 'K' => 1,
'm', 'M' => 2,
'g', 'G' => 3,
default => 0
};Reactions are currently unavailable