From cc1e182eb9b218625b0d7e31952717f5d294c426 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 26 Oct 2024 14:43:48 +0100 Subject: [PATCH] Adds the EnvVariableToEnvHelperRector rule (#264) Co-authored-by: Geni Jaho --- docs/rector_rules_overview.md | 15 ++++- .../EnvVariableToEnvHelperRector.php | 55 +++++++++++++++++++ .../EnvVariableToEnvHelperRectorTest.php | 31 +++++++++++ .../Fixture/fixture.php.inc | 15 +++++ .../skip_dim_fetch_without_dim.php.inc | 7 +++ .../Fixture/skip_non_env_variable.php.inc | 7 +++ .../config/configured_rule.php | 12 ++++ 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php create mode 100644 tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/EnvVariableToEnvHelperRectorTest.php create mode 100644 tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_dim_fetch_without_dim.php.inc create mode 100644 tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_non_env_variable.php.inc create mode 100644 tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/config/configured_rule.php diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index f8efff26..6b6b90f3 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 69 Rules Overview +# 70 Rules Overview ## AbortIfRector @@ -578,6 +578,19 @@ Replace use of the unsafe `empty()` function with Laravel's safer `blank()` & `f
+## 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'); +``` + +
+ ## FactoryApplyingStatesRector Call the state methods directly instead of specify the name of state. diff --git a/src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php b/src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php new file mode 100644 index 00000000..295d8c02 --- /dev/null +++ b/src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php @@ -0,0 +1,55 @@ +isName($node->var, '_ENV')) { + return null; + } + + if ($node->dim === null) { + return null; + } + + return $this->nodeFactory->createStaticCall('Illuminate\Support\Env', 'get', [ + new Arg($node->dim), + ]); + } +} diff --git a/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/EnvVariableToEnvHelperRectorTest.php b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/EnvVariableToEnvHelperRectorTest.php new file mode 100644 index 00000000..8fcba10b --- /dev/null +++ b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/EnvVariableToEnvHelperRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($filePath); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/fixture.php.inc b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..4419b33c --- /dev/null +++ b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/fixture.php.inc @@ -0,0 +1,15 @@ + +----- + diff --git a/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_dim_fetch_without_dim.php.inc b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_dim_fetch_without_dim.php.inc new file mode 100644 index 00000000..8665ff49 --- /dev/null +++ b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_dim_fetch_without_dim.php.inc @@ -0,0 +1,7 @@ + diff --git a/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_non_env_variable.php.inc b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_non_env_variable.php.inc new file mode 100644 index 00000000..455a2c71 --- /dev/null +++ b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/Fixture/skip_non_env_variable.php.inc @@ -0,0 +1,7 @@ + diff --git a/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/config/configured_rule.php b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/config/configured_rule.php new file mode 100644 index 00000000..fe9203e0 --- /dev/null +++ b/tests/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(EnvVariableToEnvHelperRector::class); +};