forked from driftingly/rector-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonCallToExplicitJsonCallRector.php
156 lines (123 loc) · 4.43 KB
/
JsonCallToExplicitJsonCallRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\MethodCall\JsonCallToExplicitJsonCallRector\JsonCallToExplicitJsonCallRectorTest
*/
final class JsonCallToExplicitJsonCallRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change method calls from $this->json to $this->postJson, $this->putJson, etc.',
[
new CodeSample(
// code before
'$this->json("POST", "/api/v1/users", $data);',
// code after
'$this->postJson("/api/v1/users", $data);'
),
]
);
}
public function getNodeTypes(): array
{
return [MethodCall::class];
}
public function refactor(Node $node): ?Node
{
if ($node instanceof MethodCall) {
return $this->updateCall($node);
}
return null;
}
private function updateCall(MethodCall $methodCall): ?MethodCall
{
$methodCallName = $this->getName($methodCall->name);
if ($methodCallName === null) {
return null;
}
if (! $this->isObjectType(
$methodCall->var,
new ObjectType('Illuminate\Foundation\Testing\Concerns\MakesHttpRequests')
)) {
return null;
}
if ($methodCallName !== 'json') {
return null;
}
$methodCallArgs = $methodCall->getArgs();
if (count($methodCallArgs) < 2) {
return null;
}
$firstArg = $methodCallArgs[0]->value;
if (! $firstArg instanceof String_) {
return null;
}
$lowercaseMethodName = strtolower($firstArg->value);
$supportedMethods = ['get', 'post', 'put', 'patch', 'delete', 'options'];
if (! in_array($lowercaseMethodName, $supportedMethods, true)) {
return null;
}
if ($lowercaseMethodName === 'get' && count($methodCallArgs) > 2) {
return $this->refactorGetMethodCall($methodCall);
}
$methodCall->name = $this->getMethodCallName($lowercaseMethodName);
$methodCall->args = array_slice($methodCall->args, 1);
return $methodCall;
}
/**
* Set the $data argument from the json('GET') call (3rd argument)
* as the route() helper second argument
*/
private function refactorGetMethodCall(MethodCall $methodCall): ?MethodCall
{
if (! $this->isUsingChangeableRouteHelper($methodCall)) {
return null;
}
$thirdArg = $methodCall->getArgs()[2];
// If it's a named argument, and it's not $data, we won't refactor
if ($thirdArg->name !== null && ! $this->isName($thirdArg, 'data')) {
return null;
}
/** @var FuncCall $routeHelperCall */
$routeHelperCall = $methodCall->getArgs()[1]->value;
$routeHelperCall->args = [
$routeHelperCall->args[0],
new Arg($thirdArg->value),
];
$methodCall->name = $this->getMethodCallName('get');
$methodCall->args = [new Arg($routeHelperCall)];
return $methodCall;
}
private function isUsingChangeableRouteHelper(MethodCall $methodCall): bool
{
$methodCallArgs = $methodCall->getArgs();
// More than 3 arguments means we loose $headers or $options if we refactor
if (count($methodCallArgs) !== 3) {
return false;
}
$secondArg = $methodCallArgs[1]->value;
if (! ($secondArg instanceof FuncCall && $this->isName($secondArg, 'route'))) {
return false;
}
// If there is more than 1 argument in the route() helper
// we have to take into account merging the $data argument,
// but it's too unpredictable to refactor
return count($secondArg->args) === 1;
}
private function getMethodCallName(string $lowercaseMethodName): Identifier
{
return new Identifier("{$lowercaseMethodName}Json");
}
}