Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds RequestVariablesToRequestFacadeRector rule #265

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,21 @@ Change static `validate()` method to `$request->validate()`

<br>

## 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');
```

<br>

## ResponseHelperCallToJsonResponseRector

Use new JsonResponse instead of `response()->json()`
Expand Down
90 changes: 90 additions & 0 deletions src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace RectorLaravel\Rector\ArrayDimFetch;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector\RequestVariablesToRequestFacadeRectorTest
*/
class RequestVariablesToRequestFacadeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change request variable definition in Facade',
[
new CodeSample(
<<<'CODE_SAMPLE'
$_GET['value'];
$_POST['value'];
CODE_SAMPLE,
<<<'CODE_SAMPLE'
\Illuminate\Support\Facades\Request::input('value');
\Illuminate\Support\Facades\Request::input('value');
CODE_SAMPLE
),
]
);
}

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

/**
* @param ArrayDimFetch $node
*/
public function refactor(Node $node): ?StaticCall
{
$key = $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

$var = $_GET['a'];
$deepVar = $_GET['a']['b'];
$numberUsed = $_GET['a']['b'][0];
$postVar = $_POST['a'];

?>
-----
<?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');

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

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

$a = 'foobar';
$var = $_GET[$a];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

$var = $_GETT['a'];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RequestVariablesToRequestFacadeRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RequestVariablesToRequestFacadeRector::class);
};
Loading