Skip to content

Commit aae510e

Browse files
vokuondrejmirtes
authored andcommitted
faster TypeCombinator::union()
1 parent f798546 commit aae510e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/Type/TypeCombinator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,24 @@ public static function containsNull(Type $type): bool
168168

169169
public static function union(Type ...$types): Type
170170
{
171+
$typesCount = count($types);
172+
171173
$benevolentTypes = [];
172174
$benevolentUnionObject = null;
173175
// transform A | (B | C) to A | B | C
174-
for ($i = 0; $i < count($types); $i++) {
176+
for ($i = 0; $i < $typesCount; $i++) {
175177
if ($types[$i] instanceof BenevolentUnionType) {
176178
if ($types[$i] instanceof TemplateBenevolentUnionType && $benevolentUnionObject === null) {
177179
$benevolentUnionObject = $types[$i];
178180
}
179-
foreach ($types[$i]->getTypes() as $benevolentInnerType) {
181+
$benevolentTypesCount = 0;
182+
$typesInner = $types[$i]->getTypes();
183+
foreach ($typesInner as $benevolentInnerType) {
184+
$benevolentTypesCount++;
180185
$benevolentTypes[$benevolentInnerType->describe(VerbosityLevel::value())] = $benevolentInnerType;
181186
}
182-
array_splice($types, $i, 1, $types[$i]->getTypes());
187+
array_splice($types, $i, 1, $typesInner);
188+
$typesCount += $benevolentTypesCount - 1;
183189
continue;
184190
}
185191
if (!($types[$i] instanceof UnionType)) {
@@ -189,10 +195,11 @@ public static function union(Type ...$types): Type
189195
continue;
190196
}
191197

192-
array_splice($types, $i, 1, $types[$i]->getTypes());
198+
$typesInner = $types[$i]->getTypes();
199+
array_splice($types, $i, 1, $typesInner);
200+
$typesCount += count($typesInner) - 1;
193201
}
194202

195-
$typesCount = count($types);
196203
$arrayTypes = [];
197204
$arrayAccessoryTypes = [];
198205
$scalarTypes = [];

0 commit comments

Comments
 (0)