1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\AddNamedArgumentsRector; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PhpParser\Node; |
9
|
|
|
use PhpParser\Node\Expr\FuncCall; |
10
|
|
|
use PhpParser\Node\Expr\MethodCall; |
11
|
|
|
use PhpParser\Node\Expr\New_; |
12
|
|
|
use PhpParser\Node\Expr\StaticCall; |
13
|
|
|
use PhpParser\Node\Identifier; |
14
|
|
|
use PhpParser\Node\Name; |
15
|
|
|
use PHPStan\Reflection\ClassMemberAccessAnswerer; |
|
|
|
|
16
|
|
|
use PHPStan\Reflection\ExtendedParameterReflection; |
|
|
|
|
17
|
|
|
use PHPStan\Reflection\ReflectionProvider; |
|
|
|
|
18
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface; |
19
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey; |
20
|
|
|
use Rector\Rector\AbstractRector; |
21
|
|
|
use Rector\ValueObject\PhpVersion; |
22
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
23
|
|
|
use SavinMikhail\AddNamedArgumentsRector\Config\ConfigStrategy; |
24
|
|
|
use SavinMikhail\AddNamedArgumentsRector\Config\DefaultStrategy; |
|
|
|
|
25
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
26
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
27
|
|
|
use Webmozart\Assert\Assert; |
28
|
|
|
|
29
|
|
|
use function count; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see AddNamedArgumentsRectorTest |
33
|
|
|
*/ |
34
|
|
|
final class AddNamedArgumentsRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface |
35
|
|
|
{ |
36
|
|
|
private string $configStrategy = DefaultStrategy::class; |
37
|
|
|
|
38
|
2 |
|
public function __construct( |
39
|
|
|
private readonly ReflectionProvider $reflectionProvider, |
40
|
2 |
|
) {} |
41
|
|
|
|
42
|
|
|
public function getRuleDefinition(): RuleDefinition |
43
|
|
|
{ |
44
|
|
|
return new RuleDefinition('Convert all arguments to named arguments', codeSamples: [ |
45
|
|
|
new CodeSample( |
46
|
|
|
badCode: '$user->setPassword("123456");', |
47
|
|
|
goodCode: '$user->changePassword(password: "123456");', |
48
|
|
|
), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
8 |
|
public function getNodeTypes(): array |
53
|
|
|
{ |
54
|
8 |
|
return [FuncCall::class, StaticCall::class, MethodCall::class, New_::class]; |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
public function refactor(Node $node): ?Node |
58
|
|
|
{ |
59
|
8 |
|
$parameters = $this->getParameters($node); |
60
|
|
|
|
61
|
8 |
|
if (!$this->configStrategy::shouldApply($node, $parameters)) { |
62
|
1 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @var FuncCall|StaticCall|MethodCall|New_ $node */ |
66
|
8 |
|
$hasChanged = $this->addNamesToArgs($node, $parameters); |
67
|
|
|
|
68
|
8 |
|
return $hasChanged ? $node : null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ExtendedParameterReflection[] |
73
|
|
|
*/ |
74
|
8 |
|
private function getParameters(Node $node): array |
75
|
|
|
{ |
76
|
8 |
|
$parameters = []; |
77
|
|
|
|
78
|
8 |
|
if ($node instanceof New_) { |
79
|
2 |
|
$parameters = $this->getConstructorArgs($node); |
80
|
7 |
|
} elseif ($node instanceof MethodCall) { |
81
|
1 |
|
$parameters = $this->getMethodArgs($node); |
82
|
6 |
|
} elseif ($node instanceof StaticCall) { |
83
|
1 |
|
$parameters = $this->getStaticMethodArgs($node); |
84
|
5 |
|
} elseif ($node instanceof FuncCall) { |
85
|
5 |
|
$parameters = $this->getFuncArgs($node); |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
return $parameters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return ExtendedParameterReflection[] |
93
|
|
|
*/ |
94
|
1 |
|
private function getStaticMethodArgs(StaticCall $node): array |
95
|
|
|
{ |
96
|
1 |
|
if (! $node->class instanceof Name) { |
97
|
|
|
return []; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$className = $this->getName($node->class); |
101
|
1 |
|
if (! $this->reflectionProvider->hasClass($className)) { |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$classReflection = $this->reflectionProvider->getClass($className); |
106
|
|
|
|
107
|
1 |
|
if ($node->name instanceof Identifier) { |
108
|
1 |
|
$methodName = $node->name->name; |
109
|
|
|
} elseif ($node->name instanceof Name) { |
|
|
|
|
110
|
|
|
$methodName = (string) $node->name; |
111
|
|
|
} else { |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if (! $classReflection->hasMethod($methodName)) { |
116
|
|
|
return []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @var ClassMemberAccessAnswerer $scope */ |
120
|
1 |
|
$scope = $node->getAttribute(AttributeKey::SCOPE); |
121
|
1 |
|
$methodReflection = $classReflection->getMethod($methodName, $scope); |
122
|
|
|
|
123
|
1 |
|
return $methodReflection->getOnlyVariant() |
124
|
1 |
|
->getParameters(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return ExtendedParameterReflection[] |
129
|
|
|
*/ |
130
|
1 |
|
private function getMethodArgs(MethodCall $node): array |
131
|
|
|
{ |
132
|
1 |
|
$callerType = $this->nodeTypeResolver->getType($node->var); |
133
|
1 |
|
$name = $node->name; |
134
|
1 |
|
if ($name instanceof Node\Expr) { |
135
|
|
|
return []; |
136
|
|
|
} |
137
|
1 |
|
$methodName = $name->name; |
138
|
|
|
|
139
|
1 |
|
if (! $callerType->hasMethod($methodName)->yes()) { |
140
|
|
|
return []; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** @var ClassMemberAccessAnswerer $scope */ |
144
|
1 |
|
$scope = $node->getAttribute(AttributeKey::SCOPE); |
145
|
1 |
|
$methodReflection = $callerType->getMethod($methodName, $scope); |
146
|
|
|
|
147
|
1 |
|
return $methodReflection->getOnlyVariant() |
148
|
1 |
|
->getParameters(); |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
private function resolveCalledName(Node $node): ?string |
152
|
|
|
{ |
153
|
7 |
|
if ($node instanceof FuncCall && $node->name instanceof Name) { |
154
|
5 |
|
return (string) $node->name; |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
if ($node instanceof MethodCall && $node->name instanceof Identifier) { |
158
|
|
|
return (string) $node->name; |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
if ($node instanceof StaticCall && $node->name instanceof Identifier) { |
162
|
|
|
return (string) $node->name; |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
if ($node instanceof New_ && $node->class instanceof Name) { |
166
|
2 |
|
return (string) $node->class; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return ExtendedParameterReflection[] |
174
|
|
|
*/ |
175
|
2 |
|
private function getConstructorArgs(New_ $node): array |
176
|
|
|
{ |
177
|
2 |
|
$calledName = $this->resolveCalledName($node); |
178
|
2 |
|
if ($calledName === null) { |
179
|
|
|
return []; |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
if (! $this->reflectionProvider->hasClass($calledName)) { |
183
|
|
|
return []; |
184
|
|
|
} |
185
|
2 |
|
$classReflection = $this->reflectionProvider->getClass($calledName); |
186
|
|
|
|
187
|
2 |
|
if (! $classReflection->hasConstructor()) { |
188
|
|
|
return []; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
$constructorReflection = $classReflection->getConstructor(); |
192
|
|
|
|
193
|
2 |
|
return $constructorReflection->getOnlyVariant() |
194
|
2 |
|
->getParameters(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return ExtendedParameterReflection[] |
199
|
|
|
*/ |
200
|
5 |
|
private function getFuncArgs(FuncCall $node): array |
201
|
|
|
{ |
202
|
5 |
|
$calledName = $this->resolveCalledName($node); |
203
|
5 |
|
if ($calledName === null) { |
204
|
|
|
return []; |
205
|
|
|
} |
206
|
|
|
|
207
|
5 |
|
$scope = $node->getAttribute(AttributeKey::SCOPE); |
208
|
|
|
|
209
|
5 |
|
if (! $this->reflectionProvider->hasFunction(new Name($calledName), $scope)) { |
210
|
|
|
return []; |
211
|
|
|
} |
212
|
5 |
|
$reflection = $this->reflectionProvider->getFunction(new Name($calledName), $scope); |
213
|
|
|
|
214
|
5 |
|
return $reflection |
215
|
5 |
|
->getOnlyVariant() |
216
|
5 |
|
->getParameters(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param ExtendedParameterReflection[] $parameters |
221
|
|
|
*/ |
222
|
8 |
|
private function addNamesToArgs( |
223
|
|
|
FuncCall|StaticCall|MethodCall|New_ $node, |
224
|
|
|
array $parameters, |
225
|
|
|
): bool { |
226
|
8 |
|
$argNames = []; |
227
|
8 |
|
foreach ($node->args as $index => $arg) { |
228
|
8 |
|
if (! isset($parameters[$index])) { |
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Skip variadic parameters (...$param) |
233
|
8 |
|
if ($parameters[$index]->isVariadic()) { |
234
|
1 |
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Skip unpacking arguments (...$var) |
238
|
8 |
|
if ($arg instanceof Node\Arg && $arg->unpack) { |
239
|
1 |
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
7 |
|
if ($arg instanceof Node\VariadicPlaceholder) { |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
7 |
|
$argNames[$index] = new Identifier($parameters[$index]->getName()); |
247
|
|
|
} |
248
|
|
|
|
249
|
6 |
|
foreach ($node->args as $index => $arg) { |
250
|
6 |
|
$arg->name = $argNames[$index]; |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
6 |
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
public function provideMinPhpVersion(): int |
257
|
|
|
{ |
258
|
2 |
|
return PhpVersion::PHP_80; |
259
|
|
|
} |
260
|
|
|
|
261
|
1 |
|
public function configure(array $configuration): void |
262
|
|
|
{ |
263
|
1 |
|
Assert::lessThan(count($configuration), 2, 'You can pass only 1 strategy'); |
264
|
1 |
|
if ($configuration === []) { |
265
|
|
|
return; |
266
|
|
|
} |
267
|
1 |
|
$strategyClass = $configuration[0]; |
268
|
|
|
|
269
|
1 |
|
if (!class_exists($strategyClass)) { |
270
|
|
|
throw new InvalidArgumentException("Class {$strategyClass} does not exist."); |
271
|
|
|
} |
272
|
|
|
|
273
|
1 |
|
$strategy = new $strategyClass(); |
274
|
|
|
|
275
|
1 |
|
Assert::isInstanceOf($strategy, ConfigStrategy::class, 'Your strategy must implement ConfigStrategy interface'); |
276
|
|
|
|
277
|
1 |
|
$this->configStrategy = $strategyClass; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths