Skip to content

Commit 1c9704b

Browse files
phpstan-botclaude
andcommitted
Add unit tests for setExistingOffsetValueType with maybe-array item type
Tests that when the item type is a union of array and non-array (isArray() returns maybe), the recursive elseif branch is correctly skipped and the fallback TypeCombinator::union path is used instead. Also tests the definite array case where the recursive path produces correct merged results. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 263c20a commit 1c9704b

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

tests/PHPStan/Type/ArrayTypeTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,86 @@ public function testHasOffsetValueType(
301301
);
302302
}
303303

304+
public static function dataSetExistingOffsetValueType(): array
305+
{
306+
return [
307+
'maybe array item type falls through to union' => [
308+
// array<int, int|array<int, array<int, array{abc: int}>>>
309+
new ArrayType(
310+
new IntegerType(),
311+
new UnionType([
312+
new IntegerType(),
313+
new ArrayType(
314+
new IntegerType(),
315+
new ArrayType(
316+
new IntegerType(),
317+
new ConstantArrayType(
318+
[new ConstantStringType('abc')],
319+
[new IntegerType()],
320+
),
321+
),
322+
),
323+
]),
324+
),
325+
new IntegerType(),
326+
new ArrayType(
327+
new IntegerType(),
328+
new ArrayType(
329+
new IntegerType(),
330+
new ConstantArrayType(
331+
[new ConstantStringType('def')],
332+
[new IntegerType()],
333+
),
334+
),
335+
),
336+
'array<int, array<int, array<int, array{abc: int}|array{def: int}>>|int>',
337+
],
338+
'definite array item type uses recursive path' => [
339+
// array<int, array<int, array<int, array{abc: int}>>>
340+
new ArrayType(
341+
new IntegerType(),
342+
new ArrayType(
343+
new IntegerType(),
344+
new ArrayType(
345+
new IntegerType(),
346+
new ConstantArrayType(
347+
[new ConstantStringType('abc')],
348+
[new IntegerType()],
349+
),
350+
),
351+
),
352+
),
353+
new IntegerType(),
354+
new ArrayType(
355+
new IntegerType(),
356+
new ArrayType(
357+
new IntegerType(),
358+
new ConstantArrayType(
359+
[new ConstantStringType('def')],
360+
[new IntegerType()],
361+
),
362+
),
363+
),
364+
'array<int, array<int, array<int, array{abc: int, def: int}>>>',
365+
],
366+
];
367+
}
368+
369+
#[DataProvider('dataSetExistingOffsetValueType')]
370+
public function testSetExistingOffsetValueType(
371+
ArrayType $type,
372+
Type $offsetType,
373+
Type $valueType,
374+
string $expectedType,
375+
): void
376+
{
377+
$actualResult = $type->setExistingOffsetValueType($offsetType, $valueType);
378+
$this->assertSame(
379+
$expectedType,
380+
$actualResult->describe(VerbosityLevel::precise()),
381+
);
382+
}
383+
304384
public static function dataSpliceArray(): array
305385
{
306386
return [

0 commit comments

Comments
 (0)