Skip to content

Commit

Permalink
Refactor debug mode handling with utility functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmetDenis committed Apr 10, 2024
1 parent bfce0b1 commit a19259c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/Commands/AbstractValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Workers/WorkerPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace JBZoo\CsvBlueprint\Workers;

use Fidry\CpuCoreCounter\CpuCoreCounter;
use JBZoo\CsvBlueprint\Utils;
use parallel\Runtime;

final class WorkerPool
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a19259c

Please sign in to comment.