From f38d8a3f812819671b6fc52c3c2392be8a4bfeac Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 20 Jun 2024 17:52:34 +0200 Subject: [PATCH 1/4] Prefix all sprintf() calls --- Command/DebugCommand.php | 26 +++++++++++++------------- Command/DotenvDumpCommand.php | 2 +- Dotenv.php | 4 ++-- Exception/FormatException.php | 2 +- Exception/PathException.php | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Command/DebugCommand.php b/Command/DebugCommand.php index d0c2723..2aea269 100644 --- a/Command/DebugCommand.php +++ b/Command/DebugCommand.php @@ -86,18 +86,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int $envFiles = $this->getEnvFiles($filePath); $availableFiles = array_filter($envFiles, 'is_file'); - if (\in_array(sprintf('%s.local.php', $filePath), $availableFiles, true)) { - $io->warning(sprintf('Due to existing dump file (%s.local.php) all other dotenv files are skipped.', $this->getRelativeName($filePath))); + if (\in_array(\sprintf('%s.local.php', $filePath), $availableFiles, true)) { + $io->warning(\sprintf('Due to existing dump file (%s.local.php) all other dotenv files are skipped.', $this->getRelativeName($filePath))); } - if (is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) { - $io->warning(sprintf('The file %s.dist gets skipped due to the existence of %1$s.', $this->getRelativeName($filePath))); + if (is_file($filePath) && is_file(\sprintf('%s.dist', $filePath))) { + $io->warning(\sprintf('The file %s.dist gets skipped due to the existence of %1$s.', $this->getRelativeName($filePath))); } $io->section('Scanned Files (in descending priority)'); $io->listing(array_map(fn (string $envFile) => \in_array($envFile, $availableFiles, true) - ? sprintf('✓ %s', $this->getRelativeName($envFile)) - : sprintf('⨯ %s', $this->getRelativeName($envFile)), $envFiles)); + ? \sprintf('✓ %s', $this->getRelativeName($envFile)) + : \sprintf('⨯ %s', $this->getRelativeName($envFile)), $envFiles)); $nameFilter = $input->getArgument('filter'); $variables = $this->getVariables($availableFiles, $nameFilter); @@ -112,7 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->comment('Note that values might be different between web and CLI.'); } else { - $io->warning(sprintf('No variables match the given filter "%s".', $nameFilter)); + $io->warning(\sprintf('No variables match the given filter "%s".', $nameFilter)); } return 0; @@ -176,17 +176,17 @@ private function getAvailableVars(): array private function getEnvFiles(string $filePath): array { $files = [ - sprintf('%s.local.php', $filePath), - sprintf('%s.%s.local', $filePath, $this->kernelEnvironment), - sprintf('%s.%s', $filePath, $this->kernelEnvironment), + \sprintf('%s.local.php', $filePath), + \sprintf('%s.%s.local', $filePath, $this->kernelEnvironment), + \sprintf('%s.%s', $filePath, $this->kernelEnvironment), ]; if ('test' !== $this->kernelEnvironment) { - $files[] = sprintf('%s.local', $filePath); + $files[] = \sprintf('%s.local', $filePath); } - if (!is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) { - $files[] = sprintf('%s.dist', $filePath); + if (!is_file($filePath) && is_file(\sprintf('%s.dist', $filePath))) { + $files[] = \sprintf('%s.dist', $filePath); } else { $files[] = $filePath; } diff --git a/Command/DotenvDumpCommand.php b/Command/DotenvDumpCommand.php index 37383dc..8a367a3 100644 --- a/Command/DotenvDumpCommand.php +++ b/Command/DotenvDumpCommand.php @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int EOF; file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX); - $output->writeln(sprintf('Successfully dumped .env files in .env.local.php for the %s environment.', $env)); + $output->writeln(\sprintf('Successfully dumped .env files in .env.local.php for the %s environment.', $env)); return 0; } diff --git a/Dotenv.php b/Dotenv.php index e29051e..a4feb8b 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -461,7 +461,7 @@ private function resolveCommands(string $value, array $loadedVars): string try { $process->mustRun(); } catch (ProcessException) { - throw $this->createFormatException(sprintf('Issue expanding a command (%s)', $process->getErrorOutput())); + throw $this->createFormatException(\sprintf('Issue expanding a command (%s)', $process->getErrorOutput())); } return rtrim($process->getOutput(), "\n\r"); @@ -516,7 +516,7 @@ private function resolveVariables(string $value, array $loadedVars): string if ('' === $value && isset($matches['default_value']) && '' !== $matches['default_value']) { $unsupportedChars = strpbrk($matches['default_value'], '\'"{$'); if (false !== $unsupportedChars) { - throw $this->createFormatException(sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name)); + throw $this->createFormatException(\sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name)); } $value = substr($matches['default_value'], 2); diff --git a/Exception/FormatException.php b/Exception/FormatException.php index 81c00bb..92dfa13 100644 --- a/Exception/FormatException.php +++ b/Exception/FormatException.php @@ -24,7 +24,7 @@ public function __construct( int $code = 0, ?\Throwable $previous = null, ) { - parent::__construct(sprintf("%s in \"%s\" at line %d.\n%s", $message, $context->getPath(), $context->getLineno(), $context->getDetails()), $code, $previous); + parent::__construct(\sprintf("%s in \"%s\" at line %d.\n%s", $message, $context->getPath(), $context->getLineno(), $context->getDetails()), $code, $previous); } public function getContext(): FormatExceptionContext diff --git a/Exception/PathException.php b/Exception/PathException.php index e432b2e..568b009 100644 --- a/Exception/PathException.php +++ b/Exception/PathException.php @@ -20,6 +20,6 @@ final class PathException extends \RuntimeException implements ExceptionInterfac { public function __construct(string $path, int $code = 0, ?\Throwable $previous = null) { - parent::__construct(sprintf('Unable to read the "%s" environment file.', $path), $code, $previous); + parent::__construct(\sprintf('Unable to read the "%s" environment file.', $path), $code, $previous); } } From 339dd23571cd572b2998dee84ebc761167b61341 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 6 Jul 2024 09:57:16 +0200 Subject: [PATCH 2/4] Update .gitattributes --- .gitattributes | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index 84c7add..14c3c35 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,3 @@ /Tests export-ignore /phpunit.xml.dist export-ignore -/.gitattributes export-ignore -/.gitignore export-ignore +/.git* export-ignore From ccc9d8459d1da9dd951968de910c60714272665b Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 22 Jul 2024 10:27:43 +0200 Subject: [PATCH 3/4] Use CPP where possible --- Command/DebugCommand.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Command/DebugCommand.php b/Command/DebugCommand.php index 2aea269..268330d 100644 --- a/Command/DebugCommand.php +++ b/Command/DebugCommand.php @@ -40,14 +40,10 @@ final class DebugCommand extends Command */ protected static $defaultDescription = 'List all dotenv files with variables and values'; - private string $kernelEnvironment; - private string $projectDirectory; - - public function __construct(string $kernelEnvironment, string $projectDirectory) - { - $this->kernelEnvironment = $kernelEnvironment; - $this->projectDirectory = $projectDirectory; - + public function __construct( + private string $kernelEnvironment, + private string $projectDirectory, + ) { parent::__construct(); } From 966f1900ef07b99e360f3bf6874f16adab39a8f0 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 31 Jul 2024 16:13:26 +0200 Subject: [PATCH 4/4] Remove unused code and unnecessary `else` branches --- Dotenv.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Dotenv.php b/Dotenv.php index a4feb8b..3231a08 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -485,7 +485,7 @@ private function resolveVariables(string $value, array $loadedVars): string (?P\})? # optional closing brace /x'; - $value = preg_replace_callback($regex, function ($matches) use ($loadedVars) { + return preg_replace_callback($regex, function ($matches) use ($loadedVars) { // odd number of backslashes means the $ character is escaped if (1 === \strlen($matches['backslashes']) % 2) { return substr($matches[0], 1); @@ -532,8 +532,6 @@ private function resolveVariables(string $value, array $loadedVars): string return $matches['backslashes'].$value; }, $value); - - return $value; } private function moveCursor(string $text): void