-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#14324: Foreach on constant array with closures reaching 32 entries causes crash #5246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 3 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-dfdlnal
Mar 19, 2026
+71
−0
Merged
Fix phpstan/phpstan#14324: Foreach on constant array with closures reaching 32 entries causes crash #5246
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14324; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| final class Test | ||
| { | ||
| private const ADDITIONAL_MAPS = [ | ||
| 'foo-', | ||
| 'bar-', | ||
| 'baz-', | ||
| ]; | ||
|
|
||
| /** @var array<string, callable(): string> */ | ||
| private static array $map = []; | ||
|
|
||
| public function createMap(): void | ||
| { | ||
| if ([] === self::$map) { | ||
| // 29 entries | ||
| self::$map = [ | ||
| 'foo' => static fn() => 'foo', | ||
| 'bar' => static fn() => 'bar', | ||
| 'baz' => static fn() => 'baz', | ||
| 'qux' => static fn() => 'qux', | ||
| 'quux' => static fn() => 'quux', | ||
| 'corge' => static fn() => 'corge', | ||
| 'grault' => static fn() => 'grault', | ||
| 'garply' => static fn() => 'garply', | ||
| 'waldo' => static fn() => 'waldo', | ||
| 'fred' => static fn() => 'fred', | ||
| 'plugh' => static fn() => 'plugh', | ||
| 'xyzzy' => static fn() => 'xyzzy', | ||
| 'thud' => static fn() => 'thud', | ||
| 'foo1' => static fn() => 'foo1', | ||
| 'bar1' => static fn() => 'bar1', | ||
| 'baz1' => static fn() => 'baz1', | ||
| 'qux1' => static fn() => 'qux1', | ||
| 'quux1' => static fn() => 'quux1', | ||
| 'corge1' => static fn() => 'corge1', | ||
| 'grault1' => static fn() => 'grault1', | ||
| 'garply1' => static fn() => 'garply1', | ||
| 'waldo1' => static fn() => 'waldo1', | ||
| 'fred1' => static fn() => 'fred1', | ||
| 'plugh1' => static fn() => 'plugh1', | ||
| 'xyzzy1' => static fn() => 'xyzzy1', | ||
| 'thud1' => static fn() => 'thud1', | ||
| 'foo2' => static fn() => 'foo2', | ||
| 'bar2' => static fn() => 'bar2', | ||
| 'baz2' => static fn() => 'baz2', | ||
| ]; | ||
|
|
||
| foreach (self::ADDITIONAL_MAPS as $map) { | ||
| // added with 3 entries, breaching the closure limit of 32 entries | ||
| self::$map[$map] = fn () => self::$map['foo'](); | ||
| } | ||
|
|
||
| assertType("non-empty-array<'bar'|'bar-'|'bar1'|'bar2'|'baz'|'baz-'|'baz1'|'baz2'|'corge'|'corge1'|'foo'|'foo-'|'foo1'|'foo2'|'fred'|'fred1'|'garply'|'garply1'|'grault'|'grault1'|'plugh'|'plugh1'|'quux'|'quux1'|'qux'|'qux1'|'thud'|'thud1'|'waldo'|'waldo1'|'xyzzy'|'xyzzy1', callable(): mixed>&oversized-array", self::$map); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have similar code in the codebase which asserts the result of a
getArray-method call on aConstantArrayTypeBuilderobject ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both tests and static analysis pass.
The reviewer's question was informational — asking whether there are similar patterns elsewhere in the codebase. The answer is no: the
getAllArrays()assertion atConstantArrayType.php:250is the only place that assertsgetArray()returns aConstantArrayType. No code changes are needed beyond what's already in the PR. There's nothing to commit or push.