forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddDefaultValueForUndefinedVariableRector.php
258 lines (219 loc) · 7.3 KB
/
AddDefaultValueForUndefinedVariableRector.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
declare(strict_types=1);
namespace Rector\Php56\Rector\FunctionLike;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\Cast\Unset_ as UnsetCast;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Global_;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\StaticVar;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticNodeInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://github.com/vimeo/psalm/blob/29b70442b11e3e66113935a2ee22e165a70c74a4/docs/fixing_code.md#possiblyundefinedvariable
* @see https://3v4l.org/MZFel
*
* @see \Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\AddDefaultValueForUndefinedVariableRectorTest
*/
final class AddDefaultValueForUndefinedVariableRector extends AbstractRector
{
/**
* @var string[]
*/
private $definedVariables = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Adds default value for undefined variable', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
if (rand(0, 1)) {
$a = 5;
}
echo $a;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$a = null;
if (rand(0, 1)) {
$a = 5;
}
echo $a;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class, Closure::class];
}
/**
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
$this->definedVariables = [];
$undefinedVariables = $this->collectUndefinedVariableScope($node);
if ($undefinedVariables === []) {
return null;
}
$variablesInitiation = [];
foreach ($undefinedVariables as $undefinedVariable) {
if (in_array($undefinedVariable, $this->definedVariables, true)) {
continue;
}
$value = $this->isArray($undefinedVariable, (array) $node->stmts)
? new Array_([])
: $this->nodeFactory->createNull();
$variablesInitiation[] = new Expression(new Assign(new Variable($undefinedVariable), $value));
}
$node->stmts = array_merge($variablesInitiation, (array) $node->stmts);
return $node;
}
/**
* @param ClassMethod|Function_|Closure $node
* @return string[]
*/
private function collectUndefinedVariableScope(Node $node): array
{
$undefinedVariables = [];
$this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (&$undefinedVariables): ?int {
// entering new scope - break!
if ($node instanceof FunctionLike && ! $node instanceof ArrowFunction) {
return NodeTraverser::STOP_TRAVERSAL;
}
if ($node instanceof Foreach_) {
// handled above
$this->collectDefinedVariablesFromForeach($node);
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (! $node instanceof Variable) {
return null;
}
if ($this->shouldSkipVariable($node)) {
return null;
}
/** @var string $variableName */
$variableName = $this->getName($node);
// defined 100 %
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope->hasVariableType($variableName)->yes()) {
return null;
}
$undefinedVariables[] = $variableName;
return null;
});
return array_unique($undefinedVariables);
}
/**
* @param Stmt[] $stmts
*/
private function isArray(string $undefinedVariable, array $stmts): bool
{
return (bool) $this->betterNodeFinder->findFirst($stmts, function (Node $node) use (
$undefinedVariable
): bool {
if (! $node instanceof ArrayDimFetch) {
return false;
}
return $this->isName($node->var, $undefinedVariable);
});
}
private function collectDefinedVariablesFromForeach(Foreach_ $foreach): void
{
$this->traverseNodesWithCallable($foreach->stmts, function (Node $node): void {
if ($node instanceof Assign || $node instanceof AssignRef) {
if (! $node->var instanceof Variable) {
return;
}
$variableName = $this->getName($node->var);
if ($variableName === null) {
return;
}
$this->definedVariables[] = $variableName;
}
});
}
private function shouldSkipVariable(Variable $variable): bool
{
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
return true;
}
if ($parentNode instanceof Global_) {
return true;
}
if ($parentNode instanceof Node &&
($parentNode instanceof Assign || $parentNode instanceof AssignRef || $this->isStaticVariable($parentNode)
)) {
return true;
}
if (StaticNodeInstanceOf::isOneOf($parentNode, [Unset_::class, UnsetCast::class])) {
return true;
}
// list() = | [$values] = defines variables as null
if ($this->isListAssign($parentNode)) {
return true;
}
$nodeScope = $variable->getAttribute(AttributeKey::SCOPE);
if (! $nodeScope instanceof Scope) {
return true;
}
$variableName = $this->getName($variable);
// skip $this, as probably in outer scope
if ($variableName === 'this') {
return true;
}
return $variableName === null;
}
private function isStaticVariable(Node $parentNode): bool
{
// definition of static variable
if ($parentNode instanceof StaticVar) {
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentParentNode instanceof Static_) {
return true;
}
}
return false;
}
private function isListAssign(Node $node): bool
{
$parentParentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
return StaticNodeInstanceOf::isOneOf($parentParentNode, [List_::class, Array_::class]);
}
}