Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster TypeCombinator::union() #743

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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