Skip to content

Commit ba5d370

Browse files
SanderMullerclaude
authored andcommitted
Expand unpacked args into one value per array item
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>
1 parent 527d373 commit ba5d370

14 files changed

Lines changed: 527 additions & 199 deletions

‎src/Analyser/ExprHandler/Helper/ContextualClosureParameterResolver.php‎

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Reflection\ParameterReflection;
1919
use PHPStan\Reflection\PassedByReference;
2020
use PHPStan\Reflection\Php\DummyParameter;
21+
use PHPStan\Reflection\UnpackedArgumentTypes;
2122
use PHPStan\Turbo\ShadowedByTurboExtension;
2223
use PHPStan\Type\MixedType;
2324
use PHPStan\Type\Type;
@@ -26,6 +27,7 @@
2627
use function array_key_exists;
2728
use function array_map;
2829
use function count;
30+
use function in_array;
2931
use function max;
3032

3133
/**
@@ -93,40 +95,53 @@ public function resolve(
9395

9496
$types = [];
9597
$nativeTypes = [];
96-
$arrayTypes = [];
97-
$nativeArrayTypes = [];
98-
$hasUnpackedArg = false;
98+
$unpacked = [];
9999
foreach ($intrinsicArgs as $arg) {
100-
$hasUnpackedArg = $hasUnpackedArg || $arg->unpack;
101100
$result = $storage !== null ? $storage->findExpressionResult($arg->value) : null;
102-
$type = $result !== null ? $result->getType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope);
103-
$nativeType = $result !== null ? $result->getNativeType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope->doNotTreatPhpDocTypesAsCertain());
104-
if ($arrayMapArgs !== null) {
105-
$arrayTypes[] = $type;
106-
$nativeArrayTypes[] = $nativeType;
107-
$type = $type->getIterableValueType();
108-
$nativeType = $nativeType->getIterableValueType();
109-
}
110-
$types[] = $type;
111-
$nativeTypes[] = $nativeType;
101+
$types[] = $result !== null ? $result->getType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope);
102+
$nativeTypes[] = $result !== null ? $result->getNativeType() : $this->nodeScopeResolver->readScopeStateOrSyntheticType($arg->value, $scope->doNotTreatPhpDocTypesAsCertain());
103+
$unpacked[] = $arg->unpack;
104+
}
105+
106+
return new ClosureParameterTypes(
107+
$this->createIntrinsicParameters($types, $unpacked, $arrayMapArgs !== null),
108+
$this->createIntrinsicParameters($nativeTypes, $unpacked, $arrayMapArgs !== null),
109+
);
110+
}
111+
112+
/**
113+
* @param list<Type> $argTypes
114+
* @param list<bool> $unpacked
115+
* @return list<DummyParameter>
116+
*/
117+
private function createIntrinsicParameters(array $argTypes, array $unpacked, bool $isArrayMap): array
118+
{
119+
$restType = null;
120+
if (in_array(true, $unpacked, true)) {
121+
[$argTypes, $restType] = UnpackedArgumentTypes::expand($argTypes, $unpacked);
112122
}
113123

114-
// an unpacked arg is one parameter here, so its arrays cannot be told apart
115-
if ($arrayMapArgs !== null && !$hasUnpackedArg) {
116-
$types = ArrayMapCallbackParameterTypes::addPaddingNull($types, $arrayTypes);
117-
$nativeTypes = ArrayMapCallbackParameterTypes::addPaddingNull($nativeTypes, $nativeArrayTypes);
124+
$types = $argTypes;
125+
if ($isArrayMap) {
126+
$types = [];
127+
foreach ($argTypes as $argType) {
128+
$types[] = $argType->getIterableValueType();
129+
}
130+
// with a rest, the number of arrays is not known
131+
if ($restType === null) {
132+
$types = ArrayMapCallbackParameterTypes::addPaddingNull($types, $argTypes);
133+
}
118134
}
119135

120136
$parameters = [];
121137
foreach ($types as $type) {
122138
$parameters[] = new DummyParameter('item', $type, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
123139
}
124-
$nativeParameters = [];
125-
foreach ($nativeTypes as $nativeType) {
126-
$nativeParameters[] = new DummyParameter('item', $nativeType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
140+
if ($restType !== null) {
141+
$parameters[] = new DummyParameter('item', $isArrayMap ? $restType->getIterableValueType() : $restType, optional: true, passedByReference: PassedByReference::createNo(), variadic: true, defaultValue: null);
127142
}
128143

129-
return new ClosureParameterTypes($parameters, $nativeParameters);
144+
return $parameters;
130145
}
131146

132147
/**

‎src/Reflection/ParametersAcceptorSelector.php‎

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use function constant;
5555
use function count;
5656
use function defined;
57+
use function in_array;
5758
use function is_int;
5859
use function is_string;
5960
use function sprintf;
@@ -232,62 +233,29 @@ public static function applyIntrinsicArgOverrides(
232233
$positionalArgs = self::reorderIntrinsicArgs($args, $namedArgumentsVariants[0] ?? $parametersAcceptors[0]);
233234
$arrayMapArgs = $positionalArgs[0]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
234235
if ($arrayMapArgs !== null) {
235-
$callbackValueTypes = [];
236-
$callbackArrayTypes = [];
237-
$nativeCallbackValueTypes = [];
238-
$nativeCallbackArrayTypes = [];
236+
$argTypes = [];
237+
$nativeArgTypes = [];
238+
$unpacked = [];
239239
foreach ($arrayMapArgs as $arg) {
240-
$argType = ($typeGetter)($arg->value);
241-
$nativeArgType = ($nativeTypeGetter)($arg->value);
242-
if ($arg->unpack) {
243-
$constantArrays = $argType->getConstantArrays();
244-
if (count($constantArrays) > 0) {
245-
foreach ($constantArrays as $constantArray) {
246-
$valueTypes = $constantArray->getValueTypes();
247-
foreach ($valueTypes as $valueType) {
248-
$callbackValueTypes[] = ($iterableValueTypeGetter)($valueType);
249-
$callbackArrayTypes[] = $valueType;
250-
}
251-
}
252-
}
253-
$nativeConstantArrays = $nativeArgType->getConstantArrays();
254-
if (count($nativeConstantArrays) > 0) {
255-
foreach ($nativeConstantArrays as $constantArray) {
256-
$valueTypes = $constantArray->getValueTypes();
257-
foreach ($valueTypes as $valueType) {
258-
$nativeCallbackValueTypes[] = ($iterableValueTypeGetter)($valueType);
259-
$nativeCallbackArrayTypes[] = $valueType;
260-
}
261-
}
262-
}
263-
} else {
264-
$callbackValueTypes[] = ($iterableValueTypeGetter)($argType);
265-
$callbackArrayTypes[] = $argType;
266-
$nativeCallbackValueTypes[] = ($iterableValueTypeGetter)($nativeArgType);
267-
$nativeCallbackArrayTypes[] = $nativeArgType;
268-
}
269-
}
270-
271-
$callbackParameters = [];
272-
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($callbackValueTypes, $callbackArrayTypes) as $valueType) {
273-
$callbackParameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
274-
}
275-
$nativeCallbackParameters = [];
276-
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($nativeCallbackValueTypes, $nativeCallbackArrayTypes) as $valueType) {
277-
$nativeCallbackParameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
240+
$argTypes[] = ($typeGetter)($arg->value);
241+
$nativeArgTypes[] = ($nativeTypeGetter)($arg->value);
242+
$unpacked[] = $arg->unpack;
278243
}
244+
$callbackParameters = self::createArrayMapCallbackParameters($argTypes, $unpacked, $iterableValueTypeGetter);
245+
$nativeCallbackParameters = self::createArrayMapCallbackParameters($nativeArgTypes, $unpacked, $iterableValueTypeGetter);
279246

280247
$acceptor = $parametersAcceptors[0];
281248
$parameters = $acceptor->getParameters();
282-
if (isset($parameters[0])) {
249+
// with an unknown number of arrays, the callback keeps the type from the signature
250+
if (isset($parameters[0]) && $callbackParameters !== null) {
283251
$callableType = new UnionType([
284252
new CallableType($callbackParameters, new MixedType(), false),
285253
new NullType(),
286254
]);
287-
$nativeCallableType = new UnionType([
255+
$nativeCallableType = $nativeCallbackParameters !== null ? new UnionType([
288256
new CallableType($nativeCallbackParameters, new MixedType(), false),
289257
new NullType(),
290-
]);
258+
]) : self::wrapParameter($parameters[0])->getNativeType();
291259
$parameters[0] = self::overrideParameterType($parameters[0], $callableType, $nativeCallableType);
292260
$parametersAcceptors = [self::overrideAcceptorParameters($acceptor, $parameters)];
293261
}
@@ -1425,6 +1393,34 @@ private static function getCurlOptValueType(int $curlOpt): ?Type
14251393
return null;
14261394
}
14271395

1396+
/**
1397+
* @param list<Type> $argTypes
1398+
* @param list<bool> $unpacked
1399+
* @param Closure(Type): Type $iterableValueTypeGetter
1400+
* @return list<DummyParameter>|null null when the number of arrays is not known
1401+
*/
1402+
private static function createArrayMapCallbackParameters(array $argTypes, array $unpacked, Closure $iterableValueTypeGetter): ?array
1403+
{
1404+
if (in_array(true, $unpacked, true)) {
1405+
[$argTypes, $restType] = UnpackedArgumentTypes::expand($argTypes, $unpacked);
1406+
if ($restType !== null) {
1407+
return null;
1408+
}
1409+
}
1410+
1411+
$valueTypes = [];
1412+
foreach ($argTypes as $argType) {
1413+
$valueTypes[] = ($iterableValueTypeGetter)($argType);
1414+
}
1415+
1416+
$parameters = [];
1417+
foreach (ArrayMapCallbackParameterTypes::addPaddingNull($valueTypes, $argTypes) as $valueType) {
1418+
$parameters[] = new DummyParameter('item', $valueType, optional: false, passedByReference: PassedByReference::createNo(), variadic: false, defaultValue: null);
1419+
}
1420+
1421+
return $parameters;
1422+
}
1423+
14281424
private static function overrideParameterType(ParameterReflection $original, Type $type, Type $nativeType): ExtendedDummyParameter
14291425
{
14301426
$wrapped = self::wrapParameter($original);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
use PHPStan\Turbo\ReferencedByTurboExtension;
6+
use PHPStan\Type\Type;
7+
use PHPStan\Type\TypeCombinator;
8+
use function count;
9+
use function min;
10+
11+
/**
12+
* The types of the values a call passes, with each unpacked arg (`...$arg`)
13+
* expanded into one value per item of its array.
14+
*/
15+
#[ReferencedByTurboExtension(key: 'unpackedArgumentTypes')]
16+
final class UnpackedArgumentTypes
17+
{
18+
19+
/**
20+
* Returns the type of each value whose position is known, and the type of
21+
* the values after them when their number is not known. That is the case
22+
* after an unpacked arg that is not a list shape, or that is a list shape
23+
* of more than one size.
24+
*
25+
* @param list<Type> $argTypes
26+
* @param list<bool> $unpacked whether each arg is unpacked
27+
* @return array{list<Type>, Type|null}
28+
*/
29+
public static function expand(array $argTypes, array $unpacked): array
30+
{
31+
$types = [];
32+
$restTypes = [];
33+
foreach ($argTypes as $i => $argType) {
34+
if (!$unpacked[$i]) {
35+
if ($restTypes !== []) {
36+
// PHP does not allow a positional arg after an unpacked one
37+
$restTypes[] = $argType;
38+
continue;
39+
}
40+
41+
$types[] = $argType;
42+
continue;
43+
}
44+
45+
if ($restTypes !== []) {
46+
$restTypes[] = $argType->getIterableValueType();
47+
continue;
48+
}
49+
50+
$constantArrays = $argType->getConstantArrays();
51+
$shapesAreLists = true;
52+
foreach ($constantArrays as $constantArray) {
53+
if ($constantArray->isList()->yes()) {
54+
continue;
55+
}
56+
57+
// string keys are passed as named args
58+
$shapesAreLists = false;
59+
break;
60+
}
61+
62+
if (!$shapesAreLists || $constantArrays === []) {
63+
$restTypes[] = $argType->getIterableValueType();
64+
continue;
65+
}
66+
67+
$requiredCounts = [];
68+
foreach ($constantArrays as $constantArray) {
69+
// the optional keys of a list are its last keys
70+
$requiredCounts[] = count($constantArray->getValueTypes()) - count($constantArray->getOptionalKeys());
71+
}
72+
$requiredCount = min($requiredCounts);
73+
74+
for ($position = 0; $position < $requiredCount; $position++) {
75+
$positionTypes = [];
76+
foreach ($constantArrays as $constantArray) {
77+
$positionTypes[] = $constantArray->getValueTypes()[$position];
78+
}
79+
$types[] = TypeCombinator::union(...$positionTypes);
80+
}
81+
82+
foreach ($constantArrays as $constantArray) {
83+
$valueTypes = $constantArray->getValueTypes();
84+
$valueTypesCount = count($valueTypes);
85+
for ($position = $requiredCount; $position < $valueTypesCount; $position++) {
86+
$restTypes[] = $valueTypes[$position];
87+
}
88+
}
89+
}
90+
91+
return [$types, $restTypes === [] ? null : TypeCombinator::union(...$restTypes)];
92+
}
93+
94+
}

‎src/Type/Php/ArrayMapFunctionReturnTypeExtension.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Parser\ArrayMapArgVisitor;
1111
use PHPStan\Reflection\ArrayMapCallbackParameterTypes;
1212
use PHPStan\Reflection\FunctionReflection;
13+
use PHPStan\Reflection\UnpackedArgumentTypes;
1314
use PHPStan\TrinaryLogic;
1415
use PHPStan\Type\Accessory\AccessoryArrayListType;
1516
use PHPStan\Type\Accessory\AccessoryType;
@@ -26,11 +27,11 @@
2627
use PHPStan\Type\Type;
2728
use PHPStan\Type\TypeCombinator;
2829
use PHPStan\Type\TypeUtils;
29-
use function array_filter;
3030
use function array_map;
3131
use function array_reduce;
3232
use function array_slice;
3333
use function count;
34+
use function in_array;
3435

3536
#[AutowiredService]
3637
final class ArrayMapFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -44,6 +45,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
4445
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4546
{
4647
$args = $functionCall->getArgs();
48+
if (count($args) >= 2 && in_array(true, array_map(static fn (Node\Arg $arg) => $arg->unpack, $args), true)) {
49+
$arrayArgs = array_slice($args, 1);
50+
[$arrayTypes, $restType] = UnpackedArgumentTypes::expand(
51+
array_map(static fn (Node\Arg $arg) => $scope->getType($arg->value), $arrayArgs),
52+
array_map(static fn (Node\Arg $arg) => $arg->unpack, $arrayArgs),
53+
);
54+
if ($restType !== null) {
55+
// the number of arrays is not known
56+
return null;
57+
}
58+
59+
$args = [$args[0], ...array_map(static fn (Type $arrayType) => new Node\Arg(new TypeExpr($arrayType)), $arrayTypes)];
60+
}
61+
4762
$numArgs = count($args);
4863
if ($numArgs < 2) {
4964
return null;
@@ -58,9 +73,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5873
$arrayArgs = array_slice($args, 1);
5974
$arrayTypes = array_map(static fn (Node\Arg $arg) => $scope->getType($arg->value), $arrayArgs);
6075
$callbackValueTypes = array_map(static fn (Type $arrayType) => $arrayType->getIterableValueType(), $arrayTypes);
61-
if (count(array_filter($arrayArgs, static fn (Node\Arg $arg) => $arg->unpack)) === 0) {
62-
$callbackValueTypes = ArrayMapCallbackParameterTypes::addPaddingNull($callbackValueTypes, $arrayTypes);
63-
}
76+
$callbackValueTypes = ArrayMapCallbackParameterTypes::addPaddingNull($callbackValueTypes, $arrayTypes);
6477
$valueType = $scope->getType(new FuncCall(
6578
$callback,
6679
array_map(

0 commit comments

Comments
 (0)