Skip to content

Commit 1332c41

Browse files
SanderMullerondrejmirtes
authored andcommitted
autoresearch: TypeCombinator::union fast path for never/mixed/identity 2-type cases
1 parent 3a08df4 commit 1332c41

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
@@ -153,6 +153,33 @@ public static function union(Type ...$types): Type
153153
return new NeverType();
154154
}
155155

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

0 commit comments

Comments
 (0)