Fix phpstan/phpstan#13705: Adding elements to empty array in while loop does not result in array being recognized as potentially non-empty for in_array#5290
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…sons
When `count($arr) < $rangeVar` was used as a while loop condition (or
similar), the TypeSpecifier would incorrectly narrow the array to
`array{}` inside the loop body. This happened because
`specifyTypesForCountFuncCall` treated an IntegerRangeType sizeType
in falsey context as "count NOT in range", which is stricter than
the actual semantic "count < some value within the range".
Skip specifyTypesForCountFuncCall entirely when context is falsey
and the left operand is an IntegerRangeType, since the correct
narrowing cannot be expressed with the current sizeType abstraction.
Update test expectations in bug-4700 and bug11480 which relied on
the previous unsound over-narrowing behavior.
Fixes phpstan/phpstan#13705
gnutix
pushed a commit
to gnutix/phpstan-src
that referenced
this pull request
Mar 25, 2026
…an#5059) Co-authored-by: Claude Opus 4.6 <[email protected]>
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.
This PR fixes phpstan/phpstan#13705.
Summary
When
count($array) < $variable(where$variablehas a range type likeint<1, 42>) was used as a while loop condition, PHPStan incorrectly narrowed the array type toarray{}inside the loop body. This caused false positives within_array()and similar functions that depend on the array being potentially non-empty.Root cause
The condition
count($codes) < $quantityis internally transformed to!($quantity <= count($codes)). TheSmallerOrEqualhandler inTypeSpecifiercomputes asizeTypefrom the left operand and passes it tospecifyTypesForCountFuncCall. In the falsey context (negated byBooleanNot), this function interpretssizeType = int<1, 42>as "count is NOT in int<1,42>", concluding count must be 0. But the actual semantic is "count < $quantity where $quantity is somewhere in int<1,42>" — count could easily be non-zero (e.g., count=3 when $quantity=5).Fix
Skip
specifyTypesForCountFuncCallwhen the context is falsey AND the left operand is anIntegerRangeType. The sound narrowing for this case cannot be expressed with the current sizeType abstraction, so no narrowing is better than unsound narrowing.Two existing test files (
bug-4700.php,bug11480.php) had expectations that relied on the previous unsound over-narrowing and have been updated.Test plan
tests/PHPStan/Analyser/nsrt/bug-13705.phpcovering while loop, do-while loop, and simple while loop casesbug-4700.phpandbug11480.php