From 003a0e0ac804c1740d06c6fcfcb76fb2276876cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Wed, 22 Nov 2023 00:08:34 +0100 Subject: [PATCH] refactor: Move the constants into a dedicated class (#1184) --- composer.json | 1 - src/Composer/ComposerProcessFactory.php | 4 ++-- src/Console/Php/PhpSettingsHandler.php | 6 +++--- src/{consts.php => Constants.php} | 19 ++++++++++--------- src/Phar/PharInfo.php | 4 ++-- src/functions.php | 6 +++--- 6 files changed, 20 insertions(+), 20 deletions(-) rename src/{consts.php => Constants.php} (51%) diff --git a/composer.json b/composer.json index d949a74c9..d4b8722c6 100644 --- a/composer.json +++ b/composer.json @@ -72,7 +72,6 @@ "KevinGH\\Box\\": "src" }, "files": [ - "src/consts.php", "src/functions.php" ], "exclude-from-classmap": [ diff --git a/src/Composer/ComposerProcessFactory.php b/src/Composer/ComposerProcessFactory.php index 1cfbb8878..6584ce950 100644 --- a/src/Composer/ComposerProcessFactory.php +++ b/src/Composer/ComposerProcessFactory.php @@ -16,10 +16,10 @@ use Closure; use Fidry\Console\IO; +use KevinGH\Box\Constants; use RuntimeException; use Symfony\Component\Process\ExecutableFinder; use Symfony\Component\Process\Process; -use const KevinGH\Box\BOX_ALLOW_XDEBUG; /** * @final @@ -143,7 +143,7 @@ private static function getDefaultEnvVars(): array { $vars = ['COMPOSER_ORIGINAL_INIS' => '']; - if ('1' === (string) getenv(BOX_ALLOW_XDEBUG)) { + if ('1' === (string) getenv(Constants::ALLOW_XDEBUG)) { $vars['COMPOSER_ALLOW_XDEBUG'] = '1'; } diff --git a/src/Console/Php/PhpSettingsHandler.php b/src/Console/Php/PhpSettingsHandler.php index ccec96722..059716b90 100644 --- a/src/Console/Php/PhpSettingsHandler.php +++ b/src/Console/Php/PhpSettingsHandler.php @@ -16,6 +16,7 @@ use Composer\XdebugHandler\XdebugHandler; use Fidry\FileSystem\FS; +use KevinGH\Box\Constants; use KevinGH\Box\Phar\PharPhpSettings; use Psr\Log\LoggerInterface; use Webmozart\Assert\Assert; @@ -26,7 +27,6 @@ use function KevinGH\Box\memory_to_bytes; use function sprintf; use function trim; -use const KevinGH\Box\BOX_MEMORY_LIMIT; use const PHP_EOL; /** @@ -128,7 +128,7 @@ private function bumpMemoryLimit(): void sprintf( 'Changed the memory limit from "%s" to %s="%s"', format_size($memoryLimitInBytes, 0), - BOX_MEMORY_LIMIT, + Constants::MEMORY_LIMIT, format_size($userDefinedMemoryLimit, 0), ), ); @@ -144,7 +144,7 @@ private function bumpMemoryLimit(): void private static function getUserDefinedMemoryLimit(): ?int { - $memoryLimit = getenv(BOX_MEMORY_LIMIT); + $memoryLimit = getenv(Constants::MEMORY_LIMIT); if (false === $memoryLimit) { $memoryLimitInBytes = null; diff --git a/src/consts.php b/src/Constants.php similarity index 51% rename from src/consts.php rename to src/Constants.php index ff248eedd..036758689 100644 --- a/src/consts.php +++ b/src/Constants.php @@ -14,13 +14,14 @@ namespace KevinGH\Box; -// Public constants pointing to environment variable names -const BOX_MEMORY_LIMIT = 'BOX_MEMORY_LIMIT'; -const BOX_ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG'; +final class Constants +{ + use NotInstantiable; -/** - * @internal - * - * @private - */ -const _NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING'; + public const MEMORY_LIMIT = 'BOX_MEMORY_LIMIT'; + public const ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG'; + public const BIN = 'BOX_BIN'; + + /** @internal */ + public const NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING'; +} diff --git a/src/Phar/PharInfo.php b/src/Phar/PharInfo.php index ed10a7e0a..5173c04a2 100644 --- a/src/Phar/PharInfo.php +++ b/src/Phar/PharInfo.php @@ -45,6 +45,7 @@ use Fidry\FileSystem\FS; use KevinGH\Box\Console\Command\Extract; +use KevinGH\Box\Constants; use OutOfBoundsException; use Phar; use RuntimeException; @@ -347,8 +348,7 @@ private static function getPhpExecutable(): string private static function getBoxBin(): string { - // TODO: move the constraint strings declaration in one place - return getenv('BOX_BIN') ?: $_SERVER['SCRIPT_NAME']; + return getenv(Constants::BIN) ?: $_SERVER['SCRIPT_NAME']; } /** diff --git a/src/functions.php b/src/functions.php index b8e616b7e..6b8f05019 100644 --- a/src/functions.php +++ b/src/functions.php @@ -183,8 +183,8 @@ class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component */ function disable_parallel_processing(): void { - if (false === defined(_NO_PARALLEL_PROCESSING)) { - define(_NO_PARALLEL_PROCESSING, true); + if (false === defined(Constants::NO_PARALLEL_PROCESSING)) { + define(Constants::NO_PARALLEL_PROCESSING, true); } } @@ -193,7 +193,7 @@ function disable_parallel_processing(): void */ function is_parallel_processing_enabled(): bool { - return false === defined(_NO_PARALLEL_PROCESSING) || false === constant(_NO_PARALLEL_PROCESSING); + return false === defined(Constants::NO_PARALLEL_PROCESSING) || false === constant(Constants::NO_PARALLEL_PROCESSING); } /**