Skip to content

Commit 61d9de9

Browse files
jlherrenondrejmirtes
authored andcommitted
Make IntegerRangeType represent open intervals properly
This removes the usage of PHP_INT_MIN and PHP_INT_MAX to mark open-ended intervals and instead uses null for that purpose. Since using getMin() and getMax() now became slightly more complicated for callers, a lot of IntegerRangeType related logic has been moved into the class itself.
1 parent 0af0399 commit 61d9de9

File tree

8 files changed

+315
-220
lines changed

8 files changed

+315
-220
lines changed

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,11 +1472,7 @@ private function resolveType(Expr $node): Type
14721472
}
14731473
return TypeCombinator::union(...$newTypes);
14741474
} elseif ($varType instanceof IntegerRangeType) {
1475-
$shift = $node instanceof Expr\PreInc ? +1 : -1;
1476-
return IntegerRangeType::fromInterval(
1477-
$varType->getMin() === PHP_INT_MIN ? PHP_INT_MIN : $varType->getMin() + $shift,
1478-
$varType->getMax() === PHP_INT_MAX ? PHP_INT_MAX : $varType->getMax() + $shift
1479-
);
1475+
return $varType->shift($node instanceof Expr\PreInc ? +1 : -1);
14801476
}
14811477

14821478
$stringType = new StringType();

src/Rules/Functions/RandomIntParametersRule.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use PHPStan\Type\Constant\ConstantIntegerType;
1111
use PHPStan\Type\IntegerRangeType;
12-
use PHPStan\Type\IntegerType;
1312
use PHPStan\Type\VerbosityLevel;
1413

1514
/**
@@ -45,35 +44,25 @@ public function processNode(Node $node, Scope $scope): array
4544

4645
$minType = $scope->getType($node->args[0]->value)->toInteger();
4746
$maxType = $scope->getType($node->args[1]->value)->toInteger();
48-
$integerType = new IntegerType();
4947

50-
if ($minType->equals($integerType) || $maxType->equals($integerType)) {
48+
if (
49+
!$minType instanceof ConstantIntegerType && !$minType instanceof IntegerRangeType
50+
|| !$maxType instanceof ConstantIntegerType && !$maxType instanceof IntegerRangeType
51+
) {
5152
return [];
5253
}
5354

54-
if ($minType instanceof ConstantIntegerType || $minType instanceof IntegerRangeType) {
55-
if ($minType instanceof ConstantIntegerType) {
56-
$maxPermittedType = IntegerRangeType::fromInterval($minType->getValue(), PHP_INT_MAX);
57-
} else {
58-
$maxPermittedType = IntegerRangeType::fromInterval($minType->getMax(), PHP_INT_MAX);
59-
}
60-
61-
if (!$maxPermittedType->isSuperTypeOf($maxType)->yes()) {
62-
$message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).';
63-
64-
// True if sometimes the parameters conflict.
65-
$isMaybe = !$maxType->isSuperTypeOf($minType)->no();
66-
67-
if (!$isMaybe || $this->reportMaybes) {
68-
return [
69-
RuleErrorBuilder::message(sprintf(
70-
$message,
71-
$minType->describe(VerbosityLevel::value()),
72-
$maxType->describe(VerbosityLevel::value())
73-
))->build(),
74-
];
75-
}
76-
}
55+
$isSmaller = $maxType->isSmallerThan($minType);
56+
57+
if ($isSmaller->yes() || $isSmaller->maybe() && $this->reportMaybes) {
58+
$message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).';
59+
return [
60+
RuleErrorBuilder::message(sprintf(
61+
$message,
62+
$minType->describe(VerbosityLevel::value()),
63+
$maxType->describe(VerbosityLevel::value())
64+
))->build(),
65+
];
7766
}
7867

7968
return [];

src/Type/Constant/ConstantIntegerType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
3737
}
3838

3939
if ($type instanceof IntegerRangeType) {
40-
if ($type->getMin() <= $this->value && $this->value <= $type->getMax()) {
40+
$min = $type->getMin();
41+
$max = $type->getMax();
42+
if (($min === null || $min <= $this->value) && ($max === null || $this->value <= $max)) {
4143
return TrinaryLogic::createMaybe();
4244
}
4345

0 commit comments

Comments
 (0)