Skip to content

Commit 0fcd93b

Browse files
authored
Fix IntegerRange math edge case
1 parent 1b5710a commit 0fcd93b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,6 +5174,7 @@ private function integerRangeMath(Type $range, Expr $node, Type $operand): Type
51745174
}
51755175

51765176
if ($operand->getMax() === null) {
5177+
$min = null;
51775178
$max = null;
51785179
} elseif ($rangeMax !== null) {
51795180
$max = $rangeMax - $operand->getMax();

tests/PHPStan/Analyser/data/integer-range-types.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public function math($i, $j, $z, $pi, $r1, $r2, $rMin, $rMax, $x, $y) {
268268
assertType('float|int<min, 0>', $rMin / $r1);
269269

270270
assertType('int<6, max>', $r1 + $rMax);
271-
assertType('int<-4, max>', $r1 - $rMax);
271+
assertType('int', $r1 - $rMax);
272272
assertType('int<5, max>', $r1 * $rMax);
273273
assertType('float|int<0, max>', $r1 / $rMax);
274274
assertType('int<6, max>', $rMax + $r1);

tests/PHPStan/Analyser/data/math.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function doBaz(int $rangeFiveBoth, int $rangeFiveLeft, int $rangeFiveRigh
5555
assertType('0', $rangeFiveBoth - $rangeFiveBoth);
5656

5757
assertType('int<-10, max>', $rangeFiveBoth + $rangeFiveLeft);
58-
assertType('int<0, max>', $rangeFiveBoth - $rangeFiveLeft);
58+
assertType('int', $rangeFiveBoth - $rangeFiveLeft);
5959

6060
assertType('int<min, 10>', $rangeFiveBoth + $rangeFiveRight);
6161
assertType('int<min, 0>', $rangeFiveBoth - $rangeFiveRight);
@@ -95,4 +95,18 @@ public function doDolor(int $i): void
9595
assertType('float|int<min, 9>', $divThirty + 3);
9696
}
9797

98+
public function doSit(int $i, int $j): void
99+
{
100+
if ($i < 0) {
101+
return;
102+
}
103+
if ($j < 1) {
104+
return;
105+
}
106+
107+
assertType('int<0, max>', $i);
108+
assertType('int<1, max>', $j);
109+
assertType('int', $i - $j);
110+
}
111+
98112
}

0 commit comments

Comments
 (0)