-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds RequestVariablesToRequestFacadeRector rule (#265)
Co-authored-by: Geni Jaho <jahogeni@gmail.com>
- Loading branch information
Showing
7 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
?> |
8 changes: 8 additions & 0 deletions
8
...ctor/ArrayDimFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_complex_dim.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...imFetch/RequestVariablesToRequestFacadeRector/Fixture/skip_non_matching_variables.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...Fetch/RequestVariablesToRequestFacadeRector/RequestVariablesToRequestFacadeRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |