Skip to content

Type is not resolvable via match and instanceof check #10843

Closed
phpstan/phpstan-src
#5808

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

Activity

  1. staabm commented on Apr 6, 2024

    @staabm
    Contributor
  2. added this to the Dependent types milestone on Apr 6, 2024
  3. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Collaborator

    @sampsasaarela After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
     
     No errors
     
    -PHP 7.2 – 7.4 (1 error)
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
    -5: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    +5: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    +5: Parameter $date of method HelloWorld::sayHello() has invalid type null.
    Full report

    PHP 8.0 – 8.3

    No errors

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    5 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    5 Parameter $date of method HelloWorld::sayHello() has invalid type null.
  4. phpstan-bot commented on Apr 1, 2026

    @phpstan-bot
    Collaborator

    @sampsasaarela After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
     
     No errors
     
    -PHP 7.2 – 7.4 (1 error)
    +PHP 7.2 – 7.4 (3 errors)
     ==========
     
    - 5: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    + 5: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    + 5: Parameter $date of method HelloWorld::sayHello() has invalid type null.
    +14: Method HelloWorld::sayHello() should return int but returns int|false.
    Full report

    PHP 8.0 – 8.3

    No errors

    PHP 7.2 – 7.4 (3 errors)

    Line Error
    5 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    5 Parameter $date of method HelloWorld::sayHello() has invalid type null.
    14 `Method HelloWorld::sayHello() should return int but returns int
  5. 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.

  6. 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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions