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 PHPStan\Reflection\ExtendedParameterReflection; |
|
|
|
|
15
|
|
|
use PHPStan\Reflection\ReflectionProvider; |
|
|
|
|
16
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface; |
17
|
|
|
use Rector\NodeNameResolver\NodeNameResolver; |
18
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver; |
19
|
|
|
use Rector\Rector\AbstractRector; |
20
|
|
|
use Rector\ValueObject\PhpVersion; |
21
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
22
|
|
|
use SavinMikhail\AddNamedArgumentsRector\Config\ConfigStrategy; |
23
|
|
|
use SavinMikhail\AddNamedArgumentsRector\Config\DefaultStrategy; |
|
|
|
|
24
|
|
|
use SavinMikhail\AddNamedArgumentsRector\Reflection\Reflection; |
|
|
|
|
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
|
|
|
private readonly Reflection $reflectionService; |
39
|
|
|
|
40
|
1 |
|
public function __construct( |
41
|
|
|
ReflectionProvider $reflectionProvider, |
42
|
|
|
NodeNameResolver $nodeNameResolver, |
43
|
|
|
NodeTypeResolver $nodeTypeResolver, |
44
|
|
|
?Reflection $reflectionService = null, |
45
|
|
|
) { |
46
|
1 |
|
if ($reflectionService === null) { |
47
|
|
|
$reflectionService = new Reflection( |
48
|
|
|
reflectionProvider: $reflectionProvider, |
49
|
|
|
nodeNameResolver: $nodeNameResolver, |
50
|
|
|
nodeTypeResolver: $nodeTypeResolver, |
51
|
|
|
); |
52
|
|
|
} |
53
|
1 |
|
$this->reflectionService = $reflectionService; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getRuleDefinition(): RuleDefinition |
57
|
|
|
{ |
58
|
|
|
return new RuleDefinition('Convert all arguments to named arguments', codeSamples: [ |
59
|
|
|
new CodeSample( |
60
|
|
|
badCode: '$user->setPassword("123456");', |
61
|
|
|
goodCode: '$user->changePassword(password: "123456");', |
62
|
|
|
), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
public function getNodeTypes(): array |
67
|
|
|
{ |
68
|
10 |
|
return [FuncCall::class, StaticCall::class, MethodCall::class, New_::class]; |
69
|
|
|
} |
70
|
|
|
|
71
|
10 |
|
public function refactor(Node $node): ?Node |
72
|
|
|
{ |
73
|
|
|
/** @var FuncCall|StaticCall|MethodCall|New_ $node */ |
74
|
10 |
|
$parameters = $this->reflectionService->getParameters(node: $node); |
75
|
10 |
|
$classReflection = $this->reflectionService->getClassReflection(node: $node); |
76
|
|
|
|
77
|
10 |
|
if (!$this->configStrategy::shouldApply($node, $parameters, $classReflection)) { |
78
|
6 |
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
$this->addNamesToArgs(node: $node, parameters: $parameters); |
82
|
|
|
|
83
|
4 |
|
return $node; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ExtendedParameterReflection[] $parameters |
88
|
|
|
*/ |
89
|
4 |
|
private function addNamesToArgs( |
90
|
|
|
FuncCall|StaticCall|MethodCall|New_ $node, |
91
|
|
|
array $parameters, |
92
|
|
|
): void { |
93
|
4 |
|
$argNames = []; |
94
|
4 |
|
foreach ($node->args as $index => $arg) { |
95
|
4 |
|
$argNames[$index] = new Identifier(name: $parameters[$index]->getName()); |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
foreach ($node->args as $index => $arg) { |
99
|
4 |
|
$arg->name = $argNames[$index]; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
public function provideMinPhpVersion(): int |
104
|
|
|
{ |
105
|
1 |
|
return PhpVersion::PHP_80; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function configure(array $configuration): void |
109
|
|
|
{ |
110
|
|
|
Assert::lessThan(value: count(value: $configuration), limit: 2, message: 'You can pass only 1 strategy'); |
111
|
|
|
if ($configuration === []) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
$strategyClass = $configuration[0]; |
115
|
|
|
|
116
|
|
|
if (!class_exists(class: $strategyClass)) { |
117
|
|
|
throw new InvalidArgumentException(message: "Class {$strategyClass} does not exist."); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$strategy = new $strategyClass(); |
121
|
|
|
|
122
|
|
|
Assert::isInstanceOf(value: $strategy, class: ConfigStrategy::class, message: 'Your strategy must implement ConfigStrategy interface'); |
123
|
|
|
|
124
|
|
|
$this->configStrategy = $strategyClass; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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