Fix phpstan/phpstan#14037: array_splice resets int keys of the input array#5139
Merged
VincentLanglet merged 6 commits intophpstan:2.1.xfrom Mar 8, 2026
Merged
Conversation
- Generalize specific integer key types (e.g. 10|20|30) to int in ArrayType::spliceArray() - Uses TypeTraverser to only affect integer parts of union key types, preserving string keys - New regression test in tests/PHPStan/Analyser/nsrt/bug-14037.php - The root cause was that ArrayType::spliceArray() preserved the original key type verbatim, but PHP's array_splice re-indexes integer keys to 0, 1, 2, ...
staabm
reviewed
Mar 8, 2026
Comment on lines
+390
to
+391
| /** @var array<string, string> $arr */ | ||
| $arr; |
Contributor
There was a problem hiding this comment.
I would use a @param type, so we don't unnecessarily promoted inline-phpdocs
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param array<10|20|30|'a', mixed> $a |
Contributor
There was a problem hiding this comment.
also add a test for integer-range
staabm
approved these changes
Mar 8, 2026
uekann
pushed a commit
to uekann/phpstan-src
that referenced
this pull request
Mar 9, 2026
…t array (phpstan#5139) Co-authored-by: VincentLanglet <[email protected]> Co-authored-by: Vincent Langlet <[email protected]>
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
array_spliceresets integer keys of the input array to sequential 0, 1, 2, ... while preserving string keys. PHPStan correctly handled this for constant arrays but not for general array types likearray<10|20|30|'a', mixed>, which should becomearray<'a'|int, mixed>after splicing.Changes
ArrayType::spliceArray()insrc/Type/ArrayType.phpto generalize integer key types tointusingTypeTraverser, sincearray_splicere-indexes integer keystests/PHPStan/Analyser/nsrt/bug-14037.phpRoot cause
ArrayType::spliceArray()used$this->getIterableKeyType()directly as the key type for the resulting array. This preserved specific integer constant types (like10|20|30) verbatim, but PHP'sarray_splicere-indexes all integer keys to sequential integers starting from 0. The fix usesTypeTraverserto walk the key type and replace any integer type with a genericIntegerType, while leaving string key types unchanged.Test
The regression test verifies that
array<10|20|30|'a', mixed>becomesarray<'a'|int, mixed>afterarray_splice($a, 0, 0).Fixes phpstan/phpstan#14037
Closes #4844