From ccccffce49e5858cdbee8f407f77025fe2e18742 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 8 Aug 2026 09:08:23 +0200 Subject: [PATCH] TypeCombinator: remove unnecessary iteration over types --- src/Type/TypeCombinator.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 1c6b04d0d6..838df16e51 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -1624,10 +1624,17 @@ public static function doIntersect(Type ...$types): Type return $types[0]; } + // The comparator only orders UnionTypes relative to each other, so sorting is + // a no-op unless there are at least two of them. Skip it in the common case. + $unionTypesCount = 0; foreach ($types as $type) { if ($type instanceof NeverType && !$type->isExplicit()) { return $type; } + if (!$type instanceof UnionType) { + continue; + } + $unionTypesCount++; } // Fast path: the intersection of two plain unions whose members are all finite, @@ -1667,18 +1674,6 @@ public static function doIntersect(Type ...$types): Type return 0; }; - // The comparator only orders UnionTypes relative to each other, so sorting is - // a no-op unless there are at least two of them. Skip it in the common case. - $unionTypesCount = 0; - foreach ($types as $type) { - if (!$type instanceof UnionType) { - continue; - } - $unionTypesCount++; - if ($unionTypesCount >= 2) { - break; - } - } if ($unionTypesCount >= 2) { usort($types, $sortTypes); }