Fix phpstan/phpstan#14464: Narrow list types when count() result is stored in variable#5462
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14464: Narrow list types when count() result is stored in variable#5462phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…tored in variable When the result of count() or sizeof() on a list or constant array is assigned to a variable, create ConditionalExpressionHolders so that comparing that variable to specific integers narrows the array type to the corresponding fixed-size shape. Previously, `$n = count($list); if ($n === 3)` did not narrow $list, while the direct `if (count($list) === 3)` did. This was because AssignHandler only created holders for falsey scalar values, not for count-specific integer comparisons. The fix iterates over possible array sizes and creates holders that map each size to the narrowed array type produced by TypeSpecifier's existing specifyTypesForCountFuncCall logic.
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.
Fixes phpstan/phpstan#14464
When
count()orsizeof()is called on a list or constant array and the result is stored in a variable, comparing that variable to a specific integer now narrows the array type to the corresponding fixed-size tuple shape.How it works
AssignHandleralready createsConditionalExpressionHolderentries for falsey scalar values when a variable is assigned. This PR extends that mechanism: when the assigned expression iscount($array)orsizeof($array)and the argument is a list or constant array, it iterates over possible array sizes and creates holders that map each integer size to the narrowed array type. The narrowing itself reusesTypeSpecifier::specifyTypesForCountFuncCall, which already handles the directcount($list) === Ncase.Supported patterns:
$n = count($list); if ($n === 3)(strict equality)$n = count($list); if ($n == 3)(loose equality)$n = sizeof($list); if ($n === 2)(sizeof alias)list<T>, constant array unions, andpreg_splitresultsarray<string, int>) correctly remain non-narrowed since keys are unknown