Repository navigation
Expand file tree
/
Copy pathInlineIsAInstanceOfRector.php
More file actions
131 lines (111 loc) · 3.34 KB
/
Copy pathInlineIsAInstanceOfRector.php
File metadata and controls
131 lines (111 loc) · 3.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
/**
* @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();
$secondArgValue = $args[1]->value;
if (! $secondArgValue instanceof Variable && ! $secondArgValue instanceof ClassConstFetch) {
return null;
}
$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 ($secondArgValue instanceof Variable) {
return new Instanceof_($firstArgValue, $secondArgValue);
}
$className = $this->resolveClassName($secondArgValue);
if ($className === null) {
return null;
}
if ($secondArgValue->class instanceof Name) {
return new Instanceof_($firstArgValue, $secondArgValue->class);
}
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;
}
}