Skip to content

Commit

Permalink
Micro optimizations in type combinator.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller authored and ondrejmirtes committed Oct 11, 2022
1 parent 25203fd commit 7e1c0b2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class TypeCombinator

public static function addNull(Type $type): Type
{
if ((new NullType())->isSuperTypeOf($type)->no()) {
return self::union($type, new NullType());
$nullType = new NullType();

if ($nullType->isSuperTypeOf($type)->no()) {
return self::union($type, $nullType);
}

return $type;
Expand Down Expand Up @@ -241,8 +243,12 @@ public static function union(Type ...$types): Type

// simplify string[] | int[] to (string|int)[]
for ($i = 0; $i < $typesCount; $i++) {
if (! $types[$i] instanceof IterableType) {
continue;
}

for ($j = $i + 1; $j < $typesCount; $j++) {
if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
if ($types[$j] instanceof IterableType) {
$types[$i] = new IterableType(
self::union($types[$i]->getIterableKeyType(), $types[$j]->getIterableKeyType()),
self::union($types[$i]->getIterableValueType(), $types[$j]->getIterableValueType()),
Expand Down

0 comments on commit 7e1c0b2

Please sign in to comment.