From a19259c6c7f274722457ae88c347bf4eb9edec48 Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Thu, 11 Apr 2024 03:49:53 +0400 Subject: [PATCH] Refactor debug mode handling with utility functions The code has been refactored to move the handling of the debug mode to a new utility class. This change allows us to set and get the debug mode via static methods, improving code clarity and encapsulation. The debug mode state is now part of the Utils class helping to maintain a cleaner global namespace. --- src/Commands/AbstractValidate.php | 4 +--- src/Utils.php | 14 +++++++++++++- src/Workers/WorkerPool.php | 9 ++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Commands/AbstractValidate.php b/src/Commands/AbstractValidate.php index 4c7827f8..d093cb09 100644 --- a/src/Commands/AbstractValidate.php +++ b/src/Commands/AbstractValidate.php @@ -110,9 +110,7 @@ protected function preparation(): void ); } - if ($this->getOptBool('debug')) { - \define('DEBUG_MODE', true); - } + Utils::setDebugMode($this->getOptBool('debug')); } protected function isHumanReadableMode(): bool diff --git a/src/Utils.php b/src/Utils.php index c37bb99e..bef78bae 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -29,6 +29,8 @@ final class Utils { public const MAX_DIRECTORY_DEPTH = 10; + private static bool $debugMode = false; + public static function isArrayInOrder(array $array, array $correctOrder): bool { $orderIndex = 0; @@ -72,7 +74,7 @@ public static function printList(null|array|bool|float|int|string $items, string public static function debug(int|string $message): void { - if (\defined('DEBUG_MODE')) { + if (self::$debugMode) { cli($message); } } @@ -484,6 +486,16 @@ public static function mergeConfigs(array ...$configs): array return $merged; } + public static function setDebugMode(bool $debugMode): void + { + self::$debugMode = $debugMode; + } + + public static function getDebugMode(): bool + { + return self::$debugMode; + } + /** * @param SplFileInfo[] $files */ diff --git a/src/Workers/WorkerPool.php b/src/Workers/WorkerPool.php index 80d2b16c..4237b07e 100644 --- a/src/Workers/WorkerPool.php +++ b/src/Workers/WorkerPool.php @@ -17,6 +17,7 @@ namespace JBZoo\CsvBlueprint\Workers; use Fidry\CpuCoreCounter\CpuCoreCounter; +use JBZoo\CsvBlueprint\Utils; use parallel\Runtime; final class WorkerPool @@ -136,9 +137,11 @@ private function maintainTaskPool(): void $worker = $this->tasksQueue->dequeue(); $runtime = new Runtime($bootstrap); $future = $runtime->run( - static fn (string $key, string $class, array $args): mixed => (new Worker($key, $class, $args)) - ->execute(), - [$worker->getKey(), $worker->getClass(), $worker->getArguments()], + static function (string $key, string $class, array $args, bool $debugMode): mixed { + Utils::setDebugMode($debugMode); + return (new Worker($key, $class, $args))->execute(); + }, + [$worker->getKey(), $worker->getClass(), $worker->getArguments(), Utils::getDebugMode()], ); $this->runningTasks[$worker->getKey()] = $future;