Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NLthijs48 opened this issue Jun 2, 2020 · 4 comments · Fixed by phpstan/phpstan-src#1742
Closed
Labels
Milestone

Comments

@NLthijs48
Copy link
Contributor

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!

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 2, 2020
@clxmstaab
Copy link
Contributor

also stumbled over this today.

I would expect that https://phpstan.org/r/6fdf564e-958e-4177-b330-c370f68739b7 should be considered valid using @var int<0, 100>.

I guess its the same feature request as the initially reported issue

@ondrejmirtes
Copy link
Member

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

@phpstan-bot
Copy link
Contributor

@clxmstaab After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-13: PHPDoc tag @var for property OrderState::$percentage contains unresolvable type.
-13: Property OrderState::$percentage has no typehint specified.
+No errors

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants