-
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 the EnvVariableToEnvHelperRector rule (#264)
Co-authored-by: Geni Jaho <jahogeni@gmail.com>
- 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
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 Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\EnvVariableToEnvHelperRector\EnvVariableToEnvHelperRectorTest | ||
*/ | ||
class EnvVariableToEnvHelperRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change env variable to env static call', | ||
[new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$_ENV['APP_NAME']; | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
\Illuminate\Support\Env::get('APP_NAME'); | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ArrayDimFetch::class]; | ||
} | ||
|
||
/** | ||
* @param ArrayDimFetch $node | ||
*/ | ||
public function refactor(Node $node): ?StaticCall | ||
{ | ||
if (! $this->isName($node->var, '_ENV')) { | ||
return null; | ||
} | ||
|
||
if ($node->dim === null) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createStaticCall('Illuminate\Support\Env', 'get', [ | ||
new Arg($node->dim), | ||
]); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/EnvVariableToEnvHelperRectorTest.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\EnvVariableToEnvHelperRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class EnvVariableToEnvHelperRectorTest 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'; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/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\EnvVariableToEnvHelperRector\Fixture; | ||
|
||
$_ENV['APP_NAME']; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\ArrayDimFetch\EnvVariableToEnvHelperRector\Fixture; | ||
|
||
\Illuminate\Support\Env::get('APP_NAME'); | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...tor/ArrayDimFetch/EnvVariableToEnvHelperRector/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\EnvVariableToEnvHelperRector\Fixture; | ||
|
||
$_ENV[]; | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...s/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_non_env_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\EnvVariableToEnvHelperRector\Fixture; | ||
|
||
$_ENVV['APP_NAME']; | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/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\EnvVariableToEnvHelperRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(EnvVariableToEnvHelperRector::class); | ||
}; |