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
Bug report
I feel this is a bug. You can check var type with match syntax like this:
Now it is clear that if
$dateis instance ofHellowWorldthe$aisint, otherwise it isbool. But phpstan doesn't understand that and treats$aalways asint|falsetype.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:
but it is reporting error:
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