Skip to content

Commit

Permalink
Refactor is_a (#5907)
Browse files Browse the repository at this point in the history
* Change is_a to UnionType()->isSuperTypeOf()->yes in NetteControlFactoryInterfaceAnalyzer

* fix with check ShortenedObjectType

* change is_a in ThrowWithPreviousExceptionRector
  • Loading branch information
samsonasik authored Mar 21, 2021
1 parent 0693fff commit 747104d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 747104d

Please sign in to comment.