Skip to content

Commit c3d0989

Browse files
committed
Fix native union type with incomplete PHPDoc
1 parent 917cdb1 commit c3d0989

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Type/TypehintHelper.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,22 @@ public static function decideType(
130130

131131
$resultType = $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes() ? $phpDocType : $type;
132132

133-
if (TypeCombinator::containsNull($type)) {
133+
if ($type instanceof UnionType) {
134+
$addToUnionTypes = [];
135+
foreach ($type->getTypes() as $innerType) {
136+
if (!$innerType->isSuperTypeOf($resultType)->no()) {
137+
continue;
138+
}
139+
140+
$addToUnionTypes[] = $innerType;
141+
}
142+
143+
if (count($addToUnionTypes) > 0) {
144+
$type = TypeCombinator::union($resultType, ...$addToUnionTypes);
145+
} else {
146+
$type = $resultType;
147+
}
148+
} elseif (TypeCombinator::containsNull($type)) {
134149
$type = TypeCombinator::addNull($resultType);
135150
} else {
136151
$type = $resultType;

tests/PHPStan/Reflection/data/unionTypes.php

+19
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,23 @@ public function doBar(): array|false
5959

6060
}
6161

62+
/**
63+
* @param array<int, string> $foo
64+
*/
65+
public function doBaz(array|false $foo): void
66+
{
67+
assertType('array<int, string>|false', $foo);
68+
assertNativeType('array|false', $foo);
69+
70+
assertType('array<int, string>|false', $this->doLorem());
71+
}
72+
73+
/**
74+
* @return array<int, string>
75+
*/
76+
public function doLorem(): array|false
77+
{
78+
79+
}
80+
6281
}

0 commit comments

Comments
 (0)