Repository navigation
Expand file tree
/
Copy pathByRefNodeVisitor.php
More file actions
160 lines (132 loc) · 4.66 KB
/
Copy pathByRefNodeVisitor.php
File metadata and controls
160 lines (132 loc) · 4.66 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
<?php
declare(strict_types=1);
namespace Rector\PhpParser\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
/**
* Marks by-ref returns IS_INSIDE_BYREF_FUNCTION_LIKE and by-ref variables
* (IS_BYREF_VAR) from a single FunctionLike entry, sharing the one node subscription.
*/
final class ByRefNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
/**
* By-ref variable names per entered function-like, tracked during the main traversal,
* instead of traversing the function-like stmts again on every enter
*
* @var array<string[]>
*/
private array $byRefVariableNamesStack = [];
/**
* @param Node[] $nodes
*/
public function beforeTraverse(array $nodes): null
{
$this->byRefVariableNamesStack = [];
return null;
}
public function enterNode(Node $node): ?Node
{
if ($node instanceof AssignRef) {
$node->expr->setAttribute(AttributeKey::IS_BYREF_VAR, true);
return null;
}
if ($node instanceof Variable) {
$this->decorateByRefVariable($node);
return null;
}
if (! $node instanceof FunctionLike) {
return null;
}
$stmts = $node->getStmts();
if ($stmts === null) {
// keep the stack in sync with leaveNode()
$this->byRefVariableNamesStack[] = [];
return null;
}
$this->decorateByRefReturn($node, $stmts);
// closure by-ref use is a by-ref variable in the outer function-likes as well
$closureUseVariableNames = $this->resolveClosureUseIsByRefAttribute($node);
if ($closureUseVariableNames !== []) {
foreach ($this->byRefVariableNamesStack as $key => $byRefVariableNames) {
$this->byRefVariableNamesStack[$key] = [...$byRefVariableNames, ...$closureUseVariableNames];
}
}
$this->byRefVariableNamesStack[] = [
...$closureUseVariableNames,
...$this->resolveParamIsByRefAttribute($node),
];
return null;
}
public function leaveNode(Node $node): null
{
if ($node instanceof FunctionLike) {
array_pop($this->byRefVariableNamesStack);
}
return null;
}
/**
* @param Node\Stmt[] $stmts
*/
private function decorateByRefReturn(FunctionLike $functionLike, array $stmts): void
{
if (! $functionLike->returnsByRef()) {
return;
}
SimpleNodeTraverser::decorateWithAttributeValue($stmts, AttributeKey::IS_INSIDE_BYREF_FUNCTION_LIKE, true);
}
private function decorateByRefVariable(Variable $variable): void
{
if (! is_string($variable->name)) {
return;
}
foreach ($this->byRefVariableNamesStack as $byRefVariableNames) {
if (in_array($variable->name, $byRefVariableNames, true)) {
$variable->setAttribute(AttributeKey::IS_BYREF_VAR, true);
return;
}
}
}
/**
* @return string[]
*/
private function resolveParamIsByRefAttribute(FunctionLike $functionLike): array
{
$byRefVariableNames = [];
foreach ($functionLike->getParams() as $param) {
if ($param->byRef && $param->var instanceof Variable && ! $param->var->name instanceof Expr) {
$param->var->setAttribute(AttributeKey::IS_BYREF_VAR, true);
/** @var string $paramVarName */
$paramVarName = $param->var->name;
$byRefVariableNames[] = $paramVarName;
}
}
return $byRefVariableNames;
}
/**
* @return string[]
*/
private function resolveClosureUseIsByRefAttribute(FunctionLike $functionLike): array
{
if (! $functionLike instanceof Closure) {
return [];
}
$byRefVariableNames = [];
foreach ($functionLike->uses as $closureUse) {
if ($closureUse->byRef && ! $closureUse->var->name instanceof Expr) {
$closureUse->var->setAttribute(AttributeKey::IS_BYREF_VAR, true);
/** @var string $closureVarName */
$closureVarName = $closureUse->var->name;
$byRefVariableNames[] = $closureVarName;
}
}
return $byRefVariableNames;
}
}