-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ServerVariableToRequestFacadeRector rule
- Loading branch information
Showing
7 changed files
with
141 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
src/Rector/ArrayDimFetch/ServerVariableToRequestFacadeRector.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,55 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\ArrayDimFetch; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use RectorLaravel\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\ServerVariableToRequestFacadeRector\ServerVariableToRequestFacadeRectorTest | ||
*/ | ||
class ServerVariableToRequestFacadeRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change server variable to Request facade\'s server method', | ||
[new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$_SERVER['VARIABLE']; | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
\Illuminate\Support\Facade\Request::server('VARIABLE'); | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ArrayDimFetch::class]; | ||
} | ||
|
||
/** | ||
* @param ArrayDimFetch $node | ||
*/ | ||
public function refactor(Node $node): ?StaticCall | ||
{ | ||
if (! $this->isName($node->var, '_SERVER')) { | ||
return null; | ||
} | ||
|
||
if ($node->dim === null) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Request', 'server', [ | ||
new Arg($node->dim), | ||
]); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rector/ArrayDimFetch/ServerVariableToRequestFacadeRector/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,15 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\ServerVariableToRequestFacadeRector\Fixture; | ||
|
||
$_SERVER['VARIABLE']; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\ServerVariableToRequestFacadeRector\Fixture; | ||
|
||
\Illuminate\Support\Facades\Request::server('VARIABLE'); | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...ayDimFetch/ServerVariableToRequestFacadeRector/Fixture/skip_dim_fetch_without_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,7 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\ServerVariableToRequestFacadeRector\Fixture; | ||
|
||
$_SERVER[]; | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...rrayDimFetch/ServerVariableToRequestFacadeRector/Fixture/skip_non_server_variable.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\ServerVariableToRequestFacadeRector\Fixture; | ||
|
||
$_SERVERA['VARIABLE']; | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...yDimFetch/ServerVariableToRequestFacadeRector/ServerVariableToRequestFacadeRectorTest.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\ServerVariableToRequestFacadeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ServerVariableToRequestFacadeRectorTest 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/ServerVariableToRequestFacadeRector/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\ServerVariableToRequestFacadeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(ServerVariableToRequestFacadeRector::class); | ||
}; |