Do not report maybe in RandomIntParametersRule when either argument is an unbounded IntegerRangeType#5622
Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Open
Conversation
…nt is an unbounded `IntegerRangeType` - 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<min, 0>`, 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<min, 3>)` where max <= 3 < 5 <= min). - No analogous rules exist — `RandomIntParametersRule` is the only rule using `isSmallerThan`.
staabm
approved these changes
May 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RandomIntParametersRulewas reporting false-positivemaybeerrors forrandom_int()calls where one or both arguments had unbounded integer range types (e.g.int<0, max>). This happened because the rule already skips plainint(IntegerType) but treated unboundedIntegerRangeTypevalues differently, even though they carry the same level of uncertainty about the relationship between min and max.Changes
src/Rules/Functions/RandomIntParametersRule.phpto suppressmaybereports when either the min or max argument type is anIntegerRangeTypewith an unbounded end (null min or null max). Definite (yes) errors are still reported even for unbounded ranges.tests/PHPStan/Rules/Functions/data/bug-14468.phpcovering:random_int()inside awhile ($min < $max)loop withstrlen-based maxint<0, max>)int<min, 0>)int<5, max>vsint<min, 3>) — still correctly reportedRoot cause
The rule checks
$maxType->isSmallerThan($minType)and reports when the result ismaybe(withreportMaybesenabled). For unbounded ranges likeint<0, max>,isSmallerThantrivially returnsmaybebecause unbounded ranges overlap with almost everything. The rule already had a bail-out for plainint(IntegerType) at lines 59-63, butIntegerRangeTypewith one null bound was not covered by that bail-out.The fix extends the
maybesuppression to also cover unboundedIntegerRangeType, making it consistent with the existingIntegerTypehandling.Searched for analogous rules using
isSmallerThan—RandomIntParametersRuleis the only consumer, so no other rules are affected.Test
testBug14468inRandomIntParametersRuleTest— verifies no errors for unbounded range combinations and still reports errors for definitely-wrong unbounded combinations.testFile,testBug6361) continue to pass unchanged.Fixes phpstan/phpstan#14468