Repository navigation
Expand file tree
/
Copy pathRemoveDeadInstanceOfAssertRector.php
More file actions
175 lines (147 loc) · 4.43 KB
/
Copy pathRemoveDeadInstanceOfAssertRector.php
File metadata and controls
175 lines (147 loc) · 4.43 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\StmtsAwareInterface;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Enum\ObjectReference;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveDeadInstanceOfAssertRector\RemoveDeadInstanceOfAssertRectorTest
*/
final class RemoveDeadInstanceOfAssertRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove assert() with instanceof check on a value whose type is already known',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function __construct(
private UserRepository $userRepository
) {
}
public function run(): void
{
assert($this->userRepository instanceof UserRepository);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function __construct(
private UserRepository $userRepository
) {
}
public function run(): void
{
}
}
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}
/**
* @param StmtsAware $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}
$hasChanged = false;
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
if (! $this->isDeadInstanceOfAssert($stmt->expr)) {
continue;
}
$docComment = $stmt->getDocComment();
if ($docComment instanceof Doc) {
$nop = new Nop();
$nop->setDocComment($docComment);
$node->stmts[$key] = $nop;
} else {
unset($node->stmts[$key]);
}
$hasChanged = true;
}
if (! $hasChanged) {
return null;
}
$node->stmts = array_values($node->stmts);
return $node;
}
private function isDeadInstanceOfAssert(Expr $expr): bool
{
if (! $expr instanceof FuncCall) {
return false;
}
if ($expr->isFirstClassCallable()) {
return false;
}
if (! $this->isName($expr, 'assert')) {
return false;
}
// single-arg assert only; keep asserts that carry a description message
if (count($expr->getArgs()) !== 1) {
return false;
}
$instanceof = $expr->getArgs()[0]
->value;
if (! $instanceof instanceof Instanceof_) {
return false;
}
// only property fetch or variable value
if (! $instanceof->expr instanceof PropertyFetch && ! $instanceof->expr instanceof Variable) {
return false;
}
if (! $instanceof->class instanceof Name) {
return false;
}
$classType = $this->resolveClassNameType($instanceof->class);
$exprType = $this->getType($instanceof->expr);
if ($classType->equals($exprType)) {
return true;
}
return $classType->isSuperTypeOf($exprType)
->yes();
}
private function resolveClassNameType(Name $name): Type
{
// a concrete class name is resolvable straight from the node, no type inference needed;
// self/static/parent depend on the enclosing class, so fall back to scope-based type
$className = $this->getName($name);
if ($className === null) {
return $this->getType($name);
}
$loweredClassName = strtolower($className);
if (in_array($loweredClassName, [ObjectReference::SELF, ObjectReference::STATIC, ObjectReference::PARENT], true)) {
return $this->getType($name);
}
return new ObjectType($className);
}
}