Skip to content

Commit 7e1c0b2

Browse files
mad-brillerondrejmirtes
authored andcommitted
Micro optimizations in type combinator.
1 parent 25203fd commit 7e1c0b2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/Type/TypeCombinator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ class TypeCombinator
4040

4141
public static function addNull(Type $type): Type
4242
{
43-
if ((new NullType())->isSuperTypeOf($type)->no()) {
44-
return self::union($type, new NullType());
43+
$nullType = new NullType();
44+
45+
if ($nullType->isSuperTypeOf($type)->no()) {
46+
return self::union($type, $nullType);
4547
}
4648

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

242244
// simplify string[] | int[] to (string|int)[]
243245
for ($i = 0; $i < $typesCount; $i++) {
246+
if (! $types[$i] instanceof IterableType) {
247+
continue;
248+
}
249+
244250
for ($j = $i + 1; $j < $typesCount; $j++) {
245-
if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
251+
if ($types[$j] instanceof IterableType) {
246252
$types[$i] = new IterableType(
247253
self::union($types[$i]->getIterableKeyType(), $types[$j]->getIterableKeyType()),
248254
self::union($types[$i]->getIterableValueType(), $types[$j]->getIterableValueType()),

0 commit comments

Comments
 (0)