diff --git a/src/Rules/Functions/RandomIntParametersRule.php b/src/Rules/Functions/RandomIntParametersRule.php index 1205283002..12e5b008a7 100644 --- a/src/Rules/Functions/RandomIntParametersRule.php +++ b/src/Rules/Functions/RandomIntParametersRule.php @@ -65,6 +65,15 @@ public function processNode(Node $node, Scope $scope): array $isSmaller = $maxType->isSmallerThan($minType, $this->phpVersion); + if ($isSmaller->maybe() && $this->reportMaybes) { + if ( + $minType instanceof IntegerRangeType && ($minType->getMin() === null || $minType->getMax() === null) + || $maxType instanceof IntegerRangeType && ($maxType->getMin() === null || $maxType->getMax() === null) + ) { + return []; + } + } + if ($isSmaller->yes() || $isSmaller->maybe() && $this->reportMaybes) { $message = 'Parameter #1 $min (%s) of function random_int expects lower number than parameter #2 $max (%s).'; return [ diff --git a/tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php b/tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php index 39160f054f..20829c5ce2 100644 --- a/tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/RandomIntParametersRuleTest.php @@ -74,4 +74,14 @@ public function testBug6361(): void $this->analyse([__DIR__ . '/data/bug-6361.php'], []); } + public function testBug14468(): void + { + $this->analyse([__DIR__ . '/data/bug-14468.php'], [ + [ + 'Parameter #1 $min (int<5, max>) of function random_int expects lower number than parameter #2 $max (int).', + 57, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-14468.php b/tests/PHPStan/Rules/Functions/data/bug-14468.php new file mode 100644 index 0000000000..d0b0b0c672 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-14468.php @@ -0,0 +1,65 @@ + $a + * @param int<0, max> $b + */ +function bothUnboundedMax(int $a, int $b): void +{ + random_int($a, $b); +} + +/** + * @param int $a + * @param int $b + */ +function bothUnboundedMin(int $a, int $b): void +{ + random_int($a, $b); +} + +/** + * @param int<0, max> $min + * @param int<0, 10> $max + */ +function unboundedMinBoundedMax(int $min, int $max): void +{ + random_int($min, $max); +} + +/** + * @param int<0, 10> $min + * @param int $max + */ +function boundedMinUnboundedMax(int $min, int $max): void +{ + random_int($min, $max); +} + +/** + * @param int<5, max> $min + * @param int $max + */ +function unboundedButDefinitelyWrong(int $min, int $max): void +{ + random_int($min, $max); // error - max <= 3 < 5 <= min +} + +/** @param positive-int $positiveInt */ +function positiveInt(int $int, int $positiveInt): void +{ + random_int($int, $int); + random_int($positiveInt, $positiveInt); +}