Skip to content

Commit 480626e

Browse files
committed
UnionType::pickTypes overriden in BenevolentUnionType for a more benevolent behaviour
1 parent 6debffd commit 480626e

File tree

3 files changed

+47
-41
lines changed

3 files changed

+47
-41
lines changed

src/Type/BenevolentUnionType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ protected function unionTypes(callable $getType): Type
4343
return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$resultTypes));
4444
}
4545

46+
protected function pickTypes(callable $getTypes): array
47+
{
48+
$types = [];
49+
foreach ($this->getTypes() as $type) {
50+
$innerTypes = $getTypes($type);
51+
foreach ($innerTypes as $innerType) {
52+
$types[] = $innerType;
53+
}
54+
}
55+
56+
return $types;
57+
}
58+
4659
public function getOffsetValueType(Type $offsetType): Type
4760
{
4861
$types = [];

src/Type/UnionType.php

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,55 +114,17 @@ public function getReferencedClasses(): array
114114

115115
public function getArrays(): array
116116
{
117-
$arrays = [];
118-
foreach ($this->types as $type) {
119-
$innerTypeArrays = $type->getArrays();
120-
if ($innerTypeArrays === []) {
121-
return [];
122-
}
123-
124-
foreach ($innerTypeArrays as $array) {
125-
$arrays[] = $array;
126-
}
127-
}
128-
129-
return $arrays;
117+
return $this->pickTypes(static fn (Type $type) => $type->getArrays());
130118
}
131119

132120
public function getConstantArrays(): array
133121
{
134-
$constantArrays = [];
135-
foreach ($this->types as $type) {
136-
$typeAsConstantArrays = $type->getConstantArrays();
137-
138-
if ($typeAsConstantArrays === []) {
139-
return [];
140-
}
141-
142-
foreach ($typeAsConstantArrays as $constantArray) {
143-
$constantArrays[] = $constantArray;
144-
}
145-
}
146-
147-
return $constantArrays;
122+
return $this->pickTypes(static fn (Type $type) => $type->getConstantArrays());
148123
}
149124

150125
public function getConstantStrings(): array
151126
{
152-
$strings = [];
153-
foreach ($this->types as $type) {
154-
$constantStrings = $type->getConstantStrings();
155-
156-
if ($constantStrings === []) {
157-
return [];
158-
}
159-
160-
foreach ($constantStrings as $string) {
161-
$strings[] = $string;
162-
}
163-
}
164-
165-
return $strings;
127+
return $this->pickTypes(static fn (Type $type) => $type->getConstantStrings());
166128
}
167129

168130
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
@@ -929,4 +891,26 @@ protected function unionTypes(callable $getType): Type
929891
return TypeCombinator::union(...array_map($getType, $this->types));
930892
}
931893

894+
/**
895+
* @template T of Type
896+
* @param callable(Type $type): list<T> $getTypes
897+
* @return list<T>
898+
*/
899+
protected function pickTypes(callable $getTypes): array
900+
{
901+
$types = [];
902+
foreach ($this->types as $type) {
903+
$innerTypes = $getTypes($type);
904+
if ($innerTypes === []) {
905+
return [];
906+
}
907+
908+
foreach ($innerTypes as $innerType) {
909+
$types[] = $innerType;
910+
}
911+
}
912+
913+
return $types;
914+
}
915+
932916
}

tests/PHPStan/Type/UnionTypeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,15 @@ public function dataGetConstantStrings(): iterable
14071407
"'bar'",
14081408
],
14091409
],
1410+
[
1411+
new BenevolentUnionType([
1412+
new ConstantStringType('foo'),
1413+
new NullType(),
1414+
]),
1415+
[
1416+
"'foo'",
1417+
],
1418+
],
14101419
];
14111420
}
14121421

0 commit comments

Comments
 (0)