Skip to content

Commit

Permalink
Merge 9f359d5 into 7c8a861
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 10, 2020
2 parents 7c8a861 + 9f359d5 commit 7b3d3a2
Showing 1 changed file with 7 additions and 36 deletions.
43 changes: 7 additions & 36 deletions packages/node-type-resolver/src/NodeTypeResolver.php
Expand Up @@ -158,10 +158,6 @@ public function isObjectType(Node $node, $requiredType): bool
return true;
}

if ($this->isUnionNullTypeOfRequiredType($requiredType, $resolvedType)) {
return true;
}

return $this->isMatchingUnionType($requiredType, $resolvedType);
}

Expand All @@ -180,7 +176,7 @@ public function resolve(Node $node): Type
*/
public function isNullableType(Node $node): bool
{
$nodeType = $this->getStaticType($node);
$nodeType = $this->resolve($node);
if (! $nodeType instanceof UnionType) {
return false;
}
Expand Down Expand Up @@ -231,7 +227,8 @@ public function getStaticType(Node $node): Type

public function isNullableObjectType(Node $node): bool
{
$nodeType = $this->getStaticType($node);
$nodeType = $this->resolve($node);

if (! $nodeType instanceof UnionType) {
return false;
}
Expand Down Expand Up @@ -269,7 +266,7 @@ public function isStaticType(Node $node, string $staticTypeClass): bool
));
}

return is_a($this->getStaticType($node), $staticTypeClass);
return is_a($this->resolve($node), $staticTypeClass);
}

private function addPerNodeTypeResolver(PerNodeTypeResolverInterface $perNodeTypeResolver): void
Expand Down Expand Up @@ -315,34 +312,6 @@ private function isFnMatch(Node $node, string $requiredType): bool
return false;
}

/**
* Matches:
* - Type|null
*/
private function isUnionNullTypeOfRequiredType(ObjectType $objectType, Type $resolvedType): bool
{
if (! $resolvedType instanceof UnionType) {
return false;
}

if (count($resolvedType->getTypes()) !== 2) {
return false;
}

$firstType = $resolvedType->getTypes()[0];
$secondType = $resolvedType->getTypes()[1];

if ($firstType instanceof NullType && $secondType instanceof ObjectType) {
return $objectType->equals($firstType);
}

if ($secondType instanceof NullType && $firstType instanceof ObjectType) {
return $objectType->equals($secondType);
}

return false;
}

private function resolveFirstType(Node $node): Type
{
foreach ($this->perNodeTypeResolvers as $perNodeTypeResolver) {
Expand Down Expand Up @@ -434,7 +403,7 @@ private function isAnonymousClass(Node $node): bool

private function getVendorPropertyFetchType(PropertyFetch $propertyFetch): ?Type
{
$varObjectType = $this->getStaticType($propertyFetch->var);
$varObjectType = $this->resolve($propertyFetch->var);
if (! $varObjectType instanceof TypeWithClassName) {
return null;
}
Expand Down Expand Up @@ -490,10 +459,12 @@ private function isMatchingUnionType(Type $requiredType, Type $resolvedType): bo
if (! $resolvedType instanceof UnionType) {
return false;
}

foreach ($resolvedType->getTypes() as $unionedType) {
if (! $unionedType->equals($requiredType)) {
continue;
}

return true;
}

Expand Down

0 comments on commit 7b3d3a2

Please sign in to comment.