-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
OptionalParametersAfterRequiredRector.php
194 lines (192 loc) · 7.68 KB
/
OptionalParametersAfterRequiredRector.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
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php80\NodeResolver\ArgumentSorter;
use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest
*/
final class OptionalParametersAfterRequiredRector extends AbstractRector
{
/**
* @readonly
*/
private RequireOptionalParamResolver $requireOptionalParamResolver;
/**
* @readonly
*/
private ArgumentSorter $argumentSorter;
/**
* @readonly
*/
private ReflectionResolver $reflectionResolver;
/**
* @readonly
*/
private VendorLocationDetector $vendorLocationDetector;
/**
* @var string
*/
private const HAS_SWAPPED_PARAMS = 'has_swapped_params';
public function __construct(RequireOptionalParamResolver $requireOptionalParamResolver, ArgumentSorter $argumentSorter, ReflectionResolver $reflectionResolver, VendorLocationDetector $vendorLocationDetector)
{
$this->requireOptionalParamResolver = $requireOptionalParamResolver;
$this->argumentSorter = $argumentSorter;
$this->reflectionResolver = $reflectionResolver;
$this->vendorLocationDetector = $vendorLocationDetector;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Move required parameters after optional ones', [new CodeSample(<<<'CODE_SAMPLE'
class SomeObject
{
public function run($optional = 1, $required)
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeObject
{
public function run($required, $optional = 1)
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class, Function_::class, New_::class, MethodCall::class, StaticCall::class, FuncCall::class];
}
/**
* @param ClassMethod|Function_|New_|MethodCall|StaticCall|FuncCall $node
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall
*/
public function refactor(Node $node)
{
$scope = ScopeFetcher::fetch($node);
if ($node instanceof ClassMethod || $node instanceof Function_) {
return $this->refactorClassMethodOrFunction($node, $scope);
}
if ($node instanceof New_) {
return $this->refactorNew($node, $scope);
}
return $this->refactorMethodCallOrFuncCall($node, $scope);
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $node
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
*/
private function refactorClassMethodOrFunction($node, Scope $scope)
{
if ($node->params === []) {
return null;
}
if ($node->getAttribute(self::HAS_SWAPPED_PARAMS, \false) === \true) {
return null;
}
if ($node instanceof ClassMethod) {
$reflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($node, $scope);
} else {
$reflection = $this->reflectionResolver->resolveFunctionReflectionFromFunction($node, $scope);
}
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
return null;
}
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
if ($expectedArgOrParamOrder === null) {
return null;
}
$node->params = $this->argumentSorter->sortArgsByExpectedParamOrder($node->params, $expectedArgOrParamOrder);
$node->setAttribute(self::HAS_SWAPPED_PARAMS, \true);
return $node;
}
private function refactorNew(New_ $new, Scope $scope) : ?New_
{
if ($new->args === []) {
return null;
}
if ($new->isFirstClassCallable()) {
return null;
}
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromNew($new);
if (!$methodReflection instanceof MethodReflection) {
return null;
}
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $new, $scope);
if ($expectedArgOrParamOrder === null) {
return null;
}
$new->args = $this->argumentSorter->sortArgsByExpectedParamOrder($new->getArgs(), $expectedArgOrParamOrder);
return $new;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall|null
*/
private function refactorMethodCallOrFuncCall($node, Scope $scope)
{
if ($node->isFirstClassCallable()) {
return null;
}
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
return null;
}
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
if ($expectedArgOrParamOrder === null) {
return null;
}
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($node->getArgs(), $expectedArgOrParamOrder);
if ($node->args === $newArgs) {
return null;
}
$node->args = $newArgs;
return $node;
}
/**
* @return int[]|null
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
*/
private function resolveExpectedArgParamOrderIfDifferent($reflection, $node, Scope $scope) : ?array
{
if ($reflection instanceof NativeFunctionReflection) {
return null;
}
if ($reflection instanceof MethodReflection && $this->vendorLocationDetector->detectMethodReflection($reflection)) {
return null;
}
if ($reflection instanceof FunctionReflection && $this->vendorLocationDetector->detectFunctionReflection($reflection)) {
return null;
}
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope);
$expectedParameterReflections = $this->requireOptionalParamResolver->resolveFromParametersAcceptor($parametersAcceptor);
if ($expectedParameterReflections === $parametersAcceptor->getParameters()) {
return null;
}
return \array_keys($expectedParameterReflections);
}
}