Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Dec 21, 2022
1 parent 04ccb01 commit 8a49c21
Showing 1 changed file with 9 additions and 103 deletions.
112 changes: 9 additions & 103 deletions src/Type/TypeCombinator.php
Expand Up @@ -167,26 +167,13 @@ public static function union(Type ...$types): Type

$arrayTypes = [];
$scalarTypes = [];
$hasGenericScalarTypes = [];
for ($i = 0; $i < $typesCount; $i++) {
if ($types[$i] instanceof ConstantScalarType) {
$type = $types[$i];
$scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::cache()))] = $type;
unset($types[$i]);
continue;
}
if ($types[$i] instanceof BooleanType) {
$hasGenericScalarTypes[ConstantBooleanType::class] = true;
}
if ($types[$i] instanceof FloatType) {
$hasGenericScalarTypes[ConstantFloatType::class] = true;
}
if ($types[$i] instanceof IntegerType && !$types[$i] instanceof IntegerRangeType) {
$hasGenericScalarTypes[ConstantIntegerType::class] = true;
}
if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
$hasGenericScalarTypes[ConstantStringType::class] = true;
}
if (!$types[$i]->isArray()->yes()) {
continue;
}
Expand All @@ -196,7 +183,15 @@ public static function union(Type ...$types): Type
}

foreach ($scalarTypes as $classType => $scalarTypeItems) {
$scalarTypes[$classType] = array_values($scalarTypeItems);
if ($classType === ConstantBooleanType::class && count($scalarTypeItems) === 2) {
$types[] = new BooleanType();
continue;
}
if (count($scalarTypeItems) > 1) {
$types[] = new UnionType(array_values($scalarTypeItems), true);
continue;
}
$types[] = reset($scalarTypeItems);
}

$types = array_values(
Expand All @@ -207,54 +202,6 @@ public static function union(Type ...$types): Type
);
$typesCount = count($types);

foreach ($scalarTypes as $classType => $scalarTypeItems) {
if (isset($hasGenericScalarTypes[$classType])) {
unset($scalarTypes[$classType]);
continue;
}
if ($classType === ConstantBooleanType::class && count($scalarTypeItems) === 2) {
$types[] = new BooleanType();
$typesCount++;
unset($scalarTypes[$classType]);
continue;
}

$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;
}

[$a, $b] = $compareResult;
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;
}
}
}

$scalarTypes[$classType] = $scalarTypeItems;
}

if (count($types) > 16) {
$newTypes = [];
foreach ($types as $type) {
$newTypes[$type->describe(VerbosityLevel::cache())] = $type;
}
$types = array_values($newTypes);
$typesCount = count($types);
}

// transform A | A to A
// transform A | never to A
for ($i = 0; $i < $typesCount; $i++) {
Expand All @@ -280,13 +227,6 @@ public static function union(Type ...$types): Type
}
}

foreach ($scalarTypes as $scalarTypeItems) {
foreach ($scalarTypeItems as $scalarType) {
$types[] = $scalarType;
$typesCount++;
}
}

if ($typesCount === 0) {
return new NeverType();
}
Expand Down Expand Up @@ -394,40 +334,6 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
}
}

if ($a instanceof UnionType && !$a instanceof TemplateType && $b instanceof UnionType && !$b instanceof TemplateType) {
// TODO
return [TypeCombinator::union($a, ...$b->getTypes()), null];
}

if ($a instanceof UnionType && !$a instanceof TemplateType) {
$aTypes = $a->getTypes();
foreach ($aTypes as $i => $aType) {
$compareResult = self::compareTypesInUnion($aType, $b);
if ($compareResult === null) {
continue;
}
$aTypes[$i] = $compareResult[0] ?? $compareResult[1];
return [new UnionType($aTypes, true), null];
}

return null;
}

if ($b instanceof UnionType && !$b instanceof TemplateType) {
$bTypes = $b->getTypes();
foreach ($bTypes as $i => $bType) {
$compareResult = self::compareTypesInUnion($bType, $a);
if ($compareResult === null) {
continue;
}

$bTypes[$i] = $compareResult[0] ?? $compareResult[1];
return [null, new UnionType($bTypes, true)];
}

return null;
}

if ($b->isSuperTypeOf($a)->yes()) {
return [null, $b];
}
Expand Down

0 comments on commit 8a49c21

Please sign in to comment.