Skip to content

Commit 3c21170

Browse files
phpstan-botclaude
andcommitted
Convert setExistingOffsetValueType unit tests to NSRT tests
Replace unit tests in ArrayTypeTest with an NSRT test in bug-13637.php that covers the maybe-array item type scenario (union with non-array skips recursive path) alongside the existing deep nesting tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1c9704b commit 3c21170

2 files changed

Lines changed: 18 additions & 80 deletions

File tree

tests/PHPStan/Analyser/nsrt/bug-13637.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ function sixLevelsDeep() : array {
9595
return $final;
9696
}
9797

98+
/** Tests that maybe-array item type (union with non-array) skips the recursive path */
99+
function maybeArrayItemType(bool $flag): void {
100+
$final = [];
101+
102+
for ($i = 0; $i < 5; $i++) {
103+
$j = $i * 2;
104+
$k = $j + 1;
105+
if ($flag) {
106+
$final[$i][$j][$k]['abc'] = $i;
107+
$final[$i][$j][$k]['def'] = $i;
108+
} else {
109+
$final[$i] = $i;
110+
}
111+
}
112+
113+
assertType("non-empty-array<int<0, 4>, non-empty-array<int<0, 8>, non-empty-array<int<1, 9>, array{abc: int<0, 4>, def: int<0, 4>}>>|int<0, 4>>", $final);
114+
}
115+
98116
/**
99117
* @return array<int, array<int, array{abc: int, def: int, ghi: int}>>
100118
*/

tests/PHPStan/Type/ArrayTypeTest.php

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -301,86 +301,6 @@ 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-
384304
public static function dataSpliceArray(): array
385305
{
386306
return [

0 commit comments

Comments
 (0)