Skip to content

Commit a668ade

Browse files
SanderMullerclaude
authored andcommitted
Use the callback return type when the number of arrays is not known
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>
1 parent 16cfec9 commit a668ade

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎src/Type/Php/ArrayMapFunctionReturnTypeExtension.php‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5252
array_map(static fn (Node\Arg $arg) => $arg->unpack, $arrayArgs),
5353
);
5454
if ($restType !== null) {
55-
// the number of arrays is not known
56-
return null;
55+
return self::getTypeForUnknownArrayCount($scope, $args[0]->value, [
56+
...$arrayTypes,
57+
$restType,
58+
]);
5759
}
5860

5961
$args = [$args[0], ...array_map(static fn (Type $arrayType) => new Node\Arg(new TypeExpr($arrayType)), $arrayTypes)];
@@ -184,6 +186,43 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
184186
return $mappedArrayType;
185187
}
186188

189+
/**
190+
* The number of arrays is not known, so the values have the return type of the callback.
191+
* One array keeps its keys, more arrays give a list.
192+
*
193+
* @param list<Type> $arrayTypes
194+
*/
195+
private static function getTypeForUnknownArrayCount(Scope $scope, Node\Expr $callback, array $arrayTypes): ?Type
196+
{
197+
$callableType = $scope->getType($callback);
198+
if (!$callableType->isCallable()->yes()) {
199+
return null;
200+
}
201+
202+
$returnTypes = [];
203+
foreach ($callableType->getCallableParametersAcceptors($scope) as $parametersAcceptor) {
204+
$returnTypes[] = $parametersAcceptor->getReturnType();
205+
}
206+
$valueType = TypeCombinator::union(...$returnTypes);
207+
208+
$keyTypes = [IntegerRangeType::createAllGreaterThanOrEqualTo(0)];
209+
$isList = true;
210+
foreach ($arrayTypes as $arrayType) {
211+
if ($arrayType->isList()->yes()) {
212+
continue;
213+
}
214+
215+
$isList = false;
216+
$keyTypes[] = $arrayType->getIterableKeyType();
217+
}
218+
219+
if ($isList) {
220+
return TypeCombinator::intersect(new ArrayType($keyTypes[0], $valueType), new AccessoryArrayListType());
221+
}
222+
223+
return new ArrayType(TypeCombinator::union(...$keyTypes), $valueType);
224+
}
225+
187226
private static function resolveCallbackReturnType(Scope $scope, Node\Expr $callback, Type $argType): Type
188227
{
189228
if ($callback instanceof Node\Expr\Closure || $callback instanceof Node\Expr\ArrowFunction) {

‎tests/PHPStan/Analyser/nsrt/array-map-unpacked-args.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,23 @@ public function knownThenUnknown(array $lists, array $tuple): void
7070
assertNativeType('mixed', $b);
7171
}, ...$tuple);
7272

73-
assertType('array', array_map(fn ($a, $b) => $b, [1, 2], ...$lists));
73+
assertType('list<int>', array_map(fn ($a, $b) => $b, [1, 2], ...$lists));
7474
}
7575

7676
/**
7777
* @param array{list<string>, list<string>} $tuple
7878
* @param list<list<int>> $lists
79+
* @param list<array<string, int>> $maps
7980
*/
80-
public function returnType(array $tuple, array $lists): void
81+
public function returnType(array $tuple, array $lists, array $maps): void
8182
{
8283
assertType('non-empty-list<4|5|6>', array_map(fn ($a, $b) => $b, [1, 2, 3], ...[[4, 5, 6]]));
8384
assertType('array{1, 2}', array_map(fn ($a) => $a, ...[[1, 2]]));
8485
assertType('non-empty-list<array{1|2, 3|4}>', array_map(null, ...[[1, 2], [3, 4]]));
8586
assertType('list<string>', array_map(fn ($a, $b) => $a . $b, ...$tuple));
86-
assertType('array', array_map(fn ($a) => $a, ...$lists));
87+
assertType('list<int>', array_map(fn ($a) => $a, ...$lists));
88+
assertType('list<int>', array_map(fn (int $a, int $b) => $a + $b, ...$lists));
89+
assertType('array<int<0, max>|string, int>', array_map(fn (int $a) => $a, ...$maps));
8790
}
8891

8992
public function shorterUnpackedArray(): void

0 commit comments

Comments
 (0)