Skip to content

Commit be3da68

Browse files
authored
[TypeDeclaration] Migrate EmptyOnNullableObjectToInstanceOfRector to NullableObjectToInstanceofFactory (#8635)
1 parent f4bca02 commit be3da68

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject as AliasedObject;
6+
7+
final class AliasedImport
8+
{
9+
public function run(?AliasedObject $anotherObject)
10+
{
11+
if (empty($anotherObject)) {
12+
return false;
13+
}
14+
15+
return true;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;
24+
25+
use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject as AliasedObject;
26+
27+
final class AliasedImport
28+
{
29+
public function run(?AliasedObject $anotherObject)
30+
{
31+
if (!$anotherObject instanceof \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject) {
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
}
38+
39+
?>

‎rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@
99
use PhpParser\Node\Expr\BooleanNot;
1010
use PhpParser\Node\Expr\Empty_;
1111
use PhpParser\Node\Expr\Instanceof_;
12-
use PhpParser\Node\Name;
1312
use PHPStan\Type\ObjectType;
1413
use PHPStan\Type\TypeCombinator;
1514
use PHPStan\Type\UnionType;
15+
use Rector\PhpParser\Node\NullableObjectToInstanceofFactory;
1616
use Rector\PHPStan\ScopeFetcher;
17-
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1817
use Rector\Rector\AbstractRector;
1918
use Rector\RuleDoc\CodeSample\CodeSample;
2019
use Rector\RuleDoc\RuleDefinition;
21-
use Rector\StaticTypeMapper\StaticTypeMapper;
2220

2321
/**
2422
* @see \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\EmptyOnNullableObjectToInstanceOfRectorTest
2523
*/
2624
final class EmptyOnNullableObjectToInstanceOfRector extends AbstractRector
2725
{
2826
public function __construct(
29-
private readonly StaticTypeMapper $staticTypeMapper
27+
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory
3028
) {
3129
}
3230

@@ -107,17 +105,6 @@ public function refactor(Node $node): null|Instanceof_|BooleanNot
107105
return null;
108106
}
109107

110-
$objectType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($exprType, TypeKind::RETURN);
111-
if (! $objectType instanceof Name) {
112-
return null;
113-
}
114-
115-
$instanceof = new Instanceof_($empty->expr, $objectType);
116-
117-
if ($isNegated) {
118-
return $instanceof;
119-
}
120-
121-
return new BooleanNot($instanceof);
108+
return $this->nullableObjectToInstanceofFactory->create($empty->expr, $exprType, ! $isNegated);
122109
}
123110
}

0 commit comments

Comments
 (0)