-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathRequestVariablesToRequestFacadeRector.php
143 lines (121 loc) · 4.07 KB
/
RequestVariablesToRequestFacadeRector.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
<?php
namespace RectorLaravel\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitor;
use RectorLaravel\AbstractRector;
use RectorLaravel\ValueObject\ReplaceRequestKeyAndMethodValue;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\RequestVariablesToRequestFacadeRectorTest
*/
class RequestVariablesToRequestFacadeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change request variable definition in Facade',
[
new CodeSample(
<<<'CODE_SAMPLE'
$_GET['value'];
$_POST['value'];
$_REQUEST['value'];
$_POST;
$_GET;
$_REQUEST;
CODE_SAMPLE,
<<<'CODE_SAMPLE'
\Illuminate\Support\Facades\Request::query('value');
\Illuminate\Support\Facades\Request::post('value');
\Illuminate\Support\Facades\Request::input('value');
\Illuminate\Support\Facades\Request::query();
\Illuminate\Support\Facades\Request::post();
\Illuminate\Support\Facades\Request::all();
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [ArrayDimFetch::class, Variable::class];
}
/**
* @param ArrayDimFetch|Variable $node
* @return StaticCall|1|null
*/
public function refactor(Node $node): StaticCall|int|null
{
if ($node instanceof Variable) {
return $this->processVariable($node);
}
$replaceValue = $this->findAllKeys($node);
if ($replaceValue instanceof ReplaceRequestKeyAndMethodValue) {
return $this->nodeFactory->createStaticCall(
'Illuminate\Support\Facades\Request',
$replaceValue->getMethod(),
[new Arg(new String_($replaceValue->getKey()))]
);
}
return $replaceValue;
}
/**
* @return ReplaceRequestKeyAndMethodValue|1|null
*/
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ReplaceRequestKeyAndMethodValue|int|null
{
if (! $arrayDimFetch->dim instanceof Scalar) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
$value = $this->getType($arrayDimFetch->dim)->getConstantScalarValues()[0] ?? null;
if ($value === null) {
return null;
}
if ($arrayDimFetch->var instanceof ArrayDimFetch) {
$replaceValue = $this->findAllKeys($arrayDimFetch->var);
if (! $replaceValue instanceof ReplaceRequestKeyAndMethodValue) {
return $replaceValue;
}
return new ReplaceRequestKeyAndMethodValue(implode('.', [$replaceValue->getKey(), $value]), $replaceValue->getMethod());
}
if ($this->isNames($arrayDimFetch->var, ['_GET', '_POST', '_REQUEST'])) {
if (! $arrayDimFetch->var instanceof Variable) {
return null;
}
$method = match ($arrayDimFetch->var->name) {
'_GET' => 'query',
'_POST' => 'post',
'_REQUEST' => 'input',
default => null,
};
if ($method === null) {
return null;
}
return new ReplaceRequestKeyAndMethodValue((string) $value, $method);
}
return null;
}
private function processVariable(Variable $variable): ?StaticCall
{
$method = match ($variable->name) {
'_GET' => 'query',
'_POST' => 'post',
'_REQUEST' => 'all',
default => null,
};
if ($method === null) {
return null;
}
return $this->nodeFactory->createStaticCall(
'Illuminate\Support\Facades\Request',
$method,
);
}
}