Skip to content

Commit 60f36f0

Browse files
phpstan-botondrejmirtes
authored andcommitted
Derive the % result range from the divisor's magnitude and the dividend's sign and bounds
* `InitializerExprTypeResolver::getModType()` no longer has three divergent branches (single range / constant / union). A new `getIntegerBounds()` helper collapses an integer range, an integer constant or a union of those into one `[min, max]` pair, and `getMaxModuloMagnitude()` turns that pair into `max(|min|, |max|) - 1`. * The union branch used `$type->getMax()` for range members but `value - 1` for constant members, so `int<1, 5>|int<10, 20>` produced an off-by-one `int<0, 20>`. * The union branch reused `null` for both "no member seen yet" and "unbounded", so an unbounded member was overwritten by a later one (`int<30, max>|7` produced `int<0, 6>`), and `max(null, 0)` silently dropped a real bound of 0. * The bound is now taken from the divisor's magnitude instead of its signed value, so a negative divisor no longer yields `*NEVER*` (`int<-20, -10>|int<-5, -3>`, and the non-union cases of phpstan/phpstan#15242: `$x % -9`, `$x % int<-10, -5>`, `$x % int<-20, 5>`). * `PHP_INT_MIN` as the divisor (or as the lower end of a range divisor) is treated as unbounded instead of overflowing `abs()`/`- 1` into a float. * Analogous cases fixed with the same computation: a non-positive dividend now gives a non-positive result (`int<min, 0> % int<10, 20>` is `int<-19, 0>`, not `int<-19, 19>`), a range divisor with an unbounded lower end is no longer treated as bounded (`$x % int<min, 20>` is `int<0, max>`, not `int<0, 19>`), and the result is additionally bounded by the dividend (`int<0, 10> % int<5, 20>` is `int<0, 10>`), which also applies when the divisor is not an integer type. * Probed and found already correct: `%=` (shares `getModType()`), `/` via `getDivType()`/`integerRangeMath()`, and `intdiv()`/`fmod()`, which have no range logic. `$rMin / -1` in integer-range-types.php now drops the `float` member because `int<min, 5> % -1` correctly resolves to `0` instead of `*NEVER*`.
1 parent 67107c2 commit 60f36f0

6 files changed

Lines changed: 181 additions & 31 deletions

File tree

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
use PHPStan\Type\UnionType;
100100
use stdClass;
101101
use Throwable;
102+
use function abs;
102103
use function array_key_exists;
103104
use function array_keys;
104105
use function array_map;
@@ -122,6 +123,7 @@
122123
use function str_starts_with;
123124
use function strtolower;
124125
use const INF;
126+
use const PHP_INT_MIN;
125127

126128
#[AutowiredService]
127129
final class InitializerExprTypeResolver
@@ -1453,41 +1455,107 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
14531455
}
14541456
}
14551457

1456-
$positiveInt = IntegerRangeType::fromInterval(0, null);
1458+
$maxMagnitude = null;
14571459
if ($rightType->isInteger()->yes()) {
1458-
$rangeMin = null;
1459-
$rangeMax = null;
1460-
1461-
if ($rightType instanceof IntegerRangeType) {
1462-
$rangeMax = $rightType->getMax() !== null ? $rightType->getMax() - 1 : null;
1463-
} elseif ($rightType instanceof ConstantIntegerType) {
1464-
$rangeMax = $rightType->getValue() - 1;
1465-
} elseif ($rightType instanceof UnionType) {
1466-
foreach ($rightType->getTypes() as $type) {
1467-
if ($type instanceof IntegerRangeType) {
1468-
if ($type->getMax() === null) {
1469-
$rangeMax = null;
1470-
} else {
1471-
$rangeMax = max($rangeMax, $type->getMax());
1472-
}
1473-
} elseif ($type instanceof ConstantIntegerType) {
1474-
$rangeMax = max($rangeMax, $type->getValue() - 1);
1475-
}
1476-
}
1460+
$divisorBounds = $this->getIntegerBounds($rightType);
1461+
if ($divisorBounds !== null) {
1462+
$maxMagnitude = self::getMaxModuloMagnitude($divisorBounds[0], $divisorBounds[1]);
14771463
}
1464+
}
14781465

1479-
if ($positiveInt->isSuperTypeOf($leftType)->yes()) {
1480-
$rangeMin = 0;
1481-
} elseif ($rangeMax !== null) {
1482-
$rangeMin = $rangeMax * -1;
1466+
[$leftMin, $leftMax] = $this->getIntegerBounds($leftType) ?? [null, null];
1467+
if ($leftMin === null && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()) {
1468+
$leftMin = 0;
1469+
}
1470+
if ($leftMax === null && IntegerRangeType::fromInterval(null, 0)->isSuperTypeOf($leftType)->yes()) {
1471+
$leftMax = 0;
1472+
}
1473+
1474+
// The result has the sign of the dividend and is never bigger in magnitude than either
1475+
// the dividend or the largest magnitude the divisor can have.
1476+
if ($leftMax !== null && $leftMax <= 0) {
1477+
$rangeMax = 0;
1478+
} elseif ($maxMagnitude === null) {
1479+
$rangeMax = $leftMax;
1480+
} elseif ($leftMax === null) {
1481+
$rangeMax = $maxMagnitude;
1482+
} else {
1483+
$rangeMax = min($leftMax, $maxMagnitude);
1484+
}
1485+
1486+
if ($leftMin !== null && $leftMin >= 0) {
1487+
$rangeMin = 0;
1488+
} elseif ($maxMagnitude === null) {
1489+
$rangeMin = $leftMin;
1490+
} elseif ($leftMin === null) {
1491+
$rangeMin = -$maxMagnitude;
1492+
} else {
1493+
$rangeMin = max($leftMin, -$maxMagnitude);
1494+
}
1495+
1496+
return IntegerRangeType::fromInterval($rangeMin, $rangeMax);
1497+
}
1498+
1499+
/**
1500+
* Highest possible absolute value of `$x % $divisor`, which is one less than the largest
1501+
* possible absolute value of the divisor. Null when there is no such bound.
1502+
*
1503+
* A divisor reaching PHP_INT_MIN is reported as unbounded too: `abs(PHP_INT_MIN)` does not
1504+
* fit into an integer, and the bound it stands for is PHP_INT_MAX, which is all an unbounded
1505+
* result can hold anyway.
1506+
*/
1507+
private static function getMaxModuloMagnitude(?int $divisorMin, ?int $divisorMax): ?int
1508+
{
1509+
if ($divisorMin === null || $divisorMax === null || $divisorMin === PHP_INT_MIN) {
1510+
return null;
1511+
}
1512+
1513+
return max(abs($divisorMin), abs($divisorMax)) - 1;
1514+
}
1515+
1516+
/**
1517+
* @return array{int|null, int|null}|null Lowest and highest value the type can hold, with null
1518+
* for an unbounded side. Null when the type is not built from integer ranges and constants.
1519+
*/
1520+
private function getIntegerBounds(Type $type): ?array
1521+
{
1522+
$innerTypes = $type instanceof UnionType ? $type->getTypes() : [$type];
1523+
if ($innerTypes === []) {
1524+
return null;
1525+
}
1526+
1527+
$min = null;
1528+
$max = null;
1529+
$unboundedMin = false;
1530+
$unboundedMax = false;
1531+
foreach ($innerTypes as $innerType) {
1532+
if ($innerType instanceof IntegerRangeType) {
1533+
$innerMin = $innerType->getMin();
1534+
$innerMax = $innerType->getMax();
1535+
} elseif ($innerType instanceof ConstantIntegerType) {
1536+
$innerMin = $innerType->getValue();
1537+
$innerMax = $innerMin;
1538+
} else {
1539+
return null;
14831540
}
14841541

1485-
return IntegerRangeType::fromInterval($rangeMin, $rangeMax);
1486-
} elseif ($positiveInt->isSuperTypeOf($leftType)->yes()) {
1487-
return IntegerRangeType::fromInterval(0, null);
1542+
if ($innerMin === null) {
1543+
$unboundedMin = true;
1544+
} elseif ($min === null || $innerMin < $min) {
1545+
$min = $innerMin;
1546+
}
1547+
1548+
if ($innerMax === null) {
1549+
$unboundedMax = true;
1550+
} elseif ($max === null || $innerMax > $max) {
1551+
$max = $innerMax;
1552+
}
14881553
}
14891554

1490-
return new IntegerType();
1555+
return [
1556+
$unboundedMin ? null : $min,
1557+
$unboundedMax ? null : $max,
1558+
];
14911559
}
14921560

14931561
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15242;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param int<0, 30> $nonNegative
9+
* @param int<-30, 30> $any
10+
* @param int<-10, -5> $negativeRange
11+
* @param int<-20, 5> $mixedSignRange
12+
*/
13+
function foo(int $nonNegative, int $any, int $negativeRange, int $mixedSignRange): void
14+
{
15+
assertType('int<0, 8>', $nonNegative % 9);
16+
assertType('int<0, 8>', $nonNegative % -9);
17+
assertType('int<-8, 8>', $any % -9);
18+
assertType('int<0, 9>', $nonNegative % $negativeRange);
19+
assertType('int<0, 19>', $nonNegative % $mixedSignRange);
20+
}
21+
22+
/**
23+
* @param int<0, 10> $i
24+
*/
25+
function phpIntMinDivisor(int $i): void
26+
{
27+
assertType('int<0, 10>', $i % (-9223372036854775807 - 1));
28+
assertType('int<0, 10>', $i % PHP_INT_MIN);
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15243;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param int<0, max> $a
9+
* @param int<10, 20> $range
10+
* @param int<1, 5>|int<10, 20> $ranges
11+
* @param int<30, max>|7 $unboundedOrSeven
12+
* @param int<-20, -10>|int<10, 15> $mixedSign
13+
* @param int<-20, -10>|int<-5, -3> $negative
14+
*/
15+
function foo(int $a, int $b, int $range, int $ranges, int $unboundedOrSeven, int $mixedSign, int $negative): void
16+
{
17+
assertType('int<0, 19>', $a % $range);
18+
assertType('int<0, 19>', $a % $ranges);
19+
assertType('int<-19, 19>', $b % $ranges);
20+
assertType('int<0, max>', $a % $unboundedOrSeven);
21+
assertType('int<0, 19>', $a % $mixedSign);
22+
assertType('int<0, 19>', $a % $negative);
23+
}
24+
25+
/**
26+
* @param int<30, max>|7 $divisor
27+
*/
28+
function bar(int $divisor): bool
29+
{
30+
return 100 % $divisor > 10; // e.g. 100 % 60 === 40
31+
}
32+
33+
/**
34+
* @param int<0, max> $a
35+
* @param int<min, 0> $nonPositive
36+
* @param int<1, 5>|int<10, 20> $ranges
37+
* @param int<min, 20> $unboundedMin
38+
*/
39+
function analogous(int $a, int $nonPositive, int $ranges, int $unboundedMin): void
40+
{
41+
assertType('int<min, 0>', $nonPositive % $a);
42+
assertType('int<-19, 0>', $nonPositive % $ranges);
43+
assertType('int<0, max>', $a % $unboundedMin);
44+
45+
$c = $a;
46+
$c %= $ranges;
47+
assertType('int<0, 19>', $c);
48+
}

‎tests/PHPStan/Analyser/nsrt/integer-range-types.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function maximaInversion($rMin, $rMax) {
314314
assertType('-1|1|float', -1 / $rMin);
315315
assertType('float', -2 / $rMax);
316316

317-
assertType('float|int<-5, max>', $rMin / -1);
317+
assertType('int<-5, max>', $rMin / -1);
318318
assertType('float|int<min, -2>', $rMax / -2);
319319
}
320320

‎tests/PHPStan/Analyser/nsrt/modulo-operator.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function doBar(int $i, int $j, $p, $range, $zeroOrMore, $intConst, $unionRange,
3434
assertType('int', $i % $unionRange);
3535
assertType('int<0, max>', $p % $unionRange);
3636

37-
assertType('int<-6, 6>', $i % $hybridUnionRange);
38-
assertType('int<0, 6>', $p % $hybridUnionRange);
37+
assertType('int', $i % $hybridUnionRange);
38+
assertType('int<0, max>', $p % $hybridUnionRange);
3939

4040
assertType('int<0, max>', $zeroOrMore % $mixed);
4141

‎tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,9 @@ public function testBug12167(): void
335335
$this->analyse([__DIR__ . '/data/bug-12167.php'], []);
336336
}
337337

338+
public function testBug15243(): void
339+
{
340+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-15243.php'], []);
341+
}
342+
338343
}

0 commit comments

Comments
 (0)