🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Narrow the subject of a match(...) clause based on the narrowing of its result #14772

Description

@claudepache

Feature request

In the following case, if I narrow the computed value $class, then the original value $x is narrowed accordingly:

function test_if(mixed $x) {
	if ($x === 'aa')
		$class = 'some_class';
	elseif ($x === 'bb' || $x === 'cc')
		$class = 'another_class';
	else 
		$class = null;
	if ($class === null)
		 return;
	\PHPStan\dumpType($x); // 'aa'|'bb'|'cc'
};

I want the same when I use match() instead of if:

function test_match(mixed $x) {
	$class = match ($x) {
		'aa' => 'some_class',
		'bb', 'cc' => 'another_class',
		default => null,		
	};
	if ($class === null)
		 return;
	\PHPStan\dumpType($x); // expected: 'aa'|'bb'|'cc'; actual: mixed
};

Code snippet

https://phpstan.org/r/6a443593-a414-49ff-ab67-ea675e7b3452

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions