Track by-reference value modifications in constant array foreach unrolling#5549
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Track by-reference value modifications in constant array foreach unrolling#5549phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…lling - Enable `tryProcessUnrolledConstantArrayForeach` to handle by-ref foreach by removing the early `$stmt->byRef` bail-out and propagating value variable changes back to the array after each unrolled iteration via `setExistingOffsetValueType` - Remove `isConstantArray()->no()` guard in `MutatingScope::enterForeach` so constant arrays also get `IntertwinedVariableByReferenceWithExpr` tracking as a fallback for arrays exceeding the unroll limit - Add regression tests covering: constant array by-ref cast, by-ref with key variable, shaped array by-ref, list type preservation, conditional modification, compound value replacement, and for-loop baseline
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 (array shape) with
foreach ($arr as &$value), modifications to$valuewere not propagated back to the array type. After the loop, the array retained its original type as if no modifications had occurred. This caused false positives when assigning the modified array to a typed property or returning it from a typed method.Changes
src/Analyser/NodeScopeResolver.php: Removed theif ($stmt->byRef) return null;bail-out intryProcessUnrolledConstantArrayForeach(). After each unrolled iteration's body is processed, the new value of the by-reference variable is read from the result scope and written back to the array type at the specific constant key usingsetExistingOffsetValueType(). This gives per-element precision — e.g.,[1, 2, 3]after casting each element to string becomesarray{'1', '2', '3'}.src/Analyser/MutatingScope.php: Removed the$iterateeType->isConstantArray()->no()guard from bothIntertwinedVariableByReferenceWithExprsetups inenterForeach(). This serves as a fallback for constant arrays that exceed the unroll limit (>16 elements), where the general loop path's by-ref tracking viaSetExistingOffsetValueTypeExprnow also applies to constant arrays.Root cause
Two guards excluded constant arrays from by-reference tracking in foreach loops:
tryProcessUnrolledConstantArrayForeach()returnednullwhen$stmt->byRefwas true, disabling the precise per-element unrolled processingMutatingScope::enterForeach()required$iterateeType->isConstantArray()->no()for theIntertwinedVariableByReferenceWithExprsetupTogether these meant constant arrays fell through to the general loop path without any by-ref tracking at all, so the array type was never updated.
Analogous cases probed:
array<int, array{a: string}>) — already worked correctly before this fixSetExistingOffsetValueTypeExprint|stringunion which is the correct conservative approximationTest
Added
tests/PHPStan/Analyser/nsrt/bug-1311.phpwith regression tests covering:array{a: int, b: int}) foreach by-refFixes phpstan/phpstan#1311