Skip to content

unexpected narrowing type after comparing a union type series of int with a plain int #10002

@simdoor

Description

@simdoor

Bug report

I have a variable $a with type 1|2|3|4, and a variable $test with type int

After comparing the two variables the value of $test is narrowed down to exclude all the values in the union type, i.e int<min,0>|int<5,max>.
Then an error is detected if I compare $b ( 1|2|3|4) to $test

if($a === $test){
  return true;
}

// next line raises "it always evaluates to false" error
return $b === $test;

The same doesn't apply if $a and $b are of type int<1,4>

Code snippet that reproduces the problem

https://phpstan.org/r/550fcad9-e421-40dc-8987-a59ff9082ac5

Expected output

I would expect no error since the second comparison is legitimate

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

It helps every day. Xmas included

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions