Skip to content

Scalar type does not match IntegerRange which describes the same range #3383

@NLthijs48

Description

@NLthijs48

Bug report

When you have marked a class property with a scalar type like 0|1|2|3, and try to assign a number to this property that meets the bounds (between 0 and 3), PHPStan complains with the following error:

Property HelloWorld::$classification (0|1|2|3) does not accept int<0, 3>.

I saw that integer ranges are tracked internally #2484, but I don't see a way to specify them in PHPDoc, I would expect int<0, 3> to work, but that is not recognized. One way to write code that works is using an if statement that checks in_array($int, [0,1,2,3], true), after which PHPStan does allow the assignment to the class propery.

Code snippet that reproduces the problem

https://phpstan.org/r/8f3c6888-6565-487b-a7ae-7ebbd3baee49

Expected output

The issue was reported incorrectly, it would be great if PHPStan would recognize these types to match, and allow the assignment without issues.

Praise

Overall I'm really impressed with PHPStan, we use it in our CI/CD pipeline, and it has saved the day more than once by pointing out nasty issues. It got us a lot more confidence in our code. Thanks for building this great tool!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions