You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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!
The text was updated successfully, but these errors were encountered:
@clxmstaab Your problem is different, int<0, 100> isn't valid PHPDoc type for PHPStan, it's not documented here https://phpstan.org/writing-php-code/phpdoc-types and not implemented. Only positive-int and negative-int are curently valid integer range types in PHPDocs. Of course the parser support is already there so it's just a matter of a bit of code added to TypeNodeResolver to also support this.
@@ @@-13: PHPDoc tag @var for property OrderState::$percentage contains unresolvable type.-13: Property OrderState::$percentage has no typehint specified.+No errors
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 checksin_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!
The text was updated successfully, but these errors were encountered: