Skip to content

Commit

Permalink
Improve request input refactor (#283)
Browse files Browse the repository at this point in the history
* handles request all

* Adds request

* Handles the different variables better to be more specific

* Docs update

---------

Co-authored-by: Geni Jaho <[email protected]>
  • Loading branch information
peterfox and GeniJaho authored Dec 23, 2024
1 parent dabd31f commit d4fff7a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
10 changes: 9 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,16 @@ Change request variable definition in Facade
```diff
-$_GET['value'];
-$_POST['value'];
-$_REQUEST['value'];
-$_POST;
-$_GET;
-$_REQUEST;
+\Illuminate\Support\Facades\Request::query('value');
+\Illuminate\Support\Facades\Request::post('value');
+\Illuminate\Support\Facades\Request::input('value');
+\Illuminate\Support\Facades\Request::input('value');
+\Illuminate\Support\Facades\Request::query();
+\Illuminate\Support\Facades\Request::post();
+\Illuminate\Support\Facades\Request::all();
```

<br>
Expand Down
93 changes: 73 additions & 20 deletions src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
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;

Expand All @@ -26,10 +29,18 @@ public function getRuleDefinition(): RuleDefinition
<<<'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::input('value');
\Illuminate\Support\Facades\Request::query();
\Illuminate\Support\Facades\Request::post();
\Illuminate\Support\Facades\Request::all();
CODE_SAMPLE
),
]
Expand All @@ -38,31 +49,39 @@ public function getRuleDefinition(): RuleDefinition

public function getNodeTypes(): array
{
return [ArrayDimFetch::class];
return [ArrayDimFetch::class, Variable::class];
}

/**
* @param ArrayDimFetch $node
* @param ArrayDimFetch|Variable $node
* @return StaticCall|1|null
*/
public function refactor(Node $node): ?StaticCall
public function refactor(Node $node): StaticCall|int|null
{
$key = $this->findAllKeys($node);
if ($node instanceof Variable) {
return $this->processVariable($node);
}

if (! is_string($key)) {
return null;
$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 $this->nodeFactory->createStaticCall(
'Illuminate\Support\Facades\Request',
'input',
[new Arg(new String_($key))]
);
return $replaceValue;
}

public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?string
/**
* @return ReplaceRequestKeyAndMethodValue|1|null
*/
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ReplaceRequestKeyAndMethodValue|int|null
{
if (! $arrayDimFetch->dim instanceof Scalar) {
return null;
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}

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

if ($arrayDimFetch->var instanceof ArrayDimFetch) {
$key = $this->findAllKeys($arrayDimFetch->var);
$replaceValue = $this->findAllKeys($arrayDimFetch->var);

if ($key === null) {
return null;
if (! $replaceValue instanceof ReplaceRequestKeyAndMethodValue) {
return $replaceValue;
}

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

if ($this->isNames($arrayDimFetch->var, ['_GET', '_POST'])) {
return (string) $value;
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,
);
}
}
23 changes: 23 additions & 0 deletions src/ValueObject/ReplaceRequestKeyAndMethodValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace RectorLaravel\ValueObject;

use Webmozart\Assert\Assert;

final readonly class ReplaceRequestKeyAndMethodValue
{
public function __construct(private string $key, private string $method)
{
Assert::inArray($this->method, ['query', 'post', 'input']);
}

public function getKey(): string
{
return $this->key;
}

public function getMethod(): string
{
return $this->method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ $var = $_GET['a'];
$deepVar = $_GET['a']['b'];
$numberUsed = $_GET['a']['b'][0];
$postVar = $_POST['a'];
$requestVar = $_REQUEST['a'];
$query = $_GET;
$post = $_POST;
$input = $_REQUEST;

?>
-----
<?php

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

$var = \Illuminate\Support\Facades\Request::input('a');
$deepVar = \Illuminate\Support\Facades\Request::input('a.b');
$numberUsed = \Illuminate\Support\Facades\Request::input('a.b.0');
$postVar = \Illuminate\Support\Facades\Request::input('a');
$var = \Illuminate\Support\Facades\Request::query('a');
$deepVar = \Illuminate\Support\Facades\Request::query('a.b');
$numberUsed = \Illuminate\Support\Facades\Request::query('a.b.0');
$postVar = \Illuminate\Support\Facades\Request::post('a');
$requestVar = \Illuminate\Support\Facades\Request::input('a');
$query = \Illuminate\Support\Facades\Request::query();
$post = \Illuminate\Support\Facades\Request::post();
$input = \Illuminate\Support\Facades\Request::all();

?>

0 comments on commit d4fff7a

Please sign in to comment.