Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 21, 2021
1 parent ecf4a81 commit 075aaf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
38 changes: 5 additions & 33 deletions rules/TypeDeclaration/TypeAnalyzer/ObjectTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public function isCurrentObjectTypeSubType(Type $currentType, Type $newType): bo
return true;
}

if ($this->isBothIterableIteratorGeneratorTraversable($currentType, $newType)) {
return true;
}

if (! $currentType instanceof ObjectType) {
return false;
}
Expand All @@ -37,12 +33,8 @@ public function isCurrentObjectTypeSubType(Type $currentType, Type $newType): bo
return false;
}

return is_a($currentType->getClassName(), $newType->getClassName(), true);
}

private function isClosure(Type $type): bool
{
return $type instanceof ObjectType && $type->getClassName() === 'Closure';
return $newType->isSuperTypeOf($currentType)
->yes();
}

private function isBothCallable(Type $currentType, Type $newType): bool
Expand All @@ -54,29 +46,9 @@ private function isBothCallable(Type $currentType, Type $newType): bool
return $newType instanceof CallableType && $this->isClosure($currentType);
}

private function isBothIterableIteratorGeneratorTraversable(Type $currentType, Type $newType): bool
{
if (! $currentType instanceof ObjectType) {
return false;
}

if (! $newType instanceof ObjectType) {
return false;
}

if ($currentType->getClassName() === 'iterable' && $this->isTraversableGeneratorIterator($newType)) {
return true;
}

if ($newType->getClassName() !== 'iterable') {
return false;
}

return $this->isTraversableGeneratorIterator($currentType);
}

private function isTraversableGeneratorIterator(ObjectType $objectType): bool
private function isClosure(Type $type): bool
{
return in_array($objectType->getClassName(), ['Traversable', 'Generator', 'Iterator'], true);
$closureObjectType = new ObjectType('Closure');
return $closureObjectType->equals($type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedGenericObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\TypeDeclaration\Contract\TypeInferer\ReturnTypeInfererInterface;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;

Expand Down Expand Up @@ -66,7 +67,15 @@ public function inferFunctionLike(FunctionLike $functionLike): Type
continue;
}

$types[] = $this->nodeTypeResolver->getStaticType($value);
$resolvedType = $this->nodeTypeResolver->getStaticType($value);
if ($resolvedType instanceof MixedType) {
continue;
}
$types[] = $resolvedType;
}

if (count($types) === 0) {
return new FullyQualifiedObjectType('Iterator');
}

$types = $this->typeFactory->createMixedPassedOrUnionType($types);
Expand Down

0 comments on commit 075aaf6

Please sign in to comment.