Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7fbb1de

Browse files
peterfoxGeniJaho
andauthoredOct 26, 2024
Adds RequestVariablesToRequestFacadeRector rule (#265)
Co-authored-by: Geni Jaho <jahogeni@gmail.com>
1 parent cc1e182 commit 7fbb1de

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-0
lines changed
 

‎docs/rector_rules_overview.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,21 @@ Change static `validate()` method to `$request->validate()`
11971197

11981198
<br>
11991199

1200+
## RequestVariablesToRequestFacadeRector
1201+
1202+
Change request variable definition in Facade
1203+
1204+
- class: [`RectorLaravel\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector`](../src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php)
1205+
1206+
```diff
1207+
-$_GET['value'];
1208+
-$_POST['value'];
1209+
+\Illuminate\Support\Facades\Request::input('value');
1210+
+\Illuminate\Support\Facades\Request::input('value');
1211+
```
1212+
1213+
<br>
1214+
12001215
## ResponseHelperCallToJsonResponseRector
12011216

12021217
Use new JsonResponse instead of `response()->json()`
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace RectorLaravel\Rector\ArrayDimFetch;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Arg;
7+
use PhpParser\Node\Expr\ArrayDimFetch;
8+
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Scalar;
10+
use PhpParser\Node\Scalar\String_;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\RequestVariablesToRequestFacadeRectorTest
17+
*/
18+
class RequestVariablesToRequestFacadeRector extends AbstractRector
19+
{
20+
public function getRuleDefinition(): RuleDefinition
21+
{
22+
return new RuleDefinition(
23+
'Change request variable definition in Facade',
24+
[
25+
new CodeSample(
26+
<<<'CODE_SAMPLE'
27+
$_GET['value'];
28+
$_POST['value'];
29+
CODE_SAMPLE,
30+
<<<'CODE_SAMPLE'
31+
\Illuminate\Support\Facades\Request::input('value');
32+
\Illuminate\Support\Facades\Request::input('value');
33+
CODE_SAMPLE
34+
),
35+
]
36+
);
37+
}
38+
39+
public function getNodeTypes(): array
40+
{
41+
return [ArrayDimFetch::class];
42+
}
43+
44+
/**
45+
* @param ArrayDimFetch $node
46+
*/
47+
public function refactor(Node $node): ?StaticCall
48+
{
49+
$key = $this->findAllKeys($node);
50+
51+
if (! is_string($key)) {
52+
return null;
53+
}
54+
55+
return $this->nodeFactory->createStaticCall(
56+
'Illuminate\Support\Facades\Request',
57+
'input',
58+
[new Arg(new String_($key))]
59+
);
60+
}
61+
62+
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string
63+
{
64+
if (! $arrayDimFetch->dim instanceof Scalar) {
65+
return null;
66+
}
67+
68+
$value = $this->getType($arrayDimFetch->dim)->getConstantScalarValues()[0] ?? null;
69+
70+
if ($value === null) {
71+
return null;
72+
}
73+
74+
if ($arrayDimFetch->var instanceof ArrayDimFetch) {
75+
$key = $this->findAllKeys($arrayDimFetch->var);
76+
77+
if ($key === null) {
78+
return null;
79+
}
80+
81+
return implode('.', [$key, $value]);
82+
}
83+
84+
if ($this->isNames($arrayDimFetch->var, ['_GET', '_POST'])) {
85+
return (string) $value;
86+
}
87+
88+
return null;
89+
}
90+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\Fixture;
4+
5+
$var = $_GET['a'];
6+
$deepVar = $_GET['a']['b'];
7+
$numberUsed = $_GET['a']['b'][0];
8+
$postVar = $_POST['a'];
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\Fixture;
15+
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+
21+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\Fixture;
4+
5+
$a = 'foobar';
6+
$var = $_GET[$a];
7+
8+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\Fixture;
4+
5+
$var = $_GETT['a'];
6+
7+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RequestVariablesToRequestFacadeRectorTest extends AbstractRectorTestCase
12+
{
13+
public static function provideData(): Iterator
14+
{
15+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
#[DataProvider('provideData')]
22+
public function test(string $filePath): void
23+
{
24+
$this->doTestFile($filePath);
25+
}
26+
27+
public function provideConfigFilePath(): string
28+
{
29+
return __DIR__ . '/config/configured_rule.php';
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
10+
11+
$rectorConfig->rule(RequestVariablesToRequestFacadeRector::class);
12+
};

0 commit comments

Comments
 (0)