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 the EnvVariableToEnvHelperRector rule #264

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: 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);
};
Loading