Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Dec 19, 2022
1 parent feb1f10 commit 368ae34
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Type/TypeCombinator.php
Expand Up @@ -107,6 +107,7 @@ public static function union(Type ...$types): Type
return new NeverType();
}

$arrayTypes = [];
$benevolentTypes = [];
$benevolentUnionObject = null;
// transform A | (B | C) to A | B | C
Expand All @@ -128,10 +129,27 @@ public static function union(Type ...$types): Type
if (!($types[$i] instanceof UnionType)) {
continue;
}
if ($types[$i]->isNormalized()) {
if ($types[$i] instanceof TemplateType) {
continue;
}
if ($types[$i] instanceof TemplateType) {
if ($types[$i]->isNormalized()) {
$innerTypes = $types[$i]->getTypes();
foreach ($innerTypes as $key => $innerType) {
if ($innerType->isArray()->yes()) {
$arrayTypes[] = $innerType;
$typesCount++;
unset($innerTypes[$key]);
}
}
if (count($innerTypes) === 0) {
$typesCount--;
continue;
}
if (count($innerTypes) === 1) {
$types[] = $innerTypes[0];
continue;
}
$types[$i] = new UnionType(array_values($innerTypes), true);
continue;
}

Expand All @@ -144,7 +162,6 @@ public static function union(Type ...$types): Type
return $types[0];
}

$arrayTypes = [];
$scalarTypes = [];
$hasGenericScalarTypes = [];
for ($i = 0; $i < $typesCount; $i++) {
Expand Down Expand Up @@ -326,14 +343,15 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
if ($a instanceof IntegerRangeType && $b instanceof IntegerRangeType) {
return null;
}
if ($a instanceof ConstantArrayType && $b instanceof ConstantArrayType) {
return null;
}

if ($a instanceof HasOffsetValueType && $b instanceof HasOffsetValueType) {
if ($a->getOffsetType()->equals($b->getOffsetType())) {
return [new HasOffsetValueType($a->getOffsetType(), self::union($a->getValueType(), $b->getValueType())), null];
}
}
if ($a instanceof ConstantArrayType && $b instanceof ConstantArrayType) {
return null;
}

// simplify string[] | int[] to (string|int)[]
if ($a instanceof IterableType && $b instanceof IterableType) {
Expand Down

0 comments on commit 368ae34

Please sign in to comment.