Fix #8636: Oversimplified huge const array#4975
Closed
phpstan-bot wants to merge 2 commits into2.1.xfrom
Closed
Conversation
- Changed OversizedArrayBuilder to use GeneralizePrecision::lessSpecific() instead of moreSpecific() when generalizing key and value types - This produces simpler types like `string` instead of `literal-string&lowercase-string&non-falsy-string` for oversized arrays - New regression test in tests/PHPStan/Analyser/nsrt/bug-8636.php - Updated expected types in OversizedArrayBuilderTest and ReturnTypeRuleTest - The root cause was that moreSpecific generalization preserved accessory types that made oversized array types overly complex without adding value
Automated fix attempt 1 for CI failures.
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 class constants or other array expressions contain more than 256 entries, PHPStan's
OversizedArrayBuilderwas usingGeneralizePrecision::moreSpecific()to generalize individual key and value types. This produced overly complex types likeliteral-string&lowercase-string&non-falsy-stringinstead of simplystring, making the oversized array types unnecessarily precise while still losing per-key shape information.This fix changes the generalization to use
GeneralizePrecision::lessSpecific(), producing cleaner and more practical types (e.g.,non-empty-array<string, string>&oversized-arrayinstead ofnon-empty-array<literal-string&lowercase-string&non-falsy-string, literal-string&lowercase-string&non-falsy-string>&oversized-array).Changes
GeneralizePrecision::moreSpecific()toGeneralizePrecision::lessSpecific()insrc/Type/Constant/OversizedArrayBuilder.php(lines 84, 88)tests/PHPStan/Type/Constant/OversizedArrayBuilderTest.phpfor the 4 test cases that hadliteral-string&...patternstests/PHPStan/Rules/Methods/ReturnTypeRuleTest.phpfor thetestBug8146bErrorstesttests/PHPStan/Analyser/nsrt/bug-8636.phpwith two test classes covering string-keyed and int-keyed oversized arraysRoot cause
The
OversizedArrayBuilderusedGeneralizePrecision::moreSpecific()which converts constant string values like'foo'intoliteral-string&lowercase-string&non-falsy-string(preserving accessory types). While this is more precise thanstring, it adds unnecessary complexity to oversized array types that have already lost their per-key shape information. UsinglessSpecific()instead produces simplestringandinttypes, which are more practical for downstream type checking and better match user expectations for generalized array types.Test
Added
tests/PHPStan/Analyser/nsrt/bug-8636.phpwith:Configclass with a 258-entry string-keyed constant array, testing that the oversized type isnon-empty-array<string, string>&oversized-arrayand that key access returnsstringMixedConfigclass with a 258-entry int-keyed constant array mixing string and int values, testing that the oversized type isnon-empty-list<int|string>&oversized-arrayFixes phpstan/phpstan#8636