Fix phpstan/phpstan#11533: Foreach to check the type of array is not taken into account#5318
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#5318phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
… narrowing - Added foreach unrolling for constant arrays in NodeScopeResolver: after the normal loop analysis, the body is re-processed once per element with specific constant values, accumulating type narrowing across iterations - Added MutatingScope::applyNarrowingsFromForeachUnroll() to selectively transfer only HasOffsetType/HasOffsetValueType accessory types from the unrolled scope to the final scope, avoiding interference with array mutations - 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 string array like
['need', 'field']in a foreach loop, type narrowing fromisset()andis_string()checks inside the loop body was not preserved after the loop. This meant PHPStan couldn't verify that the array had the expected shape after the loop completed.Changes
src/Analyser/NodeScopeResolver.php(after the existing foreach analysis): when the iterable is a small constant array that iterates at least once, the loop body is re-processed once per element with specific constant key/value types, accumulating type narrowingapplyNarrowingsFromForeachUnroll()method tosrc/Analyser/MutatingScope.php: selectively transfers onlyHasOffsetTypeandHasOffsetValueTypeaccessory types from the unrolled scope to the final scope, without affecting base array structure (avoiding interference with array mutations in the loop body)tests/PHPStan/Analyser/nsrt/bug-11533.phpRoot cause
PHPStan's loop analysis uses fixed-point iteration where the loop variable holds the union of all possible values (e.g.,
'need'|'field'). Whenisset($param[$field])is evaluated with this union-typed key, the TypeSpecifier can only produce aNonEmptyArrayTypenarrowing (not per-keyHasOffsetType), because narrowing for all union members would be unsound in non-loop contexts. Similarly,is_string($param[$field])narrows the expression$param[$field]but can't decompose this into per-keyHasOffsetValueTypeconstraints on$param.The fix adds a post-loop unrolling pass specifically for constant arrays: the loop body is re-analyzed once per element with the specific constant value, so
isset($param['need'])andis_string($param['need'])produce the precise per-key narrowing. Only accessory types (HasOffsetType/HasOffsetValueType) are transferred from the unrolled scope to the final scope, ensuring array mutations in the loop body are unaffected.Test
The regression test verifies that after
foreach (['need', 'field'] as $field) { if (!isset($param[$field]) || !is_string($param[$field])) throw; }, the type of$paramis correctly narrowed tonon-empty-array<mixed>&hasOffsetValue('field', string)&hasOffsetValue('need', string). Tests with and without a key variable are included, as well as a function call test matching the original issue.Fixes phpstan/phpstan#11533