-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathArrayDimFetchToMethodCallRector.php
More file actions
217 lines (180 loc) · 6.05 KB
/
ArrayDimFetchToMethodCallRector.php
File metadata and controls
217 lines (180 loc) · 6.05 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeVisitor;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\ArrayDimFetchToMethodCallRectorTest
*/
final class ArrayDimFetchToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var ArrayDimFetchToMethodCall[]
*/
private array $arrayDimFetchToMethodCalls;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change array dim fetch to method call', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$object['key'];
$object['key'] = 'value';
isset($object['key']);
unset($object['key']);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$object->get('key');
$object->set('key', 'value');
$object->has('key');
$object->unset('key');
CODE_SAMPLE
,
[new ArrayDimFetchToMethodCall(new ObjectType('SomeClass'), 'get', 'set', 'has', 'unset')],
),
]);
}
public function getNodeTypes(): array
{
return [AssignOp::class, Assign::class, Isset_::class, Unset_::class, ArrayDimFetch::class];
}
/**
* @template TNode of ArrayDimFetch|Assign|Isset_|Unset_
* @param TNode $node
* @return ($node is Unset_ ? Stmt[]|int : ($node is Isset_ ? Expr|int : MethodCall|int|null))
*/
public function refactor(Node $node): array|Expr|null|int
{
if ($node instanceof AssignOp) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Unset_) {
return $this->handleUnset($node);
}
if ($node instanceof Isset_) {
return $this->handleIsset($node);
}
if ($node instanceof Assign) {
if (! $node->var instanceof ArrayDimFetch) {
return null;
}
return $this->createExplicitMethodCall($node->var, 'set', $node->expr);
}
// is part of assign, skip
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) {
return null;
}
return $this->createExplicitMethodCall($node, 'get');
}
public function configure(array $configuration): void
{
Assert::allIsInstanceOf($configuration, ArrayDimFetchToMethodCall::class);
$this->arrayDimFetchToMethodCalls = $configuration;
}
private function handleIsset(Isset_ $isset): Expr|int|null
{
$issets = [];
$exprs = [];
foreach ($isset->vars as $var) {
if ($var instanceof ArrayDimFetch) {
$methodCall = $this->createExplicitMethodCall($var, 'exists');
if ($methodCall instanceof MethodCall) {
$exprs[] = $methodCall;
continue;
}
}
$issets[] = $var;
}
if ($exprs === []) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
if ($issets !== []) {
$isset->vars = $issets;
array_unshift($exprs, $isset);
}
return array_reduce(
$exprs,
fn (?Expr $carry, Expr $expr): Isset_|MethodCall|BooleanAnd => $carry instanceof Expr ? new BooleanAnd(
$carry,
$expr
) : $expr,
);
}
/**
* @return Stmt[]|int
*/
private function handleUnset(Unset_ $unset): array|int
{
$unsets = [];
$stmts = [];
foreach ($unset->vars as $var) {
if ($var instanceof ArrayDimFetch) {
$methodCall = $this->createExplicitMethodCall($var, 'unset');
if ($methodCall instanceof MethodCall) {
$stmts[] = new Expression($methodCall);
continue;
}
}
$unsets[] = $var;
}
if ($stmts === []) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
if ($unsets !== []) {
$unset->vars = $unsets;
array_unshift($stmts, $unset);
}
return $stmts;
}
/**
* @param 'get'|'set'|'exists'|'unset' $action
*/
private function createExplicitMethodCall(
ArrayDimFetch $arrayDimFetch,
string $action,
?Expr $expr = null
): ?MethodCall {
if (! $arrayDimFetch->dim instanceof Node) {
return null;
}
foreach ($this->arrayDimFetchToMethodCalls as $arrayDimFetchToMethodCall) {
if (! $this->isObjectType($arrayDimFetch->var, $arrayDimFetchToMethodCall->getObjectType())) {
continue;
}
$method = match ($action) {
'get' => $arrayDimFetchToMethodCall->getMethod(),
'set' => $arrayDimFetchToMethodCall->getSetMethod(),
'exists' => $arrayDimFetchToMethodCall->getExistsMethod(),
'unset' => $arrayDimFetchToMethodCall->getUnsetMethod(),
};
if ($method === null) {
continue;
}
$args = [new Arg($arrayDimFetch->dim)];
if ($expr instanceof Expr) {
$args[] = new Arg($expr);
}
return new MethodCall($arrayDimFetch->var, $method, $args);
}
return null;
}
}