Skip to content

False positive error for 'match' operator #5191

@sirian

Description

@sirian

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
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions