implemented math on IntegerRangeType and ConstantIntegerType#637
implemented math on IntegerRangeType and ConstantIntegerType#637ondrejmirtes merged 21 commits intophpstan:masterfrom
Conversation
|
the remaining out-of-memory error is unrelated. I think this is good to go. in the PR description I have separated the possible future improvements from the todo list. |
staabm
left a comment
There was a problem hiding this comment.
@ondrejmirtes I think this one is now good to go.
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0', $r1 / $z); |
There was a problem hiding this comment.
the only case, for which I am kind of suprised/not sure is this assertion. everything else feels solid to me
There was a problem hiding this comment.
Yeah, this is definitely a bug, it's never a zero.
Also, there are more divison bugs - in some cases where a float can be produced, the correct type should be int<X, Y>|float.
There was a problem hiding this comment.
ahh yes.. obviously we can get a float here :-).
fixed.
There was a problem hiding this comment.
Did you also cover a case where only int can be produced? And maybe don't do a range but a union of constant integers?
Like:
// $x 20|40|60
// $y 2|4
\PHPStan\dumpType($x / $y); // 5|10|15|20|30There was a problem hiding this comment.
Might already be covered by MutatingScope::calculateFromScalars actually
There was a problem hiding this comment.
just added your testcase which succeeds
| } | ||
|
|
||
| $dotsCount = $parentPartsCount - $i; | ||
| if ($dotsCount < 0) { |
There was a problem hiding this comment.
with the new knowledge phpstan gets by this PR, it now knows that this case can never happen.
removed the code, because we get:
------ ---------------------------------------------------------------------
Line src/File/ParentDirectoryRelativePathHelper.php
------ ---------------------------------------------------------------------
58 Comparison operation "<" between int<0, max> and 0 is always false.
------ ---------------------------------------------------------------------
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0', $r1 / $z); |
There was a problem hiding this comment.
Yeah, this is definitely a bug, it's never a zero.
Also, there are more divison bugs - in some cases where a float can be produced, the correct type should be int<X, Y>|float.
e70bf85 to
2f12f75
Compare
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0|float', $r1 / $z); |
There was a problem hiding this comment.
Still don't get this case. It can be a non-zero integer as well.
There was a problem hiding this comment.
added a line to handle this case explicitly. the test was adjusted to float only, without the 0.
|
Thank you! |
initial pass on implementing simple math operations on the
IntegerRangeType.closes phpstan/phpstan#5457
closes phpstan/phpstan#5515
for the time beeing I have only implemented
+and-betweenConstantIntegerTypeandIntegerRangeTypeto gather initial feedback on the implemation details.I am aware that this is missing lot of stuff, e.g.
ConstantIntegerTypeand right type isIntegerRangeTypepossible future improvements (separate PR)
pow()