diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 6b6b90f3..80c3b863 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1197,6 +1197,21 @@ Change static `validate()` method to `$request->validate()`
+## RequestVariablesToRequestFacadeRector + +Change request variable definition in Facade + +- class: [`RectorLaravel\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector`](../src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php) + +```diff +-$_GET['value']; +-$_POST['value']; ++\Illuminate\Support\Facades\Request::input('value'); ++\Illuminate\Support\Facades\Request::input('value'); +``` + +
+ ## ResponseHelperCallToJsonResponseRector Use new JsonResponse instead of `response()->json()` diff --git a/src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php b/src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php new file mode 100644 index 00000000..29a0c335 --- /dev/null +++ b/src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php @@ -0,0 +1,90 @@ +findAllKeys($node); + + if (! is_string($key)) { + return null; + } + + return $this->nodeFactory->createStaticCall( + 'Illuminate\Support\Facades\Request', + 'input', + [new Arg(new String_($key))] + ); + } + + public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string + { + if (! $arrayDimFetch->dim instanceof Scalar) { + return null; + } + + $value = $this->getType($arrayDimFetch->dim)->getConstantScalarValues()[0] ?? null; + + if ($value === null) { + return null; + } + + if ($arrayDimFetch->var instanceof ArrayDimFetch) { + $key = $this->findAllKeys($arrayDimFetch->var); + + if ($key === null) { + return null; + } + + return implode('.', [$key, $value]); + } + + if ($this->isNames($arrayDimFetch->var, ['_GET', '_POST'])) { + return (string) $value; + } + + return null; + } +} diff --git a/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/fixture.php.inc b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..777ad61e --- /dev/null +++ b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/fixture.php.inc @@ -0,0 +1,21 @@ + +----- + diff --git a/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_complex_dim.php.inc b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_complex_dim.php.inc new file mode 100644 index 00000000..5deadc55 --- /dev/null +++ b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_complex_dim.php.inc @@ -0,0 +1,8 @@ + diff --git a/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_non_matching_variables.php.inc b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_non_matching_variables.php.inc new file mode 100644 index 00000000..5c102be7 --- /dev/null +++ b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_non_matching_variables.php.inc @@ -0,0 +1,7 @@ + diff --git a/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/RequestVariablesToRequestFacadeRectorTest.php b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/RequestVariablesToRequestFacadeRectorTest.php new file mode 100644 index 00000000..5df0ad6f --- /dev/null +++ b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/RequestVariablesToRequestFacadeRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($filePath); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/config/configured_rule.php b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/config/configured_rule.php new file mode 100644 index 00000000..f702bdcd --- /dev/null +++ b/tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(RequestVariablesToRequestFacadeRector::class); +};