Fix phpstan/phpstan#11533: Foreach to check the type of array is not taken into account#5272
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#11533: Foreach to check the type of array is not taken into account#5272phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- After foreach over constant string arrays like ['need', 'field'], propagate per-element type narrowings (HasOffsetValueType) to variables narrowed inside the loop body - Added logic in NodeScopeResolver post-foreach processing to detect variables whose dim-fetch expressions were narrowed (e.g. via isset + is_string) and apply HasOffsetValueType for each constant array element - Guards against false positives by checking the variable wasn't modified (assigned) in the body and that the dim-fetch narrowing is new (not pre-existing) - New regression test in tests/PHPStan/Analyser/nsrt/bug-11533.php
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
When iterating over a constant array of strings (e.g.
['need', 'field']) in a foreach loop and narrowing an array variable's offsets inside the body (viaisset+is_stringchecks with throw), PHPStan did not propagate the per-element type narrowings to the variable after the loop. This caused false positiveargument.typeerrors when passing the array to functions expecting specific key types.Changes
src/Analyser/NodeScopeResolver.phpthat detects when a foreach iterates over a constant array of string literals and the body narrowed array dim-fetch expressions using the loop variableHasOffsetValueTypeaccessories for each constant string value in the iterated arrayRoot cause
When PHPStan processes a foreach body, the loop variable (e.g.
$field) has a union type of all iterated values (e.g.'need'|'field'). Type narrowing from conditions likeis_string($param[$field])creates expression types for$param[$field]but does not propagateHasOffsetValueTypeto$parambecause the dim type is a union (not a singleConstantStringType). After the loop, this narrowing information was lost.The fix adds a post-foreach processing step that recognizes when a constant string array was iterated and the loop body narrowed dim-fetch expressions using the loop variable, then applies per-element
HasOffsetValueTypeaccessories to restore the narrowing information.Test
Added regression test in
tests/PHPStan/Analyser/nsrt/bug-11533.phpcovering:isset+is_stringnarrowing and throw (the reported issue)array_key_existsinstead ofissetFixes phpstan/phpstan#11533