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

Type is not resolvable via match and instanceof check #10843

Description

@sampsasaarela

Bug report

I feel this is a bug. You can check var type with match syntax like this:

$a = match (true) {
  $date instanceof HelloWorld => 1,
  default => false
};

Now it is clear that if $date is instance of HellowWorld the $a is int, otherwise it is bool. But phpstan doesn't understand that and treats $a always as int|false type.

Code snippet that reproduces the problem

https://phpstan.org/r/f2051cc3-1650-4fee-9634-55c209e2500c

Expected output

Full test code, which should not report an error:

<?php declare(strict_types = 1);

class HelloWorld
{
	public function sayHello(HelloWorld|null $date): int
	{
		$a = match (true) {
			$date instanceof HelloWorld => 1,
			default => false
		};

		if ($date instanceof HelloWorld) {
			return $a;
		}

		throw new Exception('Error');
	}
}

but it is reporting error:

Line 13 Method HelloWorld::sayHello() should return int but returns int|false.

Example is very stupid, and just demonstrates the issue.

Some code works if written with if-else syntax: https://phpstan.org/r/cc487fbd-cbf8-46b6-ad2f-5d28bfb5d9c9

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

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions