diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index e3a300cfe9..6dd1fd1bf0 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -272,14 +272,16 @@ public static function union(Type ...$types): Type ); // simplify string[] | int[] to (string|int)[] - for ($i = 0; $i < count($types); $i++) { - for ($j = $i + 1; $j < count($types); $j++) { + $typesCount = count($types); + for ($i = 0; $i < $typesCount; $i++) { + for ($j = $i + 1; $j < $typesCount; $j++) { if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) { $types[$i] = new IterableType( self::union($types[$i]->getIterableKeyType(), $types[$j]->getIterableKeyType()), self::union($types[$i]->getIterableValueType(), $types[$j]->getIterableValueType()) ); array_splice($types, $j, 1); + $typesCount--; continue 2; } } @@ -296,8 +298,10 @@ public static function union(Type ...$types): Type continue; } - for ($i = 0; $i < count($types); $i++) { - for ($j = 0; $j < count($scalarTypeItems); $j++) { + $typesCount = count($types); + $scalarTypeItemsCount = count($scalarTypeItems); + for ($i = 0; $i < $typesCount; $i++) { + for ($j = 0; $j < $scalarTypeItemsCount; $j++) { $compareResult = self::compareTypesInUnion($types[$i], $scalarTypeItems[$j]); if ($compareResult === null) { continue; @@ -307,11 +311,13 @@ public static function union(Type ...$types): Type if ($a !== null) { $types[$i] = $a; array_splice($scalarTypeItems, $j--, 1); + $scalarTypeItemsCount--; continue 1; } if ($b !== null) { $scalarTypeItems[$j] = $b; array_splice($types, $i--, 1); + $typesCount--; continue 2; } } @@ -322,8 +328,9 @@ public static function union(Type ...$types): Type // transform A | A to A // transform A | never to A - for ($i = 0; $i < count($types); $i++) { - for ($j = $i + 1; $j < count($types); $j++) { + $typesCount = count($types); + for ($i = 0; $i < $typesCount; $i++) { + for ($j = $i + 1; $j < $typesCount; $j++) { $compareResult = self::compareTypesInUnion($types[$i], $types[$j]); if ($compareResult === null) { continue; @@ -333,11 +340,13 @@ public static function union(Type ...$types): Type if ($a !== null) { $types[$i] = $a; array_splice($types, $j--, 1); + $typesCount--; continue 1; } if ($b !== null) { $types[$j] = $b; array_splice($types, $i--, 1); + $typesCount--; continue 2; } } @@ -349,10 +358,11 @@ public static function union(Type ...$types): Type } } - if (count($types) === 0) { + $typesCount = count($types); + if ($typesCount === 0) { return new NeverType(); - - } elseif (count($types) === 1) { + } + if ($typesCount === 1) { return $types[0]; }