Skip to content

Commit b410b13

Browse files
authored
fix modulo operator when handling integer-ranges without 0
1 parent 2f991d1 commit b410b13

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,11 @@ private function resolveType(Expr $node): Type
11351135
} elseif ($rightType instanceof UnionType) {
11361136
foreach ($rightType->getTypes() as $type) {
11371137
if ($type instanceof IntegerRangeType) {
1138-
$rangeMax = max($rangeMax, $type->getMax() !== null ? $type->getMax() - 1 : null);
1138+
if ($type->getMax() === null) {
1139+
$rangeMax = null;
1140+
} else {
1141+
$rangeMax = max($rangeMax, $type->getMax());
1142+
}
11391143
} elseif ($type instanceof ConstantIntegerType) {
11401144
$rangeMax = max($rangeMax, $type->getValue() - 1);
11411145
}

tests/PHPStan/Analyser/data/modulo-operator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Foo
1414
* @param int<min, 3>|int<4, max> $unionRange
1515
* @param int<min, 3>|7 $hybridUnionRange
1616
*/
17-
function doBar(int $i, $p, $range, $zeroOrMore, $intConst, $unionRange, $hybridUnionRange, $mixed)
17+
function doBar(int $i, int $j, $p, $range, $zeroOrMore, $intConst, $unionRange, $hybridUnionRange, $mixed)
1818
{
1919
assertType('int<-1, 1>', $i % 2);
2020
assertType('int<0, 1>', $p % 2);
@@ -38,5 +38,11 @@ function doBar(int $i, $p, $range, $zeroOrMore, $intConst, $unionRange, $hybridU
3838
assertType('int<0, 6>', $p % $hybridUnionRange);
3939

4040
assertType('int<0, max>', $zeroOrMore % $mixed);
41+
42+
if ($i === 0) {
43+
return;
44+
}
45+
46+
assertType('int', $j % $i);
4147
}
4248
}

0 commit comments

Comments
 (0)