Skip to content

Commit 144c8bb

Browse files
committed
autoresearch: TypeCombinator::union fast path for never/mixed/identity 2-type cases
1 parent d8f5be7 commit 144c8bb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Type/TypeCombinator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,33 @@ public static function union(Type ...$types): Type
152152
return new NeverType();
153153
}
154154

155+
// Fast path for common 2-type cases
156+
if ($typesCount === 2) {
157+
$a = $types[0];
158+
$b = $types[1];
159+
160+
// union(never, X) = X and union(X, never) = X
161+
if ($a instanceof NeverType && !$a->isExplicit()) {
162+
return $b;
163+
}
164+
if ($b instanceof NeverType && !$b->isExplicit()) {
165+
return $a;
166+
}
167+
168+
// union(mixed, X) = mixed (non-explicit, non-template, no subtracted)
169+
if ($a instanceof MixedType && !$a->isExplicitMixed() && !$a instanceof TemplateMixedType && $a->getSubtractedType() === null) {
170+
return $a;
171+
}
172+
if ($b instanceof MixedType && !$b->isExplicitMixed() && !$b instanceof TemplateMixedType && $b->getSubtractedType() === null) {
173+
return $b;
174+
}
175+
176+
// union(X, X) = X (same object identity)
177+
if ($a === $b) {
178+
return $a;
179+
}
180+
}
181+
155182
$alreadyNormalized = [];
156183
$alreadyNormalizedCounter = 0;
157184

0 commit comments

Comments
 (0)