Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject as AliasedObject;

final class AliasedImport
{
public function run(?AliasedObject $anotherObject)
{
if (empty($anotherObject)) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject as AliasedObject;

final class AliasedImport
{
public function run(?AliasedObject $anotherObject)
{
if (!$anotherObject instanceof \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject) {
return false;
}

return true;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@
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
*/
final class EmptyOnNullableObjectToInstanceOfRector extends AbstractRector
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory
) {
}

Expand Down Expand Up @@ -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);
}
}
Loading