|
99 | 99 | use PHPStan\Type\UnionType; |
100 | 100 | use stdClass; |
101 | 101 | use Throwable; |
| 102 | +use function abs; |
102 | 103 | use function array_key_exists; |
103 | 104 | use function array_keys; |
104 | 105 | use function array_map; |
|
122 | 123 | use function str_starts_with; |
123 | 124 | use function strtolower; |
124 | 125 | use const INF; |
| 126 | +use const PHP_INT_MIN; |
125 | 127 |
|
126 | 128 | #[AutowiredService] |
127 | 129 | final class InitializerExprTypeResolver |
@@ -1453,41 +1455,107 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback): |
1453 | 1455 | } |
1454 | 1456 | } |
1455 | 1457 |
|
1456 | | - $positiveInt = IntegerRangeType::fromInterval(0, null); |
| 1458 | + $maxMagnitude = null; |
1457 | 1459 | 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]); |
1477 | 1463 | } |
| 1464 | + } |
1478 | 1465 |
|
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; |
1483 | 1540 | } |
1484 | 1541 |
|
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 | + } |
1488 | 1553 | } |
1489 | 1554 |
|
1490 | | - return new IntegerType(); |
| 1555 | + return [ |
| 1556 | + $unboundedMin ? null : $min, |
| 1557 | + $unboundedMax ? null : $max, |
| 1558 | + ]; |
1491 | 1559 | } |
1492 | 1560 |
|
1493 | 1561 | /** |
|
0 commit comments