Fix phpstan/phpstan#12601: Array keys cannot be narrowed to non-empty-string when used in templates#5314
Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Open
Conversation
- Stop generalizing non-constant scalar subtypes (non-empty-string, int<0, max>) when inferring template types bounded by array-key - The previous logic generalized ALL scalars for array-key bounds, but only constant values need generalization - Updated existing test expectations in generics-do-not-generalize.php to reflect correct behavior - New regression test in tests/PHPStan/Analyser/nsrt/bug-12601.php
staabm
reviewed
Mar 29, 2026
Contributor
staabm
left a comment
There was a problem hiding this comment.
Add a test for https://phpstan.org/r/c448c867-97fd-4e89-966b-974f59dacf5f
Adds a test case from phpstan.org/r/c448c867 verifying that when a list<string> is passed to new ArrayIterator(), the inferred key type is int<0, max> (not generalized to int). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Already handled — |
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 an
array<non-empty-string, non-empty-string>was passed to a generic constructor likenew ArrayIterator(...), the key typenon-empty-stringwas incorrectly widened tostring. Similarly,int<0, max>keys were widened toint. This fix preserves non-constant scalar subtypes during template type inference.Changes
src/Type/Generic/TemplateTypeHelper.php: Removed the overly broad first condition ingeneralizeInferredTemplateType()that generalized all scalar types when the template bound wasarray-key. The remaining condition already handles constant values correctly.tests/PHPStan/Analyser/nsrt/generics-do-not-generalize.php: Changed expected types fromArrayIterator<int, string>toArrayIterator<int<0, max>, string>since list keys (int<0, max>) should no longer be generalized.tests/PHPStan/Analyser/nsrt/bug-12601.php: Regression test covering bothnon-empty-stringandint<0, max>key types withArrayIterator.Root cause
In
TemplateTypeHelper::generalizeInferredTemplateType(), there were two conditions for generalizing inferred template types. The first condition ($type->isScalar()->yes() && $isArrayKey) was too broad — it generalized ALL scalar types when the template bound wasarray-key(int|string). This caused non-constant scalar subtypes likenon-empty-stringandint<0, max>to be generalized tostringandintrespectively. The fix removes this condition, leaving only the second condition that correctly targets constant values for generalization.Test
Added
tests/PHPStan/Analyser/nsrt/bug-12601.phpwith two test classes implementingIteratorAggregate:array<non-empty-string, non-empty-string>keys — verifiesArrayIteratorpreservesnon-empty-stringkey typearray<int<0, max>, non-empty-string>keys — verifiesArrayIteratorpreservesint<0, max>key typeFixes phpstan/phpstan#12601