Skip to content

Commit d4fff7a

Browse files
peterfoxGeniJaho
andauthored
Improve request input refactor (#283)
* handles request all * Adds request * Handles the different variables better to be more specific * Docs update --------- Co-authored-by: Geni Jaho <[email protected]>
1 parent dabd31f commit d4fff7a

File tree

4 files changed

+117
-25
lines changed

4 files changed

+117
-25
lines changed

docs/rector_rules_overview.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,16 @@ Change request variable definition in Facade
12381238
```diff
12391239
-$_GET['value'];
12401240
-$_POST['value'];
1241+
-$_REQUEST['value'];
1242+
-$_POST;
1243+
-$_GET;
1244+
-$_REQUEST;
1245+
+\Illuminate\Support\Facades\Request::query('value');
1246+
+\Illuminate\Support\Facades\Request::post('value');
12411247
+\Illuminate\Support\Facades\Request::input('value');
1242-
+\Illuminate\Support\Facades\Request::input('value');
1248+
+\Illuminate\Support\Facades\Request::query();
1249+
+\Illuminate\Support\Facades\Request::post();
1250+
+\Illuminate\Support\Facades\Request::all();
12431251
```
12441252

12451253
<br>

src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use PhpParser\Node\Arg;
77
use PhpParser\Node\Expr\ArrayDimFetch;
88
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Expr\Variable;
910
use PhpParser\Node\Scalar;
1011
use PhpParser\Node\Scalar\String_;
12+
use PhpParser\NodeVisitor;
1113
use RectorLaravel\AbstractRector;
14+
use RectorLaravel\ValueObject\ReplaceRequestKeyAndMethodValue;
1215
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1316
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1417

@@ -26,10 +29,18 @@ public function getRuleDefinition(): RuleDefinition
2629
<<<'CODE_SAMPLE'
2730
$_GET['value'];
2831
$_POST['value'];
32+
$_REQUEST['value'];
33+
$_POST;
34+
$_GET;
35+
$_REQUEST;
2936
CODE_SAMPLE,
3037
<<<'CODE_SAMPLE'
38+
\Illuminate\Support\Facades\Request::query('value');
39+
\Illuminate\Support\Facades\Request::post('value');
3140
\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();
3344
CODE_SAMPLE
3445
),
3546
]
@@ -38,31 +49,39 @@ public function getRuleDefinition(): RuleDefinition
3849

3950
public function getNodeTypes(): array
4051
{
41-
return [ArrayDimFetch::class];
52+
return [ArrayDimFetch::class, Variable::class];
4253
}
4354

4455
/**
45-
* @param ArrayDimFetch $node
56+
* @param ArrayDimFetch|Variable $node
57+
* @return StaticCall|1|null
4658
*/
47-
public function refactor(Node $node): ?StaticCall
59+
public function refactor(Node $node): StaticCall|int|null
4860
{
49-
$key = $this->findAllKeys($node);
61+
if ($node instanceof Variable) {
62+
return $this->processVariable($node);
63+
}
5064

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+
);
5373
}
5474

55-
return $this->nodeFactory->createStaticCall(
56-
'Illuminate\Support\Facades\Request',
57-
'input',
58-
[new Arg(new String_($key))]
59-
);
75+
return $replaceValue;
6076
}
6177

62-
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string
78+
/**
79+
* @return ReplaceRequestKeyAndMethodValue|1|null
80+
*/
81+
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ReplaceRequestKeyAndMethodValue|int|null
6382
{
6483
if (! $arrayDimFetch->dim instanceof Scalar) {
65-
return null;
84+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
6685
}
6786

6887
$value = $this->getType($arrayDimFetch->dim)->getConstantScalarValues()[0] ?? null;
@@ -72,19 +91,53 @@ public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string
7291
}
7392

7493
if ($arrayDimFetch->var instanceof ArrayDimFetch) {
75-
$key = $this->findAllKeys($arrayDimFetch->var);
94+
$replaceValue = $this->findAllKeys($arrayDimFetch->var);
7695

77-
if ($key === null) {
78-
return null;
96+
if (! $replaceValue instanceof ReplaceRequestKeyAndMethodValue) {
97+
return $replaceValue;
7998
}
8099

81-
return implode('.', [$key, $value]);
100+
return new ReplaceRequestKeyAndMethodValue(implode('.', [$replaceValue->getKey(), $value]), $replaceValue->getMethod());
82101
}
83102

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);
86120
}
87121

88122
return null;
89123
}
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+
}
90143
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace RectorLaravel\ValueObject;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
final readonly class ReplaceRequestKeyAndMethodValue
8+
{
9+
public function __construct(private string $key, private string $method)
10+
{
11+
Assert::inArray($this->method, ['query', 'post', 'input']);
12+
}
13+
14+
public function getKey(): string
15+
{
16+
return $this->key;
17+
}
18+
19+
public function getMethod(): string
20+
{
21+
return $this->method;
22+
}
23+
}

tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/fixture.php.inc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ $var = $_GET['a'];
66
$deepVar = $_GET['a']['b'];
77
$numberUsed = $_GET['a']['b'][0];
88
$postVar = $_POST['a'];
9+
$requestVar = $_REQUEST['a'];
10+
$query = $_GET;
11+
$post = $_POST;
12+
$input = $_REQUEST;
913

1014
?>
1115
-----
1216
<?php
1317

1418
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\Fixture;
1519

16-
$var = \Illuminate\Support\Facades\Request::input('a');
17-
$deepVar = \Illuminate\Support\Facades\Request::input('a.b');
18-
$numberUsed = \Illuminate\Support\Facades\Request::input('a.b.0');
19-
$postVar = \Illuminate\Support\Facades\Request::input('a');
20+
$var = \Illuminate\Support\Facades\Request::query('a');
21+
$deepVar = \Illuminate\Support\Facades\Request::query('a.b');
22+
$numberUsed = \Illuminate\Support\Facades\Request::query('a.b.0');
23+
$postVar = \Illuminate\Support\Facades\Request::post('a');
24+
$requestVar = \Illuminate\Support\Facades\Request::input('a');
25+
$query = \Illuminate\Support\Facades\Request::query();
26+
$post = \Illuminate\Support\Facades\Request::post();
27+
$input = \Illuminate\Support\Facades\Request::all();
2028

2129
?>

0 commit comments

Comments
 (0)