From 747104df6b81939b41cca9ee8edb985f1a2ecaa0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 21 Mar 2021 19:12:35 +0700 Subject: [PATCH] Refactor is_a (#5907) * Change is_a to UnionType()->isSuperTypeOf()->yes in NetteControlFactoryInterfaceAnalyzer * fix with check ShortenedObjectType * change is_a in ThrowWithPreviousExceptionRector --- .../ThrowWithPreviousExceptionRector.php | 4 +++- .../NetteControlFactoryInterfaceAnalyzer.php | 24 +++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index 3a1f27e3313b..28cc93ba0602 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -16,6 +16,7 @@ use PhpParser\Node\Stmt\Throw_; use PhpParser\NodeTraverser; use PHPStan\Reflection\ReflectionProvider; +use PHPStan\Type\ObjectType; use PHPStan\Type\TypeWithClassName; use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\MethodName; @@ -186,7 +187,8 @@ private function resolveExceptionArgumentPosition(Name $exceptionName): ?int continue; } - if (! is_a($parameterType->getClassName(), Throwable::class, true)) { + $objectType = new ObjectType('Throwable'); + if ($objectType->isSuperTypeOf($parameterType)->no()) { continue; } diff --git a/rules/NetteToSymfony/NodeAnalyzer/NetteControlFactoryInterfaceAnalyzer.php b/rules/NetteToSymfony/NodeAnalyzer/NetteControlFactoryInterfaceAnalyzer.php index 27956cb9ba91..82ed4261cda1 100644 --- a/rules/NetteToSymfony/NodeAnalyzer/NetteControlFactoryInterfaceAnalyzer.php +++ b/rules/NetteToSymfony/NodeAnalyzer/NetteControlFactoryInterfaceAnalyzer.php @@ -5,8 +5,10 @@ namespace Rector\NetteToSymfony\NodeAnalyzer; use PhpParser\Node\Stmt\Interface_; +use PHPStan\Type\ObjectType; use PHPStan\Type\TypeWithClassName; -use Rector\NodeTypeResolver\NodeTypeResolver; +use PHPStan\Type\UnionType; +use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; final class NetteControlFactoryInterfaceAnalyzer @@ -16,15 +18,9 @@ final class NetteControlFactoryInterfaceAnalyzer */ private $returnTypeInferer; - /** - * @var NodeTypeResolver - */ - private $nodeTypeResolver; - - public function __construct(ReturnTypeInferer $returnTypeInferer, NodeTypeResolver $nodeTypeResolver) + public function __construct(ReturnTypeInferer $returnTypeInferer) { $this->returnTypeInferer = $returnTypeInferer; - $this->nodeTypeResolver = $nodeTypeResolver; } /** @@ -38,12 +34,16 @@ public function isComponentFactoryInterface(Interface_ $interface): bool return false; } - $className = $this->nodeTypeResolver->getFullyQualifiedClassName($returnType); - if (is_a($className, 'Nette\Application\UI\Control', true)) { - return true; + $controlOrForm = new UnionType([ + new ObjectType('Nette\Application\UI\Control'), + new ObjectType('Nette\Application\UI\Form'), + ]); + + if ($returnType instanceof ShortenedObjectType) { + $returnType = new ObjectType($returnType->getFullyQualifiedName()); } - if (is_a($className, 'Nette\Application\UI\Form', true)) { + if ($controlOrForm->isSuperTypeOf($returnType)->yes()) { return true; } }