From bcc6f45eb464b8a87dd62d76611fff56fd23d79f Mon Sep 17 00:00:00 2001 From: Netkas Date: Sun, 22 Oct 2023 19:26:16 -0400 Subject: [PATCH] Fix division by zero in ConsoleProgressBar The rendering of the progress bar was causing a division by zero error when the maximum value was set to 0. This change adds a condition to check if the maximum value is not 0 before calculating the number of hashes and the percentage done in the progress bar. This avoids the division by zero error and makes the progress bar rendering more robust. The CHANGELOG.md file has also been updated to reflect this bug fix. --- CHANGELOG.md | 1 + src/ncc/Utilities/ConsoleProgressBar.php | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75092a6..ba812a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This update introduces minor bug fixes. ### Fixed - Improve build efficiency by preventing duplicate merges - Updated file tracking in Runtime class + - Fixed division by zero in ConsoleProgressBar ## [2.0.3] - 2023-10-17 diff --git a/src/ncc/Utilities/ConsoleProgressBar.php b/src/ncc/Utilities/ConsoleProgressBar.php index 09ecaa6..b98f781 100644 --- a/src/ncc/Utilities/ConsoleProgressBar.php +++ b/src/ncc/Utilities/ConsoleProgressBar.php @@ -368,10 +368,16 @@ */ private function renderProgressBar(): string { - $hashes_count = round($this->progress_width * $this->value / $this->max_value); - $dashes_count = $this->progress_width - $hashes_count; - $percent_done = round($this->value * 100 / $this->max_value); + $hashes_count = 0; + $percent_done = 0; + if($this->max_value !== 0) + { + $hashes_count = round($this->progress_width * $this->value / $this->max_value); + $percent_done = round($this->value * 100 / $this->max_value); + } + + $dashes_count = $this->progress_width - $hashes_count; return ' [' . str_repeat('#', $hashes_count) . str_repeat('-', $dashes_count) . ']' . sprintf('%4s', $percent_done) . '%'; }