Skip to content

Expand unpacked args into one value per array item - #7038

Open
SanderMuller wants to merge 8 commits into
phpstan:2.3.xfrom
SanderMuller:unpacked-array-map-args
Open

SanderMuller wants to merge 8 commits into
phpstan:2.3.xfrom
SanderMuller:unpacked-array-map-args

Conversation

@SanderMuller

@SanderMuller SanderMuller commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes phpstan/phpstan#15620

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, and unpacking a general array reported a false argument.type error.

The new UnpackedArgumentTypes::expand() turns the arg types into one type per position:

  • A list shape gives one type per required item. With a union of shapes, each position gets the union of the members, up to the smallest number of required items. The items after that go into a rest type.
  • A general array, or a shape that is not a list, gives a rest type, because the number of values is not known.

These places use it:

  • ContextualClosureParameterResolver, for array_map() callbacks and immediately invoked closures. A rest type becomes a variadic parameter. A closure parameter with a default after the known positions gets mixed, because it may get no value. That is also what it gets when an arg is left out without unpacking.
  • ParametersAcceptorSelector and ArrayMapParameterClosureTypeExtension. With a rest type, the callback keeps the type from the signature.
  • ArrayMapFunctionReturnTypeExtension. 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.

array_map() now adds null for the shorter arrays of an unpacked arg. #6874 does the same for arrays that are not unpacked, but it skipped unpacked args. With a rest type, the arrays after the known ones are compared with the others as one more array.

ContextualClosureParameterResolver and ParametersAcceptorSelector have Turbo twins. Both C++ twins call the PHP helper.

Reproducer from the issue, at level 8:

Line Before After
9 array{4, 5, 6} 4|5|6
13 array{1, 2} 1
16 non-empty-list<array{4, 5, 6}> non-empty-list<4|5|6>
18 Parameter #1 $callback of function array_map expects (callable(): mixed)|null, Closure(int, int): int given. no error

🤖 Generated with Claude Code

SanderMuller and others added 3 commits November 22, 2026 12:25
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 array_map() returns its declared type. 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>
array_map() with an unpacked array of unknown size returned its declared
type, array. 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>
@staabm
staabm force-pushed the unpacked-array-map-args branch from 56a4cb5 to a668ade Compare November 22, 2026 11:25
@staabm
staabm requested a balanced review from Copilot November 22, 2026 11:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unpacked-argument inference still drops unsealed shape values, misses array_map null padding and generic callback resolution, and excludes valid defaults for possibly omitted closure parameters.

4 open issues
What changed in this PR

This PR improves PHPStan’s inference for unpacked arguments in array_map() and immediately invoked closures, addressing incorrect callback types and false argument errors reported in issue #15620.

Changes:

  • Adds shared expansion of unpacked arguments into known positions and an optional rest type.
  • Updates callback and return-type inference, including null padding when the array count is known.
  • Mirrors resolver changes in Turbo and adds inference and rule regression tests.
File Description
turbo-ext/​src/​support.h Adds the unpacked-argument helper identifier.
turbo-ext/​src/​support.cpp Registers the PHP helper for Turbo calls.
turbo-ext/​src/​ParametersAcceptorSelector.cpp Mirrors callback parameter expansion.
turbo-ext/​src/​generated/​ParametersAcceptorSelector.h Updates generated method signatures.
turbo-ext/​src/​generated/​ContextualClosureParameterResolver.h Adds the intrinsic-parameter helper signature.
turbo-ext/​src/​ContextualClosureParameterResolver.cpp Mirrors intrinsic argument expansion and rest parameters.
tests/​PHPStan/​Rules/​Functions/​data/​array-map-unpacked-args.php Adds unpacked callback validation cases.
tests/​PHPStan/​Rules/​Functions/​CallToFunctionParametersRuleTest.php Registers expected callback diagnostics.
tests/​PHPStan/​Analyser/​nsrt/​array-map-unpacked-args.php Tests callback, return, and immediate-closure inference.
src/​Type/​Php/​ArrayMapParameterClosureTypeExtension.php Expands unpacked arrays for callback signatures.
src/​Type/​Php/​ArrayMapFunctionReturnTypeExtension.php Handles known and unknown expanded array counts.
src/​Reflection/​UnpackedArgumentTypes.php Introduces shared positional and rest-type expansion.
src/​Reflection/​ParametersAcceptorSelector.php Updates callback validation to use expanded arguments.
src/​Analyser/​ExprHandler/​Helper/​ContextualClosureParameterResolver.php Infers closure parameters from expanded argument types.

Review effort: Balanced

Comment on lines +130 to +132
// with a rest, the number of arrays is not known
if ($restType === null) {
$types = ArrayMapCallbackParameterTypes::addPaddingNull($types, $argTypes);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b9f23cb. The arrays after the known positions are now compared with the others as one more array. So $a is 1|null here, and the return type is list<1|null>. The C++ twin calls the same PHP helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 5de1fa8, each possible number of arrays is mapped on its own here, so the return type is now non-empty-list<1|null>. The parameter type is still 1|null.

foreach ($nativeTypes as $nativeType) {
$nativeParameters[] = new DummyParameter('item', $nativeType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
if ($restType !== null) {
$parameters[] = new DummyParameter('item', $isArrayMap ? $restType->getIterableValueType() : $restType, optional: true, passedByReference: PassedByReference::createNo(), variadic: true, defaultValue: null);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 69220fc. A parameter with a default after the known positions now gets mixed, so $b is mixed here. That is what it already gets when an arg is left out without unpacking. The C++ twin does the same.

Comment on lines +83 to +84
$valueTypes = $constantArray->getValueTypes();
$valueTypesCount = count($valueTypes);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not make this give a wrong type. With list{array{1}, ...<array{2}>}, fn ($a, $b = 'none') => $b gives array{mixed}, which is wide but correct. The arguments.count error for array_map($cb, ...$arrays) also happens on 2.3.x without this PR.

Comment on lines +203 to +204
foreach ($callableType->getCallableParametersAcceptors($scope) as $parametersAcceptor) {
$returnTypes[] = $parametersAcceptor->getReturnType();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bbe09e3. With an unknown number of arrays, the callback is now called with the rest arrays unpacked, as on the known-count path. array_map(identity(...), ...$lists) with list<list<int>> now gives list<int>.

SanderMuller and others added 5 commits November 23, 2026 16:30
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>
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>
…P version

On PHP 7.4 a variadic parameter has no named args, so its type is a list
there and not on PHP 8. The assertion tests no part of the change, so
remove it.

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>
$constantArrays = $argType->getConstantArrays();
$shapesAreLists = true;
foreach ($constantArrays as $constantArray) {
if ($constantArray->isList()->yes()) {

@staabm staabm Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line seems to miss testing (per mutation testing)

 			$constantArrays = $argType->getConstantArrays();
 			$shapesAreLists = true;
 			foreach ($constantArrays as $constantArray) {
-				if ($constantArray->isList()->yes()) {
+				if (!$constantArray->isList()->no()) {
 					continue;
 				}

@staabm

staabm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

this PR feels like doing a lot of changes.. is there some refactoring possible?

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.

Unpacked args to array_map() and immediately invoked closures are typed as one value

3 participants