forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgumentAdderRector.php
268 lines (231 loc) · 7.66 KB
/
ArgumentAdderRector.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
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace Rector\Arguments\Rector\ClassMethod;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\ArgumentAdderRectorTest
*/
final class ArgumentAdderRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const ADDED_ARGUMENTS = 'added_arguments';
/**
* @var ArgumentAdder[]
*/
private $addedArguments = [];
/**
* @var ArgumentAddingScope
*/
private $argumentAddingScope;
public function __construct(ArgumentAddingScope $argumentAddingScope)
{
$this->argumentAddingScope = $argumentAddingScope;
}
public function getRuleDefinition(): RuleDefinition
{
$exampleConfiguration = [
self::ADDED_ARGUMENTS => [
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', 'true', 'SomeType'),
],
];
return new RuleDefinition(
'This Rector adds new default arguments in calls of defined methods and class types.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->someMethod();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->someMethod(true);
CODE_SAMPLE
,
$exampleConfiguration
),
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class MyCustomClass extends SomeExampleClass
{
public function someMethod()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class MyCustomClass extends SomeExampleClass
{
public function someMethod($value = true)
{
}
}
CODE_SAMPLE
,
$exampleConfiguration
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->addedArguments as $addedArgument) {
if (! $this->isObjectTypeMatch($node, $addedArgument->getObjectType())) {
continue;
}
if (! $this->isName($node->name, $addedArgument->getMethod())) {
continue;
}
$this->processPositionWithDefaultValues($node, $addedArgument);
}
return $node;
}
/**
* @param array<string, ArgumentAdder[]> $configuration
*/
public function configure(array $configuration): void
{
$addedArguments = $configuration[self::ADDED_ARGUMENTS] ?? [];
Assert::allIsInstanceOf($addedArguments, ArgumentAdder::class);
$this->addedArguments = $addedArguments;
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
private function isObjectTypeMatch(Node $node, ObjectType $objectType): bool
{
if ($node instanceof MethodCall) {
return $this->isObjectType($node->var, $objectType);
}
if ($node instanceof StaticCall) {
return $this->isObjectType($node->class, $objectType);
}
// ClassMethod
/** @var Class_|null $classLike */
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
// anonymous class
if (! $classLike instanceof Class_) {
return false;
}
return $this->isObjectType($classLike, $objectType);
}
/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
private function processPositionWithDefaultValues(Node $node, ArgumentAdder $argumentAdder): void
{
if ($this->shouldSkipParameter($node, $argumentAdder)) {
return;
}
$defaultValue = $argumentAdder->getArgumentDefaultValue();
$argumentType = $argumentAdder->getArgumentType();
$position = $argumentAdder->getPosition();
if ($node instanceof ClassMethod) {
$this->addClassMethodParam($node, $argumentAdder, $defaultValue, $argumentType, $position);
} elseif ($node instanceof StaticCall) {
$this->processStaticCall($node, $position, $argumentAdder);
} else {
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
if (isset($node->args[$position])) {
return;
}
$node->args[$position] = $arg;
}
}
/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
private function shouldSkipParameter(Node $node, ArgumentAdder $argumentAdder): bool
{
$position = $argumentAdder->getPosition();
$argumentName = $argumentAdder->getArgumentName();
if ($argumentName === null) {
return true;
}
if ($node instanceof ClassMethod) {
// already added?
if (! isset($node->params[$position])) {
return false;
}
return $this->isName($node->params[$position], $argumentName);
}
// already added?
if (! isset($node->args[$position])) {
// is correct scope?
return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
if (! $this->isName($node->args[$position], $argumentName)) {
// is correct scope?
return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
return true;
}
/**
* @param mixed $defaultValue
*/
private function addClassMethodParam(
ClassMethod $classMethod,
ArgumentAdder $argumentAdder,
$defaultValue,
?string $type,
int $position
): void {
$argumentName = $argumentAdder->getArgumentName();
if ($argumentName === null) {
throw new ShouldNotHappenException();
}
$param = new Param(new Variable($argumentName), BuilderHelpers::normalizeValue($defaultValue));
if ($type) {
$param->type = ctype_upper($type[0]) ? new FullyQualified($type) : new Identifier($type);
}
$classMethod->params[$position] = $param;
}
private function processStaticCall(StaticCall $staticCall, int $position, ArgumentAdder $argumentAdder): void
{
$argumentName = $argumentAdder->getArgumentName();
if ($argumentName === null) {
throw new ShouldNotHappenException();
}
if (! $staticCall->class instanceof Name) {
return;
}
if (! $this->isName($staticCall->class, 'parent')) {
return;
}
$staticCall->args[$position] = new Arg(new Variable($argumentName));
}
}