Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Fix phpstan/phpstan#14170: lost list in iterable type after iteration#5199

Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-5pophhv
Closed

Fix phpstan/phpstan#14170: lost list in iterable type after iteration#5199
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-5pophhv

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When iterating over an iterable<string, list<string>> and modifying elements via nested array dim fetch assignment ($convert[$outerKey][$key] = ...), the list type was lost and the variable's type degraded to array<string, mixed>|iterable<string, list<string>> instead of remaining iterable<string, list<string>>.

Changes

  • Override getOffsetValueType() on IterableType (src/Type/IterableType.php) to return the actual item type instead of mixed (from MaybeOffsetAccessibleTypeTrait)
  • Skip the generalOffsetAccessibleType intersection for iterable types in produceArrayDimFetchAssignValueToWrite (src/Analyser/ExprHandler/AssignHandler.php) to prevent decomposing iterable into array|ArrayAccess union
  • Update existing test expectation in tests/PHPStan/Analyser/nsrt/iterable.php to reflect the now-correct offset value type for typed iterables

Root cause

Two issues combined to cause the bug:

  1. IterableType::getOffsetValueType() inherited from MaybeOffsetAccessibleTypeTrait returned mixed instead of the item type. When processing nested array dim fetch assignments like $convert[$outerKey][$key], the intermediate value type at $convert[$outerKey] became mixed, losing the list<string> information.

  2. In produceArrayDimFetchAssignValueToWrite, the iterable type was intersected with generalOffsetAccessibleType (array|ArrayAccess|null), which decomposed the iterable compound type into separate array and ArrayAccess branches, permanently losing the iterable identity.

Test

Added regression test tests/PHPStan/Analyser/nsrt/bug-14170.php with two test cases:

  • example3a: Iterating with references (already worked correctly)
  • example3b: Iterating with explicit key access and array dim assignment (was failing, now passes)

Fixes phpstan/phpstan#14170

- Override getOffsetValueType() on IterableType to return the item type instead of mixed
- Skip generalOffsetAccessibleType intersection for iterable types in produceArrayDimFetchAssignValueToWrite to prevent decomposing iterable into array|ArrayAccess
- Update existing iterable test to reflect correct offset value type
- New regression test in tests/PHPStan/Analyser/nsrt/bug-14170.php
@staabm staabm closed this Mar 11, 2026
@staabm staabm deleted the create-pull-request/patch-5pophhv branch March 11, 2026 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants