Skip to content

Commit

Permalink
Adds the EnvVariableToEnvHelperRector rule (#264)
Browse files Browse the repository at this point in the history
Co-authored-by: Geni Jaho <jahogeni@gmail.com>
  • Loading branch information
peterfox and GeniJaho authored Oct 26, 2024
1 parent cefbadd commit cc1e182
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 69 Rules Overview
# 70 Rules Overview

## AbortIfRector

Expand Down Expand Up @@ -578,6 +578,19 @@ Replace use of the unsafe `empty()` function with Laravel's safer `blank()` & `f

<br>

## EnvVariableToEnvHelperRector

Change env variable to env static call

- class: [`RectorLaravel\Rector\ArrayDimFetch\EnvVariableToEnvHelperRector`](../src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php)

```diff
-$_ENV['APP_NAME'];
+\Illuminate\Support\Env::get('APP_NAME');
```

<br>

## FactoryApplyingStatesRector

Call the state methods directly instead of specify the name of state.
Expand Down
55 changes: 55 additions & 0 deletions src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php
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),
]);
}
}
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';
}
}
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');

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

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

$_ENV[];

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

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

0 comments on commit cc1e182

Please sign in to comment.