-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathInlineIsAInstanceOfRector.php
101 lines (99 loc) · 2.95 KB
/
InlineIsAInstanceOfRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector\InlineIsAInstanceOfRectorTest
*/
final class InlineIsAInstanceOfRector extends AbstractRector
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change is_a() with object and class name check to instanceof', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run(object $object)
{
return is_a($object, SomeType::class);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run(object $object)
{
return $object instanceof SomeType;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isName($node->name, 'is_a')) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$args = $node->getArgs();
$firstArgValue = $args[0]->value;
if (!$this->isFirstObjectType($firstArgValue)) {
return null;
}
/**
* instanceof with Variable is ok, while on FuncCal with instanceof cause fatal error, see https://3v4l.org/IHb30
*/
if ($args[1]->value instanceof Variable) {
return new Instanceof_($firstArgValue, $args[1]->value);
}
$className = $this->resolveClassName($args[1]->value);
if ($className === null) {
return null;
}
return new Instanceof_($firstArgValue, new FullyQualified($className));
}
private function resolveClassName(Expr $expr) : ?string
{
if (!$expr instanceof ClassConstFetch) {
return null;
}
$type = $this->getType($expr);
if ($type instanceof GenericClassStringType) {
$type = $type->getGenericType();
}
return ClassNameFromObjectTypeResolver::resolve($type);
}
private function isFirstObjectType(Expr $expr) : bool
{
$exprType = $this->getType($expr);
if ($exprType instanceof ObjectWithoutClassType) {
return \true;
}
return $exprType instanceof ObjectType;
}
}