Skip to content

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

Closed
phpstan/phpstan-src
#5808

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

Activity

  1. changed the title [-]Narrow the subject of a match(..) clause based on the narrowing of its result[/-] [+]Narrow the subject of a match(...) clause based on the narrowing of its result[/+] on Jun 5, 2026
  2. added this to the Easy fixes milestone on Jun 5, 2026
  3. github-actions commented on Jul 7, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  4. locked as resolved and limited conversation to collaborators on Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions