Skip to content

Infer closure parameter types in array arguments - #6422

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
codex/array-closure-inference
Sep 11, 2026
Merged

Infer closure parameter types in array arguments#6422
ondrejmirtes merged 1 commit into
2.3.xfrom
codex/array-closure-inference

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Closures passed inside array arguments now inherit their parameter types from the expected array element type. For example, a closure inside an array<Closure(string, int): mixed> parameter sees string and int instead of mixed.

Pass the expected PHPDoc and native types through expression context to array elements and closure analysis. This also covers arrow functions, nested arrays, array shapes, nullable arrays, and iterable parameters while preserving explicit closure parameter types.

Added the original playground reproducers for both issues and additional inference coverage. All three regression fixtures fail without the source changes and pass with them restored. The full suite passes (21,799 tests, 67 skipped), as do make phpstan and coding style checks for the changed source files.

Closes phpstan/phpstan#6430
Closes phpstan/phpstan#11215

@phpstan-bot

Copy link
Copy Markdown
Collaborator

You've opened the pull request against the latest branch 2.3.x. PHPStan 2.3 is not going to be released for months. If your code is relevant on 2.2.x and you want it to be released sooner, please rebase your pull request and change its target to 2.2.x.

@ondrejmirtes
ondrejmirtes merged commit f0b92e1 into 2.3.x Sep 11, 2026
746 of 809 checks passed
@ondrejmirtes
ondrejmirtes deleted the codex/array-closure-inference branch September 11, 2026 20:24
@canvural

Copy link
Copy Markdown
Contributor

Thank you but there is one missing feature that my previous PR was handling.

https://phpstan.org/r/bc3d261f-c044-45a7-bec5-b21094c41561

My PR was dumping 'email'|'name' but now it's just string

Do you think this can be supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to resolve closure type inside an array callable and Closure template types can't be inferred if it's in array

3 participants