From 38b1dc397c7946f0173a9587a3c9409e2b490ea2 Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Sat, 13 Nov 2021 23:35:06 +0200 Subject: [PATCH] Codestyle --- src/CliApplication.php | 8 ++--- src/CliCommand.php | 8 ++--- src/CliCommandMultiProc.php | 28 ++++++++-------- src/Helper.php | 28 ++++++++-------- src/Icons.php | 2 +- src/ProgressBar.php | 66 ++++++++++++++++++------------------- 6 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/CliApplication.php b/src/CliApplication.php index 8134ceb..4b0b0e3 100644 --- a/src/CliApplication.php +++ b/src/CliApplication.php @@ -46,7 +46,7 @@ public function registerCommandsByPath( string $gloabalNamespace, bool $strictMode = true ): bool { - if ($strictMode && !is_dir($commandsDir)) { + if ($strictMode && !\is_dir($commandsDir)) { throw new Exception('First argument is not directory!'); } @@ -57,16 +57,16 @@ public function registerCommandsByPath( } foreach ($files as $file) { - if (!file_exists($file)) { + if (!\file_exists($file)) { continue; } require_once $file; - $taskNamespace = trim(str_replace('/', '\\', (string)strstr(\dirname($file), 'Commands'))); + $taskNamespace = \trim(\str_replace('/', '\\', (string)\strstr(\dirname($file), 'Commands'))); $commandClassName = "{$gloabalNamespace}\\{$taskNamespace}\\" . FS::filename($file); - if (class_exists($commandClassName)) { + if (\class_exists($commandClassName)) { $reflection = new \ReflectionClass($commandClassName); } else { throw new Exception("Command/Class \"{$commandClassName}\" can'be loaded from the file \"{$file}\""); diff --git a/src/CliCommand.php b/src/CliCommand.php index 72d8738..a882a22 100644 --- a/src/CliCommand.php +++ b/src/CliCommand.php @@ -139,7 +139,7 @@ protected function getOpt(string $optionName, bool $canBeArray = true) { $value = $this->input->getOption($optionName); - if ($canBeArray && is_array($value)) { + if ($canBeArray && \is_array($value)) { return Arr::last($value); } @@ -207,8 +207,8 @@ protected static function getStdIn(): ?string if (null === $result) { $result = ''; - while (!feof(STDIN)) { - $result .= fread(STDIN, 1024); + while (!\feof(\STDIN)) { + $result .= \fread(\STDIN, 1024); } } @@ -253,7 +253,7 @@ private function showProfiler(): void [$totalTime, $curMemory, $maxMemory] = $this->helper->getProfileDate(); - $this->_(implode('; ', [ + $this->_(\implode('; ', [ "Memory Usage/Peak: {$curMemory}/{$maxMemory}", "Execution Time: {$totalTime} sec" ])); diff --git a/src/CliCommandMultiProc.php b/src/CliCommandMultiProc.php index 846ef86..4d586ca 100644 --- a/src/CliCommandMultiProc.php +++ b/src/CliCommandMultiProc.php @@ -148,13 +148,13 @@ protected function executeMultiProcessAction(): int $this->afterFinishAllProcesses($this->procPool); $errorList = $this->getErrorList(); - if (count($errorList) > 0) { - throw new Exception(implode("\n" . str_repeat('-', 60) . "\n", $errorList)); + if (\count($errorList) > 0) { + throw new Exception(\implode("\n" . \str_repeat('-', 60) . "\n", $errorList)); } $warningList = $this->getWarningList(); - if (count($warningList) > 0) { - $this->_(implode("\n" . str_repeat('-', 60) . "\n", $warningList), 'warn'); + if (\count($warningList) > 0) { + $this->_(\implode("\n" . \str_repeat('-', 60) . "\n", $warningList), 'warn'); } return 0; @@ -213,7 +213,7 @@ private function initProcManager( private function createSubProcess(string $procId): Process { // Prepare option list from the parent process - $options = array_filter($this->input->getOptions(), static function ($optionValue): bool { + $options = \array_filter($this->input->getOptions(), static function ($optionValue): bool { return $optionValue !== false && $optionValue !== ''; }); @@ -228,7 +228,7 @@ private function createSubProcess(string $procId): Process $argumentsList = []; foreach ($arguments as $argKey => $argValue) { - if (is_array($argValue)) { + if (\is_array($argValue)) { continue; } @@ -241,11 +241,11 @@ private function createSubProcess(string $procId): Process // Build full command line $process = Process::fromShellCommandline( - Cli::build(implode(' ', [ + Cli::build(\implode(' ', [ Sys::getBinary(), Helper::getBinPath(), $this->getName(), - implode(" ", $argumentsList) + \implode(" ", $argumentsList) ]), $options), Helper::getRootPath(), null, @@ -253,7 +253,7 @@ private function createSubProcess(string $procId): Process $this->getMaxTimeout() ); - $this->procPool[spl_object_id($process)] = [ + $this->procPool[\spl_object_id($process)] = [ 'command' => $process->getCommandLine(), 'proc_id' => $procId, 'exit_code' => null, @@ -272,15 +272,15 @@ private function createSubProcess(string $procId): Process */ private function getErrorList(): array { - return array_reduce($this->procPool, function (array $acc, array $procInfo): array { + return \array_reduce($this->procPool, function (array $acc, array $procInfo): array { if ($procInfo['reached_timeout']) { - $acc[] = implode("\n", [ + $acc[] = \implode("\n", [ "Command : {$procInfo['command']}", "Error : The process with ID \"{$procInfo['proc_id']}\"" . " exceeded the timeout of {$this->getMaxTimeout()} seconds.", ]); } elseif ($procInfo['err_out'] && $procInfo['exit_code'] > 0) { - $acc[] = implode("\n", [ + $acc[] = \implode("\n", [ "Command : {$procInfo['command']}", "Code : {$procInfo['exit_code']}", "Error : {$procInfo['err_out']}", @@ -297,9 +297,9 @@ private function getErrorList(): array */ private function getWarningList(): array { - return array_reduce($this->procPool, static function (array $acc, array $procInfo): array { + return \array_reduce($this->procPool, static function (array $acc, array $procInfo): array { if ($procInfo['err_out'] && $procInfo['exit_code'] === 0) { - $acc[] = implode("\n", [ + $acc[] = \implode("\n", [ "Command : {$procInfo['command']}", "Warning : {$procInfo['err_out']}", "StdOut : {$procInfo['std_out']}", diff --git a/src/Helper.php b/src/Helper.php index 05b853f..5d64282 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -67,7 +67,7 @@ class Helper */ public function __construct(InputInterface $input, OutputInterface $output) { - $this->startTimer = microtime(true); + $this->startTimer = \microtime(true); $this->input = $input; $this->output = self::addOutputStyles($output); @@ -123,7 +123,7 @@ public function getErrOutput(): OutputInterface */ public static function getRootPath(): string { - $rootPath = defined('JBZOO_PATH_ROOT') ? (string)JBZOO_PATH_ROOT : null; + $rootPath = \defined('JBZOO_PATH_ROOT') ? (string)\JBZOO_PATH_ROOT : null; if (!$rootPath) { return Env::string('JBZOO_PATH_ROOT'); } @@ -136,7 +136,7 @@ public static function getRootPath(): string */ public static function getBinPath(): string { - $binPath = defined('JBZOO_PATH_BIN') ? (string)JBZOO_PATH_BIN : null; + $binPath = \defined('JBZOO_PATH_BIN') ? (string)\JBZOO_PATH_BIN : null; if (!$binPath) { return Env::string('JBZOO_PATH_BIN'); } @@ -171,9 +171,9 @@ public static function addOutputStyles(OutputInterface $output): OutputInterface public function getProfileDate(): array { return [ - number_format(microtime(true) - $this->startTimer, 3), - FS::format(memory_get_usage(false)), - FS::format(memory_get_peak_usage(false)), + \number_format(\microtime(true) - $this->startTimer, 3), + FS::format(\memory_get_usage(false)), + FS::format(\memory_get_peak_usage(false)), ]; } @@ -190,7 +190,7 @@ public function _($messages, string $verboseLevel = ''): void { $verboseLevel = \strtolower(\trim($verboseLevel)); - if (is_array($messages)) { + if (\is_array($messages)) { foreach ($messages as $message) { $this->_($message, $verboseLevel); } @@ -254,7 +254,7 @@ public static function findMaxLength(array $items): int { $result = 0; foreach ($items as $item) { - $tmpMax = strlen($item); + $tmpMax = \strlen($item); if ($result < $tmpMax) { $result = $tmpMax; } @@ -269,19 +269,19 @@ public static function findMaxLength(array $items): int */ public static function renderList(array $metrics): string { - $maxLength = self::findMaxLength(array_keys($metrics)); + $maxLength = self::findMaxLength(\array_keys($metrics)); $lines = []; foreach ($metrics as $metricKey => $metricTmpl) { - $currentLength = strlen($metricKey); - $lines[] = implode('', [ + $currentLength = \strlen((string)$metricKey); + $lines[] = \implode('', [ $metricKey, - str_repeat(' ', $maxLength - $currentLength), + \str_repeat(' ', $maxLength - $currentLength), ': ', - implode('; ', (array)$metricTmpl) + \implode('; ', (array)$metricTmpl) ]); } - return implode("\n", array_filter($lines)) . "\n"; + return \implode("\n", \array_filter($lines)) . "\n"; } } diff --git a/src/Icons.php b/src/Icons.php index e5493da..f957fcd 100644 --- a/src/Icons.php +++ b/src/Icons.php @@ -161,7 +161,7 @@ public static function getRandomIcon(string $group, bool $isDecorated): string } $icons = self::$icons[$group]; - shuffle($icons); + \shuffle($icons); return $icons[0]; } diff --git a/src/ProgressBar.php b/src/ProgressBar.php index ac1dd95..16f80bb 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -196,7 +196,7 @@ public static function run( ->setCallback($callback) ->setThrowBatchException($throwBatchException); - if (is_iterable($listOrMax)) { + if (\is_iterable($listOrMax)) { $progress->setList($listOrMax); } else { $progress->setMax($listOrMax); @@ -234,23 +234,23 @@ public function execute(): bool foreach ($this->list as $stepIndex => $stepValue) { $this->setStep($stepIndex, $currentIndex); - $startTime = microtime(true); - $startMemory = memory_get_usage(false); + $startTime = \microtime(true); + $startMemory = \memory_get_usage(false); [$stepResult, $exceptionMessage] = $this->handleOneStep($stepValue, $stepIndex, $currentIndex); - $this->stepMemoryDiff[] = memory_get_usage(false) - $startMemory; - $this->stepTimers[] = microtime(true) - $startTime; + $this->stepMemoryDiff[] = \memory_get_usage(false) - $startMemory; + $this->stepTimers[] = \microtime(true) - $startTime; - $exceptionMessages = array_merge($exceptionMessages, (array)$exceptionMessage); + $exceptionMessages = \array_merge($exceptionMessages, (array)$exceptionMessage); if ($this->progressBar) { - $errorNumbers = count($exceptionMessages); + $errorNumbers = \count($exceptionMessages); $errMessage = $errorNumbers > 0 ? "{$errorNumbers}" : '0'; $this->progressBar->setMessage($errMessage, 'jbzoo_caught_exceptions'); } - if (strpos($stepResult, self::BREAK) !== false) { + if (\strpos($stepResult, self::BREAK) !== false) { $isSkipped = true; break; } @@ -301,7 +301,7 @@ private function handleOneStep($stepValue, $stepIndex, int $currentIndex): array // Executing callback try { - $callbackResults = (array)call_user_func($this->callback, $stepValue, $stepIndex, $currentIndex); + $callbackResults = (array)\call_user_func($this->callback, $stepValue, $stepIndex, $currentIndex); } catch (\Exception $exception) { if ($this->throwBatchException) { $errorMessage = 'Error. ' . $exception->getMessage(); @@ -315,12 +315,12 @@ private function handleOneStep($stepValue, $stepIndex, int $currentIndex): array // Handle status messages $stepResult = ''; if (!empty($callbackResults)) { - $stepResult = str_replace(["\n", "\r", "\t"], ' ', implode('; ', $callbackResults)); + $stepResult = \str_replace(["\n", "\r", "\t"], ' ', \implode('; ', $callbackResults)); if ($this->progressBar) { if ($stepResult) { - if (strlen(strip_tags($stepResult)) > self::MAX_LINE_LENGTH) { - $stepResult = Str::limitChars(strip_tags($stepResult), self::MAX_LINE_LENGTH); + if (\strlen(\strip_tags($stepResult)) > self::MAX_LINE_LENGTH) { + $stepResult = Str::limitChars(\strip_tags($stepResult), self::MAX_LINE_LENGTH); } $this->progressBar->setMessage($stepResult); @@ -382,20 +382,20 @@ private function buildTemplate(): string } if ($this->output->isVeryVerbose()) { - $progressBarLines[] = implode(' ', [ + $progressBarLines[] = \implode(' ', [ $percent, $steps, $bar, $this->finishIcon, ]); - $footerLine['Time (pass/left/est/avg/last)'] = implode(' / ', [ + $footerLine['Time (pass/left/est/avg/last)'] = \implode(' / ', [ '%jbzoo_time_elapsed:9s%', '%jbzoo_time_remaining:8s%', '%jbzoo_time_estimated:8s%', '%jbzoo_time_step_avg%', '%jbzoo_time_step_last%', ]); - $footerLine['Memory (cur/peak/limit/leak/last)'] = implode(' / ', [ + $footerLine['Memory (cur/peak/limit/leak/last)'] = \implode(' / ', [ '%jbzoo_memory_current:8s%', '%jbzoo_memory_peak%', '%jbzoo_memory_limit%', @@ -404,7 +404,7 @@ private function buildTemplate(): string ]); $footerLine['Caught exceptions'] = '%jbzoo_caught_exceptions%'; } elseif ($this->output->isVerbose()) { - $progressBarLines[] = implode(' ', [ + $progressBarLines[] = \implode(' ', [ $percent, $steps, $bar, @@ -412,14 +412,14 @@ private function buildTemplate(): string '%jbzoo_memory_current:8s%' ]); - $footerLine['Time (pass/left/est)'] = implode(' / ', [ + $footerLine['Time (pass/left/est)'] = \implode(' / ', [ '%jbzoo_time_elapsed:8s%', '%jbzoo_time_remaining:8s%', '%jbzoo_time_estimated%' ]); $footerLine['Caught exceptions'] = '%jbzoo_caught_exceptions%'; } else { - $progressBarLines[] = implode(' ', [ + $progressBarLines[] = \implode(' ', [ $percent, $steps, $bar, @@ -430,7 +430,7 @@ private function buildTemplate(): string $footerLine['Last Step Message'] = '%message%'; - return implode("\n", $progressBarLines) . "\n" . Helper::renderList($footerLine) . "\n"; + return \implode("\n", $progressBarLines) . "\n" . Helper::renderList($footerLine) . "\n"; } /** @@ -448,11 +448,11 @@ private function configureProgressBar(): void // Memory self::setPlaceholder('jbzoo_memory_current', static function (): string { - return SymfonyHelper::formatMemory(memory_get_usage(false)); + return SymfonyHelper::formatMemory(\memory_get_usage(false)); }); self::setPlaceholder('jbzoo_memory_peak', static function (): string { - return SymfonyHelper::formatMemory(memory_get_peak_usage(false)); + return SymfonyHelper::formatMemory(\memory_get_peak_usage(false)); }); self::setPlaceholder('jbzoo_memory_limit', static function (): string { @@ -460,7 +460,7 @@ private function configureProgressBar(): void }); self::setPlaceholder('jbzoo_memory_step_avg', function (SymfonyProgressBar $bar): string { - if (!$bar->getMaxSteps() || !$bar->getProgress() || count($this->stepMemoryDiff) === 0) { + if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) { return 'n/a'; } @@ -468,7 +468,7 @@ private function configureProgressBar(): void }); self::setPlaceholder('jbzoo_memory_step_last', function (SymfonyProgressBar $bar): string { - if (!$bar->getMaxSteps() || !$bar->getProgress() || count($this->stepMemoryDiff) === 0) { + if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) { return 'n/a'; } @@ -477,7 +477,7 @@ private function configureProgressBar(): void // Timers self::setPlaceholder('jbzoo_time_elapsed', static function (SymfonyProgressBar $bar): string { - return Dates::formatTime(time() - $bar->getStartTime()); + return Dates::formatTime(\time() - $bar->getStartTime()); }); self::setPlaceholder('jbzoo_time_remaining', static function (SymfonyProgressBar $bar): string { @@ -488,8 +488,8 @@ private function configureProgressBar(): void if (!$bar->getProgress()) { $remaining = 0; } else { - $remaining = round( - (time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()) + $remaining = \round( + (\time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()) ); } @@ -504,22 +504,22 @@ private function configureProgressBar(): void if (!$bar->getProgress()) { $estimated = 0; } else { - $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps()); + $estimated = \round((\time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps()); } return Dates::formatTime($estimated); }); self::setPlaceholder('jbzoo_time_step_avg', function (SymfonyProgressBar $bar): string { - if (!$bar->getMaxSteps() || !$bar->getProgress() || count($this->stepTimers) === 0) { + if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) { return 'n/a'; } - return str_replace('±', '±', Stats::renderAverage($this->stepTimers)) . ' sec'; + return \str_replace('±', '±', Stats::renderAverage($this->stepTimers)) . ' sec'; }); self::setPlaceholder('jbzoo_time_step_last', function (SymfonyProgressBar $bar): string { - if (!$bar->getMaxSteps() || !$bar->getProgress() || count($this->stepTimers) === 0) { + if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) { return 'n/a'; } @@ -541,9 +541,9 @@ public static function setPlaceholder(string $name, callable $callable): void */ private static function showListOfExceptions(array $exceptionMessages): void { - if (count($exceptionMessages)) { - $listOfErrors = implode("\n", $exceptionMessages) . "\n"; - $listOfErrors = str_replace('Error. ', '', $listOfErrors); + if (\count($exceptionMessages)) { + $listOfErrors = \implode("\n", $exceptionMessages) . "\n"; + $listOfErrors = \str_replace('Error. ', '', $listOfErrors); throw new Exception("\n Error list:\n" . $listOfErrors); }