diff --git a/rules-tests/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector/Fixture/aliased_import.php.inc b/rules-tests/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector/Fixture/aliased_import.php.inc new file mode 100644 index 00000000000..46b7fc5ce59 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector/Fixture/aliased_import.php.inc @@ -0,0 +1,39 @@ + +----- + diff --git a/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php b/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php index 90418b0483f..d86c418bfa9 100644 --- a/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php +++ b/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php @@ -9,16 +9,14 @@ use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\Instanceof_; -use PhpParser\Node\Name; use PHPStan\Type\ObjectType; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; +use Rector\PhpParser\Node\NullableObjectToInstanceofFactory; use Rector\PHPStan\ScopeFetcher; -use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; -use Rector\StaticTypeMapper\StaticTypeMapper; /** * @see \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\EmptyOnNullableObjectToInstanceOfRectorTest @@ -26,7 +24,7 @@ final class EmptyOnNullableObjectToInstanceOfRector extends AbstractRector { public function __construct( - private readonly StaticTypeMapper $staticTypeMapper + private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory ) { } @@ -107,17 +105,6 @@ public function refactor(Node $node): null|Instanceof_|BooleanNot return null; } - $objectType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($exprType, TypeKind::RETURN); - if (! $objectType instanceof Name) { - return null; - } - - $instanceof = new Instanceof_($empty->expr, $objectType); - - if ($isNegated) { - return $instanceof; - } - - return new BooleanNot($instanceof); + return $this->nullableObjectToInstanceofFactory->create($empty->expr, $exprType, ! $isNegated); } }