6
6
use PhpParser \Node \Arg ;
7
7
use PhpParser \Node \Expr \ArrayDimFetch ;
8
8
use PhpParser \Node \Expr \StaticCall ;
9
+ use PhpParser \Node \Expr \Variable ;
9
10
use PhpParser \Node \Scalar ;
10
11
use PhpParser \Node \Scalar \String_ ;
12
+ use PhpParser \NodeVisitor ;
11
13
use RectorLaravel \AbstractRector ;
14
+ use RectorLaravel \ValueObject \ReplaceRequestKeyAndMethodValue ;
12
15
use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
13
16
use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
14
17
@@ -26,10 +29,18 @@ public function getRuleDefinition(): RuleDefinition
26
29
<<<'CODE_SAMPLE'
27
30
$_GET['value'];
28
31
$_POST['value'];
32
+ $_REQUEST['value'];
33
+ $_POST;
34
+ $_GET;
35
+ $_REQUEST;
29
36
CODE_SAMPLE,
30
37
<<<'CODE_SAMPLE'
38
+ \Illuminate\Support\Facades\Request::query('value');
39
+ \Illuminate\Support\Facades\Request::post('value');
31
40
\Illuminate\Support\Facades\Request::input('value');
32
- \Illuminate\Support\Facades\Request::input('value');
41
+ \Illuminate\Support\Facades\Request::query();
42
+ \Illuminate\Support\Facades\Request::post();
43
+ \Illuminate\Support\Facades\Request::all();
33
44
CODE_SAMPLE
34
45
),
35
46
]
@@ -38,31 +49,39 @@ public function getRuleDefinition(): RuleDefinition
38
49
39
50
public function getNodeTypes (): array
40
51
{
41
- return [ArrayDimFetch::class];
52
+ return [ArrayDimFetch::class, Variable::class ];
42
53
}
43
54
44
55
/**
45
- * @param ArrayDimFetch $node
56
+ * @param ArrayDimFetch|Variable $node
57
+ * @return StaticCall|1|null
46
58
*/
47
- public function refactor (Node $ node ): ? StaticCall
59
+ public function refactor (Node $ node ): StaticCall | int | null
48
60
{
49
- $ key = $ this ->findAllKeys ($ node );
61
+ if ($ node instanceof Variable) {
62
+ return $ this ->processVariable ($ node );
63
+ }
50
64
51
- if (! is_string ($ key )) {
52
- return null ;
65
+ $ replaceValue = $ this ->findAllKeys ($ node );
66
+
67
+ if ($ replaceValue instanceof ReplaceRequestKeyAndMethodValue) {
68
+ return $ this ->nodeFactory ->createStaticCall (
69
+ 'Illuminate\Support\Facades\Request ' ,
70
+ $ replaceValue ->getMethod (),
71
+ [new Arg (new String_ ($ replaceValue ->getKey ()))]
72
+ );
53
73
}
54
74
55
- return $ this ->nodeFactory ->createStaticCall (
56
- 'Illuminate\Support\Facades\Request ' ,
57
- 'input ' ,
58
- [new Arg (new String_ ($ key ))]
59
- );
75
+ return $ replaceValue ;
60
76
}
61
77
62
- public function findAllKeys (ArrayDimFetch $ arrayDimFetch ): ?string
78
+ /**
79
+ * @return ReplaceRequestKeyAndMethodValue|1|null
80
+ */
81
+ public function findAllKeys (ArrayDimFetch $ arrayDimFetch ): ReplaceRequestKeyAndMethodValue |int |null
63
82
{
64
83
if (! $ arrayDimFetch ->dim instanceof Scalar) {
65
- return null ;
84
+ return NodeVisitor:: DONT_TRAVERSE_CHILDREN ;
66
85
}
67
86
68
87
$ value = $ this ->getType ($ arrayDimFetch ->dim )->getConstantScalarValues ()[0 ] ?? null ;
@@ -72,19 +91,53 @@ public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string
72
91
}
73
92
74
93
if ($ arrayDimFetch ->var instanceof ArrayDimFetch) {
75
- $ key = $ this ->findAllKeys ($ arrayDimFetch ->var );
94
+ $ replaceValue = $ this ->findAllKeys ($ arrayDimFetch ->var );
76
95
77
- if ($ key === null ) {
78
- return null ;
96
+ if (! $ replaceValue instanceof ReplaceRequestKeyAndMethodValue ) {
97
+ return $ replaceValue ;
79
98
}
80
99
81
- return implode ('. ' , [$ key , $ value ]);
100
+ return new ReplaceRequestKeyAndMethodValue ( implode ('. ' , [$ replaceValue -> getKey () , $ value ]), $ replaceValue -> getMethod () );
82
101
}
83
102
84
- if ($ this ->isNames ($ arrayDimFetch ->var , ['_GET ' , '_POST ' ])) {
85
- return (string ) $ value ;
103
+ if ($ this ->isNames ($ arrayDimFetch ->var , ['_GET ' , '_POST ' , '_REQUEST ' ])) {
104
+ if (! $ arrayDimFetch ->var instanceof Variable) {
105
+ return null ;
106
+ }
107
+
108
+ $ method = match ($ arrayDimFetch ->var ->name ) {
109
+ '_GET ' => 'query ' ,
110
+ '_POST ' => 'post ' ,
111
+ '_REQUEST ' => 'input ' ,
112
+ default => null ,
113
+ };
114
+
115
+ if ($ method === null ) {
116
+ return null ;
117
+ }
118
+
119
+ return new ReplaceRequestKeyAndMethodValue ((string ) $ value , $ method );
86
120
}
87
121
88
122
return null ;
89
123
}
124
+
125
+ private function processVariable (Variable $ variable ): ?StaticCall
126
+ {
127
+ $ method = match ($ variable ->name ) {
128
+ '_GET ' => 'query ' ,
129
+ '_POST ' => 'post ' ,
130
+ '_REQUEST ' => 'all ' ,
131
+ default => null ,
132
+ };
133
+
134
+ if ($ method === null ) {
135
+ return null ;
136
+ }
137
+
138
+ return $ this ->nodeFactory ->createStaticCall (
139
+ 'Illuminate\Support\Facades\Request ' ,
140
+ $ method ,
141
+ );
142
+ }
90
143
}
0 commit comments