forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseStatusCodeRector.php
More file actions
224 lines (185 loc) · 6.29 KB
/
ResponseStatusCodeRector.php
File metadata and controls
224 lines (185 loc) · 6.29 KB
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
220
221
222
223
224
<?php
declare(strict_types=1);
namespace Rector\Symfony\CodeQuality\Rector\BinaryOp;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\Symfony\NodeAnalyzer\LiteralCallLikeConstFetchReplacer;
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
use Rector\Symfony\ValueObject\ConstantMap\SymfonyResponseConstantMap;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\CodeQuality\Rector\BinaryOp\ResponseStatusCodeRector\ResponseStatusCodeRectorTest
*/
final class ResponseStatusCodeRector extends AbstractRector
{
private readonly ObjectType $responseObjectType;
public function __construct(
private readonly ControllerAnalyzer $controllerAnalyzer,
private readonly LiteralCallLikeConstFetchReplacer $literalCallLikeConstFetchReplacer
) {
$this->responseObjectType = new ObjectType('Symfony\Component\HttpFoundation\Response');
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns status code numbers to constants', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Response;
class SomeController
{
public function index()
{
$response = new Response();
$response->setStatusCode(200);
if ($response->getStatusCode() === 200) {
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Response;
class SomeController
{
public function index()
{
$response = new Response();
$response->setStatusCode(Response::HTTP_OK);
if ($response->getStatusCode() === Response::HTTP_OK) {
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BinaryOp::class, MethodCall::class, New_::class];
}
/**
* @param BinaryOp|MethodCall|New_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof New_) {
return $this->processNew($node);
}
if ($node instanceof MethodCall) {
return $this->processMethodCall($node);
}
return $this->processBinaryOp($node);
}
private function processMethodCall(MethodCall $methodCall): CallLike|null
{
$methodCallName = $this->nodeNameResolver->getName($methodCall->name);
if (! is_string($methodCallName)) {
return null;
}
if (str_starts_with($methodCallName, 'assert')) {
return $this->processAssertMethodCall($methodCall);
}
if ($methodCallName === 'redirect') {
return $this->processRedirectMethodCall($methodCall);
}
if ($methodCallName !== 'setStatusCode') {
return null;
}
if (! $this->isObjectType($methodCall->var, $this->responseObjectType)) {
return null;
}
return $this->literalCallLikeConstFetchReplacer->replaceArgOnPosition(
$methodCall,
0,
'Symfony\Component\HttpFoundation\Response',
SymfonyResponseConstantMap::CODE_TO_CONST
);
}
private function processBinaryOp(BinaryOp $binaryOp): ?BinaryOp
{
if (! $this->isGetStatusMethod($binaryOp->left) && ! $this->isGetStatusMethod($binaryOp->right)) {
return null;
}
if ($binaryOp->right instanceof LNumber && $this->isGetStatusMethod($binaryOp->left)) {
$binaryOp->right = $this->convertNumberToConstant($binaryOp->right);
return $binaryOp;
}
if ($binaryOp->left instanceof LNumber && $this->isGetStatusMethod($binaryOp->right)) {
$binaryOp->left = $this->convertNumberToConstant($binaryOp->left);
return $binaryOp;
}
return null;
}
private function isGetStatusMethod(Node $node): bool
{
if (! $node instanceof MethodCall) {
return false;
}
if (! $this->isName($node->name, 'getStatusCode')) {
return false;
}
return $this->isObjectType($node->var, $this->responseObjectType);
}
private function convertNumberToConstant(LNumber $lNumber): ClassConstFetch|LNumber
{
if (! isset(SymfonyResponseConstantMap::CODE_TO_CONST[$lNumber->value])) {
return $lNumber;
}
return $this->nodeFactory->createClassConstFetch(
$this->responseObjectType->getClassName(),
SymfonyResponseConstantMap::CODE_TO_CONST[$lNumber->value]
);
}
private function processAssertMethodCall(MethodCall $methodCall): MethodCall|null
{
$args = $methodCall->getArgs();
if (! isset($args[1])) {
return null;
}
$secondArg = $args[1];
if (! $this->isGetStatusMethod($secondArg->value)) {
return null;
}
return $this->literalCallLikeConstFetchReplacer->replaceArgOnPosition(
$methodCall,
0,
'Symfony\Component\HttpFoundation\Response',
SymfonyResponseConstantMap::CODE_TO_CONST
);
}
private function processRedirectMethodCall(MethodCall $methodCall): MethodCall|null
{
if (! $this->controllerAnalyzer->isController($methodCall->var)) {
return null;
}
return $this->literalCallLikeConstFetchReplacer->replaceArgOnPosition(
$methodCall,
1,
'Symfony\Component\HttpFoundation\Response',
SymfonyResponseConstantMap::CODE_TO_CONST
);
}
private function processNew(New_ $new): New_|null
{
if (! $this->isObjectType($new->class, $this->responseObjectType)) {
return null;
}
return $this->literalCallLikeConstFetchReplacer->replaceArgOnPosition(
$new,
1,
'Symfony\Component\HttpFoundation\Response',
SymfonyResponseConstantMap::CODE_TO_CONST
);
}
}