Repository navigation
Expand file tree
/
Copy pathSimplifyUselessVariableRector.php
More file actions
193 lines (159 loc) · 4.95 KB
/
Copy pathSimplifyUselessVariableRector.php
File metadata and controls
193 lines (159 loc) · 4.95 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\FunctionLike;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeAnalyzer\CallAnalyzer;
use Rector\NodeAnalyzer\VariableAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector\SimplifyUselessVariableRectorTest
*/
final class SimplifyUselessVariableRector extends AbstractRector
{
public function __construct(
private readonly VariableAnalyzer $variableAnalyzer,
private readonly CallAnalyzer $callAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove useless variable assigns', [
new CodeSample(
<<<'CODE_SAMPLE'
function () {
$a = true;
return $a;
};
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function () {
return true;
};
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}
/**
* @param StmtsAware $node
*/
public function refactor(Node $node): ?Node
{
$stmts = $node->stmts;
if ($stmts === null) {
return null;
}
foreach ($stmts as $key => $stmt) {
// has previous node?
if (! isset($stmts[$key - 1])) {
continue;
}
if (! $stmt instanceof Return_) {
continue;
}
$previousStmt = $stmts[$key - 1];
if ($this->shouldSkipStmt($stmt, $previousStmt)) {
return null;
}
if ($this->hasSomeComment($previousStmt)) {
return null;
}
// the variable might be used in commented-out code between assign and return
if ($this->isVariableMentionedInComment($stmt)) {
return null;
}
if ($this->isReturnWithVarAnnotation($stmt)) {
return null;
}
if (! $previousStmt instanceof Expression) {
return null;
}
$assign = $previousStmt->expr;
if (! $assign instanceof Assign) {
return null;
}
$stmt->expr = $assign->expr;
unset($node->stmts[$key - 1]);
return $node;
}
return null;
}
private function shouldSkipStmt(Return_ $return, Stmt $previousStmt): bool
{
if (! $return->expr instanceof Variable) {
return true;
}
if ($return->getAttribute(AttributeKey::IS_INSIDE_BYREF_FUNCTION_LIKE) === true) {
return true;
}
if (! $previousStmt instanceof Expression) {
return true;
}
// is variable part of single assign
$previousNode = $previousStmt->expr;
if (! $previousNode instanceof Assign) {
return true;
}
$variable = $return->expr;
// is the same variable
if (! $this->nodeComparator->areNodesEqual($previousNode->var, $variable)) {
return true;
}
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
return true;
}
/** @var Variable $previousVar */
$previousVar = $previousNode->var;
if ($this->callAnalyzer->isNewInstance($previousVar)) {
return true;
}
return $this->variableAnalyzer->isUsedByReference($variable);
}
private function isVariableMentionedInComment(Return_ $return): bool
{
$comments = $return->getComments();
if ($comments === []) {
return false;
}
if (! $return->expr instanceof Variable) {
return false;
}
$variableName = $return->expr->name;
if (! is_string($variableName)) {
return false;
}
return array_any($comments, fn ($comment): bool => str_contains($comment->getText(), '$' . $variableName));
}
private function hasSomeComment(Stmt $stmt): bool
{
if ($stmt->getComments() !== []) {
return true;
}
return $stmt->getDocComment() instanceof Doc;
}
private function isReturnWithVarAnnotation(Return_ $return): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($return);
return ! $phpDocInfo->getVarType() instanceof MixedType;
}
}