Skip to content

Commit

Permalink
faster TypeCombinator::union()
Browse files Browse the repository at this point in the history
  • Loading branch information
voku authored and ondrejmirtes committed Oct 26, 2021
1 parent f798546 commit aae510e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,24 @@ public static function containsNull(Type $type): bool

public static function union(Type ...$types): Type
{
$typesCount = count($types);

$benevolentTypes = [];
$benevolentUnionObject = null;
// transform A | (B | C) to A | B | C
for ($i = 0; $i < count($types); $i++) {
for ($i = 0; $i < $typesCount; $i++) {
if ($types[$i] instanceof BenevolentUnionType) {
if ($types[$i] instanceof TemplateBenevolentUnionType && $benevolentUnionObject === null) {
$benevolentUnionObject = $types[$i];
}
foreach ($types[$i]->getTypes() as $benevolentInnerType) {
$benevolentTypesCount = 0;
$typesInner = $types[$i]->getTypes();
foreach ($typesInner as $benevolentInnerType) {
$benevolentTypesCount++;
$benevolentTypes[$benevolentInnerType->describe(VerbosityLevel::value())] = $benevolentInnerType;
}
array_splice($types, $i, 1, $types[$i]->getTypes());
array_splice($types, $i, 1, $typesInner);
$typesCount += $benevolentTypesCount - 1;
continue;
}
if (!($types[$i] instanceof UnionType)) {
Expand All @@ -189,10 +195,11 @@ public static function union(Type ...$types): Type
continue;
}

array_splice($types, $i, 1, $types[$i]->getTypes());
$typesInner = $types[$i]->getTypes();
array_splice($types, $i, 1, $typesInner);
$typesCount += count($typesInner) - 1;
}

$typesCount = count($types);
$arrayTypes = [];
$arrayAccessoryTypes = [];
$scalarTypes = [];
Expand Down

0 comments on commit aae510e

Please sign in to comment.