Skip to content

Commit

Permalink
TypeCombinator - call count() less often
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 2, 2021
1 parent 5314a6b commit f3c054b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Type/TypeCombinator.php
Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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];
}

Expand Down

0 comments on commit f3c054b

Please sign in to comment.