-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathArgumentFuncCallToMethodCallRector.php
219 lines (184 loc) · 6.94 KB
/
ArgumentFuncCallToMethodCallRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\Transform\NodeAnalyzer\FuncCallStaticCallToMethodCallAnalyzer;
use RectorLaravel\AbstractRector;
use RectorLaravel\Contract\ValueObject\ArgumentFuncCallToMethodCallInterface;
use RectorLaravel\ValueObject\ArgumentFuncCallToMethodCall;
use RectorLaravel\ValueObject\ArrayFuncCallToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \RectorLaravel\Tests\Rector\FuncCall\ArgumentFuncCallToMethodCallRector\ArgumentFuncCallToMethodCallRectorTest
*/
final class ArgumentFuncCallToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var ArgumentFuncCallToMethodCallInterface[]
*/
private array $argumentFuncCallToMethodCalls = [];
public function __construct(
private readonly ArrayTypeAnalyzer $arrayTypeAnalyzer,
private readonly FuncCallStaticCallToMethodCallAnalyzer $funcCallStaticCallToMethodCallAnalyzer
) {}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Move help facade-like function calls to constructor injection', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeController
{
public function action()
{
$template = view('template.blade');
$viewFactory = view();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeController
{
/**
* @var \Illuminate\Contracts\View\Factory
*/
private $viewFactory;
public function __construct(\Illuminate\Contracts\View\Factory $viewFactory)
{
$this->viewFactory = $viewFactory;
}
public function action()
{
$template = $this->viewFactory->make('template.blade');
$viewFactory = $this->viewFactory;
}
}
CODE_SAMPLE
,
[new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make')]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;
$class = $node;
foreach ($node->getMethods() as $classMethod) {
if ($classMethod->isStatic()) {
continue;
}
if ($classMethod->isAbstract()) {
continue;
}
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
$class,
$classMethod,
&$hasChanged
): ?Node {
if (! $node instanceof FuncCall) {
return null;
}
foreach ($this->argumentFuncCallToMethodCalls as $argumentFuncCallToMethodCall) {
if (! $this->isName($node->name, $argumentFuncCallToMethodCall->getFunction())) {
continue;
}
if ($argumentFuncCallToMethodCall instanceof ArgumentFuncCallToMethodCall) {
$expr = $this->funcCallStaticCallToMethodCallAnalyzer->matchTypeProvidingExpr(
$class,
$classMethod,
new ObjectType($argumentFuncCallToMethodCall->getClass()),
);
$hasChanged = true;
return $this->refactorFuncCallToMethodCall($node, $argumentFuncCallToMethodCall, $expr);
}
if ($argumentFuncCallToMethodCall instanceof ArrayFuncCallToMethodCall) {
$expr = $this->funcCallStaticCallToMethodCallAnalyzer->matchTypeProvidingExpr(
$class,
$classMethod,
new ObjectType($argumentFuncCallToMethodCall->getClass()),
);
$hasChanged = true;
return $this->refactorArrayFunctionToMethodCall(
$node,
$argumentFuncCallToMethodCall,
$expr
);
}
}
return null;
});
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsInstanceOf($configuration, ArgumentFuncCallToMethodCallInterface::class);
$this->argumentFuncCallToMethodCalls = $configuration;
}
public function refactorFuncCallToMethodCall(
FuncCall $funcCall,
ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall,
MethodCall|PropertyFetch|Variable $expr
): MethodCall|PropertyFetch|Variable {
if ($funcCall->args === []) {
return $this->refactorEmptyFuncCallArgs($argumentFuncCallToMethodCall, $expr);
}
$methodIfArgs = $argumentFuncCallToMethodCall->getMethodIfArgs();
if (! is_string($methodIfArgs)) {
throw new ShouldNotHappenException;
}
return $this->nodeFactory->createMethodCall($expr, $methodIfArgs, $funcCall->args);
}
private function refactorArrayFunctionToMethodCall(
FuncCall $funcCall,
ArrayFuncCallToMethodCall $arrayFuncCallToMethodCall,
MethodCall|PropertyFetch|Variable $expr
): ?Node {
if ($funcCall->getArgs() === []) {
return $expr;
}
if ($this->arrayTypeAnalyzer->isArrayType($funcCall->getArgs()[0]->value)) {
return new MethodCall($expr, $arrayFuncCallToMethodCall->getArrayMethod(), $funcCall->getArgs());
}
if ($arrayFuncCallToMethodCall->getNonArrayMethod() === '') {
return null;
}
return new MethodCall($expr, $arrayFuncCallToMethodCall->getNonArrayMethod(), $funcCall->getArgs());
}
private function refactorEmptyFuncCallArgs(
ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall,
MethodCall|PropertyFetch|Variable $expr
): MethodCall|PropertyFetch|Variable {
if ($argumentFuncCallToMethodCall->getMethodIfNoArgs() !== null) {
return $this->nodeFactory->createMethodCall($expr, $argumentFuncCallToMethodCall->getMethodIfNoArgs());
}
return $expr;
}
}