From b432a7a89c6770c9d878e58f697a6f09ee3ecf65 Mon Sep 17 00:00:00 2001 From: staabm <120441+staabm@users.noreply.github.com> Date: Sat, 9 May 2026 14:49:24 +0000 Subject: [PATCH 1/2] Do not report `maybe` in `RandomIntParametersRule` when either argument is an unbounded `IntegerRangeType` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The rule already skips plain `int` (IntegerType) since the comparison is inherently uncertain, but it was reporting `maybe` errors for unbounded IntegerRangeType values like `int<0, max>` or `int`, which are equally uncertain. - When `isSmallerThan` returns `maybe`, now also skip if either argument's type is an IntegerRangeType with a null min or null max bound. - Definite (`yes`) errors with unbounded ranges are still reported correctly (e.g. `random_int(int<5, max>, int)` where max <= 3 < 5 <= min). - No analogous rules exist — `RandomIntParametersRule` is the only rule using `isSmallerThan`. --- .../Functions/RandomIntParametersRule.php | 9 +++ .../Functions/RandomIntParametersRuleTest.php | 10 ++++ .../Rules/Functions/data/bug-14468.php | 58 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-14468.php diff --git a/src/Rules/Functions/RandomIntParametersRule.php b/src/Rules/Functions/RandomIntParametersRule.php index 1205283002d..12e5b008a78 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 39160f054fc..20829c5ce22 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 00000000000..afa67fb095a --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-14468.php @@ -0,0 +1,58 @@ + $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 +} From 128b3eb0d35a74acfe36fc528b19379be0864516 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 May 2026 17:01:49 +0200 Subject: [PATCH 2/2] Update bug-14468.php --- tests/PHPStan/Rules/Functions/data/bug-14468.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/PHPStan/Rules/Functions/data/bug-14468.php b/tests/PHPStan/Rules/Functions/data/bug-14468.php index afa67fb095a..d0b0b0c6724 100644 --- a/tests/PHPStan/Rules/Functions/data/bug-14468.php +++ b/tests/PHPStan/Rules/Functions/data/bug-14468.php @@ -56,3 +56,10 @@ 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); +}