Skip to content

Type array_map() with an unknown number of unpacked arrays - #7140

Open
SanderMuller wants to merge 11 commits into
phpstan:2.3.xfrom
SanderMuller:unpacked-array-map-return-type
Open

SanderMuller wants to merge 11 commits into
phpstan:2.3.xfrom
SanderMuller:unpacked-array-map-return-type

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

Based on #7038, which it needs. Only the last 4 commits are new. I split them out of #7038 to keep that PR smaller.

An unpacked arg can make the number of arrays unknown. Then array_map() typed the callback call with one array per unpacked arg, not with the values in the arrays. This PR gives ArrayMapFunctionReturnTypeExtension a type for that case. A rest type is what UnpackedArgumentTypes::expand() from #7038 gives for the values whose position is not known.

  • When every unpacked arg is a union of list shapes, each possible list of arrays is mapped as a known number of arrays. The results are unioned. A list with fewer arrays than a closure written in the call requires is skipped, because PHP throws an ArgumentCountError for it. With a rest type, the callback is called with the arrays after the known ones unpacked, so a generic callback resolves its template types. The result is a list when every array is a list.
  • With a null callback and a rest type, array_map() gives a list of lists with one value per array. PHP returns one array as it is. For general arrays the type system cannot tell that case apart, so the type assumes more than one array. The values get null as for a known number of arrays whose sizes are not all the same.
  • An unsealed shape is mapped as a general array, because its extra items do not give a known number of arrays.

🤖 Generated with Claude Code

@SanderMuller
SanderMuller force-pushed the unpacked-array-map-return-type branch 2 times, most recently from e6c8fbf to 9651bf4 Compare November 27, 2026 13:44
SanderMuller and others added 11 commits November 28, 2026 01:27
An unpacked arg passes one value per item of its array. array_map() and
immediately invoked closures typed it as one value, so the callback
parameters and the return type of array_map() were wrong. Unpacking a
general array also reported a false argument.type error.

When the number of values is not known, the callback keeps the type from
the signature, and the return type of array_map() does not change. When
it is known, array_map() adds null for the shorter arrays, as it does for
arrays that are not unpacked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An array shape whose first key is optional might not be a list, so its
values are not known by position. Only a list is expanded into one
value per position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When an unpacked arg makes the number of arrays unknown, array_map()
still passes null for an array that is shorter than another one. Compare
the arrays after the known positions with the others as one more array,
so that the callback parameters get null as they do for a known number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A closure parameter after the known positions of the unpacked args may
get no value, and then has its default. It got only the type of the
values after the known positions. Give such a parameter the mixed type,
as when an arg is left out without unpacking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An unsealed list shape, such as array{array{1}, ...<int, array{2}>}, can
have items after its keys. When it is unpacked, the number of values is
not known. Before, the extra items were dropped, so the closure
parameter of array_map() over it was 1. Now the shape gives a rest type,
as a general array does.

The unit test also covers a shape that is only maybe a list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
expand() merges each position across the shapes of an unpacked union,
so addPaddingNull() missed an array that is shorter than another one in
the same shape. addPaddingNullToArgs() pads each shape on its own and
unions the results. The arrays after the known positions are different
arrays, so their smallest size counts. With more than one unpacked
union, every position gets null.

The closure resolver treated a named arg as the next positional arg.
It now types the parameter of that name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When an unpacked arg makes the number of arrays unknown, array_map()
typed the callback call with one array per unpacked arg, not with the
values in the arrays. Now the values have the return type of the
callback. One array keeps its keys, and more arrays give a list. So the
result is a list when every array is a list, and otherwise it also has
the keys of the arrays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The return type of array_map() with an unknown number of arrays read the
return type from the callback signature, so a generic callback kept its
template type. Call the callback with the value types of the arrays, and
with the arrays after the known ones unpacked, as the path for a known
number of arrays does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For an unpacked union of list shapes, map each list of arrays the shapes
can pass as a known number of arrays, and union the results. Skip a list
with fewer arrays than a closure written in the call requires, because
PHP then throws an ArgumentCountError. Before, a null callback with an
unknown number of arrays fell back to the array return type of the
function signature.

For general arrays the number of arrays is not known. With a null
callback, assume more than one array, so each value is a list with one
item per array. One array is returned as it is, but the type system
cannot tell that case apart. Add null to the items as for a known number
of arrays whose sizes are not all the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An unsealed shape can have items after its keys, so it does not give a
known number of arrays. Map it with the arrays after the known ones
unpacked, as for a general array. Before, the extra items were dropped,
so array_map() over array{array{1}, ...<int, array{2}>} gave array{1}.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SanderMuller
SanderMuller force-pushed the unpacked-array-map-return-type branch from 9651bf4 to eef531b Compare November 28, 2026 00:34
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.

1 participant