Fix phpstan/phpstan#14305: Internal error crash report#5231
Merged
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom Mar 17, 2026
Merged
Fix phpstan/phpstan#14305: Internal error crash report#5231ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
Conversation
…nteger keys from union offsets - Fixed nextAutoIndexes not being updated when adding optional integer keys from scalar type decomposition in setOffsetValueType - The crash occurred when array_search() result (string|false) was used as an array offset, and false->toArrayKey() produced ConstantIntegerType(0) that wasn't tracked in nextAutoIndexes - Added regression test in tests/PHPStan/Rules/Arrays/data/bug-14305.php - Updated baseline count for varTag.nativeType in ConstantArrayTypeBuilder.php Closes phpstan/phpstan#14305
cf64b20 to
f9b8c83
Compare
staabm
reviewed
Mar 17, 2026
Comment on lines
+295
to
+297
| if (!($scalarType instanceof ConstantIntegerType)) { | ||
| continue; | ||
| } |
Contributor
There was a problem hiding this comment.
I guess we need to use toArrayKey, as it also fails with "1", see https://phpstan.org/r/037d4dd3-a079-4e52-a184-e5fb050c97a3
wdyt @VincentLanglet ?
Collaborator
Author
There was a problem hiding this comment.
I processed this review but have nothing to report.
Contributor
There was a problem hiding this comment.
hmm the origin reproducer causes a exception here:
I feel this fix is wonky
Collaborator
Author
There was a problem hiding this comment.
I processed this review but have nothing to report.
Contributor
There was a problem hiding this comment.
Scalar types should be already casted because of
$scalarTypes = $offsetType->toArrayKey()->getConstantScalarTypes();
Collaborator
Author
There was a problem hiding this comment.
I processed this review but have nothing to report.
Closed
szepeviktor
reviewed
Mar 17, 2026
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
Fixes an internal error (crash) when using
array_search()result as an array offset in a foreach loop. The crash occurred at level 5 but not level 10 becauseRuleLevelHelperwithcheckUnionTypes=falsedecomposes union types and tests individual members againstsetOffsetValueType.Changes
src/Type/Constant/ConstantArrayTypeBuilder.php: Updated the scalar type decomposition branch (around line 290) to also updatenextAutoIndexeswhen addingConstantIntegerTypekeys as optional entries. Previously, integer keys were added without updating the auto-index tracker, causing aShouldNotHappenExceptionwhen those keys were later matched in a non-optionalsetOffsetValueTypecall.tests/PHPStan/Rules/Arrays/data/bug-14305.php: New test data file reproducing the issuetests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php: NewtestBug14305methodRoot cause
In
ConstantArrayTypeBuilder::setOffsetValueType(), when the offset type is a union that gets decomposed into scalar types (line 243+), and none of those scalar types match existing keys, the code at line 289 adds them as optional keys. However, it did not updatenextAutoIndexesfor anyConstantIntegerTypeentries. This meant aConstantIntegerType(0)could be added as a key whilenextAutoIndexesremained[0]. Later, whensetOffsetValueTypewas called again withConstantIntegerType(0)(matching the existing key), the code at line 183 filterednextAutoIndexesfor values > 0, found an empty array, and threwShouldNotHappenException.Test
The regression test reproduces the exact scenario from the issue:
define()d constants used witharray_search()whose result is used as an array offset in a foreach loop. The test verifies no internal error occurs.Fixes phpstan/phpstan#14305