Skip to content

[TypeDeclaration] Fix AddClosureParamTypeForArrayMapRector to type closure params from array values, not keys#8163

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-array-map-closure-value-types
Jul 9, 2026
Merged

[TypeDeclaration] Fix AddClosureParamTypeForArrayMapRector to type closure params from array values, not keys#8163
TomasVotruba merged 1 commit into
mainfrom
fix-array-map-closure-value-types

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

AddClosureParamTypeForArrayMapRector modeled array_map() as if it passed (value, key) pairs to the closure — like array_walk(). It doesn't. array_map() passes values only, positionally: with N array arguments, the i-th closure parameter receives values from the i-th array. Keys are never passed.

Because of this, the second closure parameter was typed from the merged key types of all arrays, producing wrong (and often union) types.

Before

/** @param array<string, string> $tokens */
array_map(function ($token, $value) {
    echo $token . $value;
}, array_keys($tokens), $tokens);

got typed as:

array_map(function (string $token, int|string $value) {

$value iterates $tokens values (string), yet it received int|string — the merged keys of array_keys($tokens) (int) and $tokens (string).

After

array_map(function (string $token, string $value) {

Each parameter is now typed from the value type of its matching array argument.

Change

  • $closure->params[$i] is typed from getIterableValueType() of the i-th array argument; key handling removed entirely.
  • Spread arguments (array_map($fn, ...$arrays)) bail out — positions can't be resolved statically (already covered by skip_tuple).
  • Existing fixtures updated to the corrected semantics; added array_keys_and_values fixture for the reported case.

@TomasVotruba TomasVotruba merged commit e1283b9 into main Jul 9, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-array-map-closure-value-types branch July 9, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant