Skip to content

[FrameworkBundle] Detect unused environment variables in .env #49161

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

Open
wants to merge 7 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Detect unused environment variables in .env
  • Loading branch information
mw3e21a committed Jan 30, 2023
commit f241ee7998552ee12eb3792b35e781fcdd744e65
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Deprecate the `notifier.logger_notification_listener` service, use the `notifier.notification_logger_listener` service instead
* Allow setting private services with the test container
* Register alias for argument for workflow services with workflow name only
* Improved `bin/console debug:container --env-vars` and the `--env-var` option to display the number of occurrences of each environment variable in the container.

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -296,9 +297,25 @@ private function getContainerEnvVars(ContainerBuilder $container): array
return [];
}

$file = file_get_contents($container->getParameter('debug.container.dump'));
preg_match_all('{%env\(((?:\w++:)*+\w++)\)%}', $file, $envVars);
$envVars = array_unique($envVars[1]);
if(!$container->hasParameter('kernel.project_dir') || !$container->hasParameter('kernel.environment')) {
return [];
}

$envVarsFromContainer = [];
$containerFile = file_get_contents($container->getParameter('debug.container.dump'));
preg_match_all('{%env\(((?:\w++:)*+\w++)\)%}', $containerFile, $envVarsFromContainer);
$envVarsFromContainer = array_unique($envVarsFromContainer[1]);

$envFiles = $this->getEnvFiles($container->getParameter('kernel.project_dir'), $container->getParameter('kernel.environment'));
$availableFiles = array_filter($envFiles, static fn (string $file) => is_file($file));

$dotenv = new Dotenv();
$envVarsFromFiles = [];
foreach ($availableFiles as $file) {
$envs = $dotenv->parse(file_get_contents($file), $file);
$envVarsFromFiles += $envs;
}
$allEnvVars = array_unique(array_merge($envVarsFromContainer, array_keys($envVarsFromFiles)));

$bag = $container->getParameterBag();
$getDefaultParameter = fn (string $name) => parent::get($name);
Expand All @@ -308,7 +325,8 @@ private function getContainerEnvVars(ContainerBuilder $container): array

$envs = [];

foreach ($envVars as $env) {
foreach ($allEnvVars as $env) {

$processor = 'string';
if (false !== $i = strrpos($name = $env, ':')) {
$name = substr($env, $i + 1);
Expand All @@ -319,6 +337,10 @@ private function getContainerEnvVars(ContainerBuilder $container): array
$runtimeValue = null;
}
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;

preg_match_all('/'.preg_quote($env).'/', $containerFile, $matches);
$usageCounter = count($matches[0]);

$envs["$name$processor"] = [
'name' => $name,
'processor' => $processor,
Expand All @@ -327,6 +349,7 @@ private function getContainerEnvVars(ContainerBuilder $container): array
'runtime_available' => $hasRuntime,
'runtime_value' => $runtimeValue,
'processed_value' => $processedValue,
'usage_count' => $usageCounter,
];
}
ksort($envs);
Expand All @@ -342,4 +365,30 @@ protected function getServiceEdges(ContainerBuilder $builder, string $serviceId)
return [];
}
}

private function getEnvFiles(string $projectDir, string $kernelEnvironment): array
{
$files = [
'.env.local.php',
sprintf('.env.%s.local', $kernelEnvironment),
sprintf('.env.%s', $kernelEnvironment),
];

if ('test' !== $kernelEnvironment) {
$files[] = $this->getFilePath($projectDir, '.env.local');
}

if (!is_file($this->getFilePath($projectDir, '.env')) && is_file($this->getFilePath($projectDir, '.env.dist'))) {
$files[] = $this->getFilePath($projectDir, '.env.dist');
} else {
$files[] = $this->getFilePath($projectDir, '.env');
}

return $files;
}
private function getFilePath(string $projectDir, string $file): string
{
return $projectDir.\DIRECTORY_SEPARATOR.$file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
['<info>Default value</>', $env['default_available'] ? $dump($env['default_value']) : 'n/a'],
['<info>Real value</>', $env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a'],
['<info>Processed value</>', $env['default_available'] || $env['runtime_available'] ? $dump($env['processed_value']) : 'n/a'],
['<info>Usage count</>', $env['usage_count'] ? $dump($env['usage_count']) : 'n/a'],
]);
}
}
Expand Down Expand Up @@ -462,13 +463,14 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
$env['name'],
$env['default_available'] ? $dump($env['default_value']) : 'n/a',
$env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a',
$env['usage_count'] ? $dump($env['usage_count']) : 'n/a',
];
if (!$env['default_available'] && !$env['runtime_available']) {
$missing[$env['name']] = true;
}
}

$options['output']->table(['Name', 'Default value', 'Real value'], $rows);
$options['output']->table(['Name', 'Default value', 'Real value', 'Usage count'], $rows);
$options['output']->comment('Note real values might be different between web and CLI.');

if ($missing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
Expand Down Expand Up @@ -125,6 +126,19 @@ public function testTagsPartialSearch()
public function testDescribeEnvVars()
{
putenv('REAL=value');

file_put_contents(__DIR__.'/app/.env', <<<EOF
APP_EXAMPLE_ENV=example_value
APP_EXAMPLE_ENV_TO_OVERRIDE=example_value
EOF
);
file_put_contents(__DIR__.'/app/.env.local', <<<EOF
APP_EXAMPLE_ENV_TO_OVERRIDE=example_overridden
EOF
);

(new Dotenv())->bootEnv(__DIR__.'/app/.env');

static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);

$application = new Application(static::$kernel);
Expand All @@ -140,13 +154,15 @@ public function testDescribeEnvVars()
Symfony Container Environment Variables
=======================================

--------- ----------------- ------------%w
Name Default value Real value%w
--------- ----------------- ------------%w
JSON "[1, "2.5", 3]" n/a%w
REAL n/a "value"%w
UNKNOWN n/a n/a%w
--------- ----------------- ------------%w
----------------------------- ----------------- ---------------------- -------------%w
Name Default value Real value Usage count%w
----------------------------- ----------------- ---------------------- -------------%w
APP_EXAMPLE_ENV_TO_OVERRIDE n/a "example_overridden" n/a%w
APP_EXAMPLE_ENV n/a "example_value" n/a%w
JSON "[1, "2.5", 3]" n/a 1%w
REAL n/a "value" 3%w
UNKNOWN n/a n/a 1%w
----------------------------- ----------------- ---------------------- -------------%w

// Note real values might be different between web and CLI.%w

Expand All @@ -158,6 +174,8 @@ public function testDescribeEnvVars()
, $tester->getDisplay(true));

putenv('REAL');
@unlink(__DIR__.'/app/.env');
@unlink(__DIR__.'/app/.env.local');
}

public function testDescribeEnvVar()
Expand All @@ -172,7 +190,7 @@ public function testDescribeEnvVar()
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);

$this->assertStringContainsString(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
$this->assertStringEqualsFile(__DIR__.'/Fixtures/describe_env_vars.txt', $tester->getDisplay(true));
}

public function testGetDeprecation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

Symfony Container Environment Variables
=======================================

// Displaying detailed environment variable usage matching js

%env(float:key:2:json:JSON)%
----------------------------

----------------- -----------------
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3.0
Usage count 1
----------------- -----------------

%env(int:key:2:json:JSON)%
Expand All @@ -14,4 +21,8 @@
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3
Usage count 1
----------------- -----------------

// Note real values might be different between web and CLI.

Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ services:
- '%env(REAL)%'
- '%env(int:key:2:json:JSON)%'
- '%env(float:key:2:json:JSON)%'
useEnv:
class: stdClass
arguments:
- '%env(REAL)%'
- '%env(REAL)%'