-
Notifications
You must be signed in to change notification settings - Fork 571
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
#5419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VincentLanglet
merged 6 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-njt1inu
Apr 7, 2026
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd08780
Fix count comparison with IntegerRangeType narrowing array too aggres…
VincentLanglet 9e186b6
Add tests for all IntegerRangeType count comparison conditions
phpstan-bot 1b549b9
Add exact original issue code as regression test
phpstan-bot 638c010
Add rule test for bug-13705 to verify no false positive in_array errors
phpstan-bot 66ffa81
Fix IntegerRangeType count narrowing for falsey context with unbounde…
phpstan-bot e05edaa
Reuse nsrt/bug-13705.php in rule test instead of duplicating it
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13705; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function whileLoop(): void | ||
| { | ||
| $quantity = random_int(1, 42); | ||
| $codes = []; | ||
| while (count($codes) < $quantity) { | ||
| assertType('list<non-empty-string>', $codes); | ||
| $code = random_bytes(16); | ||
| if (!in_array($code, $codes, true)) { | ||
| $codes[] = $code; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function whileLoopOriginal(int $length, int $quantity): void | ||
| { | ||
| if ($length < 8) { | ||
| throw new \InvalidArgumentException(); | ||
| } | ||
| $codes = []; | ||
| while ($quantity >= 1 && count($codes) < $quantity) { | ||
| $code = ''; | ||
| for ($i = 0; $i < $length; $i++) { | ||
| $code .= 'x'; | ||
| } | ||
| if (!in_array($code, $codes, true)) { | ||
| $codes[] = $code; | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the newly added bug-13705.php should also be checked against a rule test which verifies we will no longer get
"Call to function in_array() with arguments literal-string&non-falsy-string, array{} and true will always evaluate to false." and similar errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed both commits. Here's a summary of the changes:
Commit 1: Add rule test for bug-13705
tests/PHPStan/Rules/Comparison/data/bug-13705.phpwith three test cases (simple while loop, compound condition with$quantity >= 1 &&, and the original issue's class method)testBug13705()toImpossibleCheckTypeFunctionCallRuleTestexpecting no errorsCommit 2: Fix IntegerRangeType count narrowing for falsey context with unbounded max
$quantityisint<1, max>(unbounded max, e.g. after$quantity >= 1narrowing), the falsey context fell through to the min-based branch, producingsizeType = int<1, max>which incorrectly narrowed arrays toarray{}elseif ($leftType->getMin() !== null)toelseif ($context->truthy() && $leftType->getMin() !== null)— the min-based branch should only apply in truthy context$sizeTypeis left asnulland no count narrowing occurs (correct behavior since we can't determine a definite threshold)bug-4700.phptest expectations which were previously overly precise (narrowing toarray{}when the range was unbounded)