Skip to content

Commit b61a0fd

Browse files
vokuondrejmirtes
authored andcommitted
faster TypeCombinator::union() v2
1 parent 2e32030 commit b61a0fd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/Type/TypeCombinator.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ public static function union(Type ...$types): Type
277277
self::processArrayTypes($arrayTypes, $arrayAccessoryTypesToProcess)
278278
)
279279
);
280+
$typesCount = count($types);
280281

281282
// simplify string[] | int[] to (string|int)[]
282-
$typesCount = count($types);
283283
for ($i = 0; $i < $typesCount; $i++) {
284284
for ($j = $i + 1; $j < $typesCount; $j++) {
285285
if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
@@ -301,11 +301,11 @@ public static function union(Type ...$types): Type
301301
}
302302
if ($classType === ConstantBooleanType::class && count($scalarTypeItems) === 2) {
303303
$types[] = new BooleanType();
304+
$typesCount++;
304305
unset($scalarTypes[$classType]);
305306
continue;
306307
}
307308

308-
$typesCount = count($types);
309309
$scalarTypeItemsCount = count($scalarTypeItems);
310310
for ($i = 0; $i < $typesCount; $i++) {
311311
for ($j = 0; $j < $scalarTypeItemsCount; $j++) {
@@ -339,11 +339,11 @@ public static function union(Type ...$types): Type
339339
$newTypes[$type->describe(VerbosityLevel::cache())] = $type;
340340
}
341341
$types = array_values($newTypes);
342+
$typesCount = count($types);
342343
}
343344

344345
// transform A | A to A
345346
// transform A | never to A
346-
$typesCount = count($types);
347347
for ($i = 0; $i < $typesCount; $i++) {
348348
for ($j = $i + 1; $j < $typesCount; $j++) {
349349
$compareResult = self::compareTypesInUnion($types[$i], $types[$j]);
@@ -370,18 +370,18 @@ public static function union(Type ...$types): Type
370370
foreach ($scalarTypes as $scalarTypeItems) {
371371
foreach ($scalarTypeItems as $scalarType) {
372372
$types[] = $scalarType;
373+
$typesCount++;
373374
}
374375
}
375376

376-
$typesCount = count($types);
377377
if ($typesCount === 0) {
378378
return new NeverType();
379379
}
380380
if ($typesCount === 1) {
381381
return $types[0];
382382
}
383383

384-
if (count($benevolentTypes) > 0) {
384+
if ($benevolentTypes !== []) {
385385
$tempTypes = $types;
386386
foreach ($tempTypes as $i => $type) {
387387
if (!isset($benevolentTypes[$type->describe(VerbosityLevel::value())])) {
@@ -391,7 +391,7 @@ public static function union(Type ...$types): Type
391391
unset($tempTypes[$i]);
392392
}
393393

394-
if (count($tempTypes) === 0) {
394+
if ($tempTypes === []) {
395395
if ($benevolentUnionObject instanceof TemplateBenevolentUnionType) {
396396
return $benevolentUnionObject->withTypes($types);
397397
}
@@ -575,9 +575,11 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp
575575
break 2;
576576
}
577577
}
578-
if (count($arrayTypes) === 0) {
578+
579+
if ($arrayTypes === []) {
579580
return [];
580-
} elseif (count($arrayTypes) === 1) {
581+
}
582+
if (count($arrayTypes) === 1) {
581583
return [
582584
self::intersect($arrayTypes[0], ...$accessoryTypes),
583585
];
@@ -895,7 +897,6 @@ public static function intersect(Type ...$types): Type
895897

896898
if (count($types) === 1) {
897899
return $types[0];
898-
899900
}
900901

901902
return new IntersectionType($types);

0 commit comments

Comments
 (0)