Skip to content

Commit a5b645d

Browse files
SanderMullerclaude
andcommitted
Pass the extra items of an unsealed shape as a rest
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 array_map() over it gave array{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>
1 parent 5de1fa8 commit a5b645d

4 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎src/Reflection/UnpackedArgumentTypes.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection;
44

55
use PHPStan\Turbo\ReferencedByTurboExtension;
6+
use PHPStan\Type\Constant\ConstantArrayType;
67
use PHPStan\Type\Type;
78
use PHPStan\Type\TypeCombinator;
89
use function count;
@@ -50,7 +51,7 @@ public static function expand(array $argTypes, array $unpacked): array
5051
$constantArrays = $argType->getConstantArrays();
5152
$shapesAreLists = true;
5253
foreach ($constantArrays as $constantArray) {
53-
if ($constantArray->isList()->yes()) {
54+
if ($constantArray->isList()->yes() && !self::hasExtraItems($constantArray)) {
5455
continue;
5556
}
5657

@@ -91,4 +92,13 @@ public static function expand(array $argTypes, array $unpacked): array
9192
return [$types, $restTypes === [] ? null : TypeCombinator::union(...$restTypes)];
9293
}
9394

95+
/**
96+
* Whether an unsealed shape can have items after its keys, so the values it
97+
* passes when unpacked come in a number that is not known.
98+
*/
99+
public static function hasExtraItems(ConstantArrayType $array): bool
100+
{
101+
return $array->isUnsealed()->yes();
102+
}
103+
94104
}

‎src/Type/Php/ArrayMapFunctionReturnTypeExtension.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ private static function getUnpackedArrayLists(array $argTypes, array $unpacked):
110110

111111
$choices = [];
112112
foreach ($constantArrays as $constantArray) {
113+
if (UnpackedArgumentTypes::hasExtraItems($constantArray)) {
114+
return null;
115+
}
113116
foreach ($constantArray->getAllArrays() as $array) {
114117
if (!self::hasPositionKeys($array)) {
115118
continue;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ public function immediatelyInvoked(array $ints, array $maybeList): void
223223
(fn ($a, $b) => assertType('2', $b))(1, ...[2]);
224224
}
225225

226+
/**
227+
* @param array{array{1}, ...<int, array{2}>} $unsealed
228+
* @param array{array{1}, array{2}, ...<int, array{3}>} $unsealedPair
229+
*/
230+
public function unsealedShapes(array $unsealed, array $unsealedPair): void
231+
{
232+
array_map(function ($a, ...$rest) {
233+
assertType('1|2', $a);
234+
}, ...$unsealed);
235+
236+
assertType('list<1|2>', array_map(fn ($a) => $a, ...$unsealed));
237+
assertType('list<non-empty-list<1|2|3>>', array_map(null, ...$unsealedPair));
238+
239+
(function ($a) {
240+
assertType('array{1}|array{2}', $a);
241+
})(...$unsealed);
242+
}
243+
226244
}
227245

228246
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
use PHPStan\Testing\PHPStanTestCase;
6+
use PHPStan\TrinaryLogic;
7+
use PHPStan\Type\Constant\ConstantArrayType;
8+
use PHPStan\Type\Constant\ConstantIntegerType;
9+
use PHPStan\Type\IntegerType;
10+
use PHPStan\Type\NeverType;
11+
use PHPStan\Type\Type;
12+
use PHPStan\Type\VerbosityLevel;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use function array_map;
15+
16+
class UnpackedArgumentTypesTest extends PHPStanTestCase
17+
{
18+
19+
/**
20+
* @return iterable<string, array{ConstantArrayType, list<string>, string|null}>
21+
*/
22+
public static function dataExpand(): iterable
23+
{
24+
$keyTypes = [new ConstantIntegerType(0), new ConstantIntegerType(1)];
25+
$valueTypes = [new ConstantIntegerType(1), new ConstantIntegerType(2)];
26+
$never = new NeverType(true);
27+
28+
yield 'list' => [
29+
new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes()),
30+
['1', '2'],
31+
null,
32+
];
33+
34+
yield 'sealed list' => [
35+
new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes(), unsealed: [$never, $never]),
36+
['1', '2'],
37+
null,
38+
];
39+
40+
yield 'maybe a list' => [
41+
new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createMaybe()),
42+
[],
43+
'1|2',
44+
];
45+
46+
yield 'unsealed list' => [
47+
new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes(), unsealed: [new IntegerType(), new ConstantIntegerType(3)]),
48+
[],
49+
'1|2|3',
50+
];
51+
}
52+
53+
/**
54+
* @param list<string> $expectedTypes
55+
*/
56+
#[DataProvider('dataExpand')]
57+
public function testExpand(ConstantArrayType $array, array $expectedTypes, ?string $expectedRestType): void
58+
{
59+
[$types, $restType] = UnpackedArgumentTypes::expand([$array], [true]);
60+
61+
$this->assertSame($expectedTypes, array_map(static fn (Type $type) => $type->describe(VerbosityLevel::precise()), $types));
62+
$this->assertSame($expectedRestType, $restType?->describe(VerbosityLevel::precise()));
63+
}
64+
65+
}

0 commit comments

Comments
 (0)