Skip to content

Commit dc0c8b3

Browse files
phpstan-botclaude
andcommitted
Add test cases with array<Foo|Bar> annotation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9655055 commit dc0c8b3

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,48 @@ public function doitIf(array $items): void
5454
}, $items);
5555
}
5656
}
57+
58+
/**
59+
* @param array<Foo|Bar> $items
60+
*/
61+
public function doitMatchArrayUnion(array $items): void
62+
{
63+
if ([] === $items) {return; }
64+
65+
$first = reset($items);
66+
match (true) {
67+
$first instanceOf Foo => array_map(function ($i) {
68+
assertType('Bug14201\Foo', $i);
69+
return $i->fooName;
70+
}, $items),
71+
$first instanceOf Bar => array_map(function ($i) {
72+
assertType('Bug14201\Bar', $i);
73+
return $i->barName;
74+
}, $items),
75+
default => throw new \RuntimeException('None of Foo nor Bar')
76+
};
77+
}
78+
79+
/**
80+
* @param array<Foo|Bar> $items
81+
*/
82+
public function doitIfArrayUnion(array $items): void
83+
{
84+
if ([] === $items) {return; }
85+
86+
$first = reset($items);
87+
if ($first instanceof Foo) {
88+
assertType('non-empty-array<Bug14201\Foo>', $items);
89+
array_map(function ($i) {
90+
assertType('Bug14201\Foo', $i);
91+
return $i->fooName;
92+
}, $items);
93+
} elseif ($first instanceof Bar) {
94+
assertType('non-empty-array<Bug14201\Bar>', $items);
95+
array_map(function ($i) {
96+
assertType('Bug14201\Bar', $i);
97+
return $i->barName;
98+
}, $items);
99+
}
100+
}
57101
}

0 commit comments

Comments
 (0)