Skip to content

Commit

Permalink
decopule ternary type method
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 7, 2021
1 parent eb811b1 commit be3d0c6
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,7 @@ public function isObjectType(Node $node, ObjectType $requiredObjectType): bool
public function resolve(Node $node): Type
{
if ($node instanceof Ternary) {
if ($node->if !== null) {
$first = $this->resolve($node->if);
$second = $this->resolve($node->else);

if ($this->isUnionTypeable($first, $second)) {
return new UnionType([$first, $second]);
}
}

$condType = $this->resolve($node->cond);
if ($this->isNullableType($node->cond) && $condType instanceof UnionType) {
$first = $condType->getTypes()[0];
$second = $this->resolve($node->else);

if ($this->isUnionTypeable($first, $second)) {
return new UnionType([$first, $second]);
}
}
return $this->resolveTernaryType($node);
}

if ($node instanceof Coalesce) {
Expand Down Expand Up @@ -501,4 +484,28 @@ private function resolveObjectType(ObjectType $resolvedObjectType, ObjectType $r

return true;
}

private function resolveTernaryType(Ternary $ternary): MixedType|UnionType
{
if ($ternary->if !== null) {
$first = $this->resolve($ternary->if);
$second = $this->resolve($ternary->else);

if ($this->isUnionTypeable($first, $second)) {
return new UnionType([$first, $second]);
}
}

$condType = $this->resolve($ternary->cond);
if ($this->isNullableType($ternary->cond) && $condType instanceof UnionType) {
$first = $condType->getTypes()[0];
$second = $this->resolve($ternary->else);

if ($this->isUnionTypeable($first, $second)) {
return new UnionType([$first, $second]);
}
}

return new MixedType();
}
}

0 comments on commit be3d0c6

Please sign in to comment.