-
Notifications
You must be signed in to change notification settings - Fork 428
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
use IntegerRangeType
in modulo-operator
#614
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace ModuloOperator; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @param positive-int $p | ||
* @param int<5, 10> $range | ||
* @param int<0, max> $zeroOrMore | ||
*/ | ||
function doBar(int $i, $p, $range, $zeroOrMore, $mixed) | ||
{ | ||
assertType('int<-1, 1>', $i % 2); | ||
assertType('int<0, 1>', $p % 2); | ||
|
||
assertType('int<-2, 2>', $i % 3); | ||
assertType('int<0, 2>', $p % 3); | ||
|
||
assertType('int<0, 2>', $range % 3); | ||
|
||
assertType('int<-9, 9>', $i % $range); | ||
assertType('int<0, 9>', $p % $range); | ||
|
||
assertType('int<0, max>', $zeroOrMore % $mixed); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will lead to creating
int<X, max>
when you have1|2|3
which is wrong I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another great catch. added a test and a fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because
instanceof *Type
is almost always wrong and you can find edge-cases like that.. https://phpstan.org/developing-extensions/type-system#querying-a-specific-typeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now it's still wrong for unions of disjoint ranges like
int<min, 3>|int<4, max>
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got this case and a few more also covered. I guess you won't like that, I had to add a few more
instanceof *Type
calls.I somehow get the feeling you let me built this thing intentionally and we will now refactor the logic regarding merging
IntegerRangeType
andConstantIntegerType
types (or unions of it) into eitherTypeUtils
orIntegerRange
class, right? ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's fine like that. A correct approach is to add use-case based methods on
PHPStan\Type\Type
that would tell us exactly what we need here. EachType
implementation would return what it knows.I'm preparing myself mentally for this refactoring for a long time, in the end
PHPStan\Type\Type
might have hundreds of methods, and it might be the only valid situation considered quality code in the universe 😂