From 10d3576acefabc8db2a79b4b6f27856df27ea977 Mon Sep 17 00:00:00 2001 From: Nick Been <66799549+nickbeen@users.noreply.github.com> Date: Fri, 24 Jan 2025 18:47:28 +0100 Subject: [PATCH 1/3] Fix division by zero error --- src/ProgressBar.php | 2 +- tests/ProgressBarTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ProgressBar.php b/src/ProgressBar.php index 74114f7..aaac597 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -239,7 +239,7 @@ public function setProgress(int|float $progress): ProgressBar $this->setMaxProgress($progress); } - $this->progress = max(0, $progress); + $this->progress = max(1, $progress); $this->setPercentage(); diff --git a/tests/ProgressBarTest.php b/tests/ProgressBarTest.php index 55f6516..1e6b192 100644 --- a/tests/ProgressBarTest.php +++ b/tests/ProgressBarTest.php @@ -148,4 +148,16 @@ public function it_cannot_set_incorrect_max_progress() $this->assertEquals(1, $progressBar->getMaxProgress()); } + + /** + * @test + */ + public function it_can_gracefully_handle_zero_progress() + { + $progressBar = new ProgressBar(maxProgress: 0); + + $progressBar->tick(0); + + $this->assertEquals(100, $progressBar->getPercentage()); + } } From aebefdec417acdf11f3e3b6d537639b0ac367a50 Mon Sep 17 00:00:00 2001 From: Nick Been <66799549+nickbeen@users.noreply.github.com> Date: Fri, 24 Jan 2025 20:36:19 +0100 Subject: [PATCH 2/3] Fix failing tests and correct percentage at zero progress --- src/ProgressBar.php | 19 +++++++++++++++++-- tests/ProgressBarTest.php | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ProgressBar.php b/src/ProgressBar.php index aaac597..a4ea96f 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -31,6 +31,12 @@ final class ProgressBar /** Time when progress bar starts displaying */ private int $startTime; + /** + * Indicator whether maxProgress was truly set as zero. + * MaxProgress must always be at least one to be displayed. + */ + private bool $maxProgressIsZero = false; + /** * Initialize and set progress and maxProgress when available. * Use separate setProgress() and setMaxProgress() methods when undetermined during initialization. @@ -204,7 +210,7 @@ private function setEstimatedTime(): ProgressBar $this->estimatedTime = round( ( time() - $this->startTime - ) / $this->progress * ($this->maxProgress - $this->progress) + ) / max(1, $this->progress) * ($this->maxProgress - $this->progress) ); return $this; @@ -215,6 +221,10 @@ private function setEstimatedTime(): ProgressBar */ public function setMaxProgress(int|float $maxProgress): ProgressBar { + if ($maxProgress == 0) { + $this->maxProgressIsZero = true; + } + $this->maxProgress = max(1, $maxProgress); return $this; @@ -239,7 +249,7 @@ public function setProgress(int|float $progress): ProgressBar $this->setMaxProgress($progress); } - $this->progress = max(1, $progress); + $this->progress = max(0, $progress); $this->setPercentage(); @@ -274,6 +284,11 @@ public function start(): void */ public function tick(int $progress = 1): void { + if ($progress == 0 && $this->maxProgressIsZero) { + $this->finish(); + return; + } + $this->setProgress($this->progress + $progress) ->setEstimatedTime(); diff --git a/tests/ProgressBarTest.php b/tests/ProgressBarTest.php index 1e6b192..90551ed 100644 --- a/tests/ProgressBarTest.php +++ b/tests/ProgressBarTest.php @@ -155,8 +155,10 @@ public function it_cannot_set_incorrect_max_progress() public function it_can_gracefully_handle_zero_progress() { $progressBar = new ProgressBar(maxProgress: 0); + $this->assertEquals(1, $progressBar->getMaxProgress()); $progressBar->tick(0); + $this->assertEquals(1, $progressBar->getProgress()); $this->assertEquals(100, $progressBar->getPercentage()); } From 014ae3f9ff7409747ea5890120c62e50796ec9a1 Mon Sep 17 00:00:00 2001 From: nickbeen <66799549+nickbeen@users.noreply.github.com> Date: Fri, 24 Jan 2025 19:36:51 +0000 Subject: [PATCH 3/3] Apply php-cs-fixer changes --- src/ProgressBar.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ProgressBar.php b/src/ProgressBar.php index a4ea96f..d4a1824 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -286,6 +286,7 @@ public function tick(int $progress = 1): void { if ($progress == 0 && $this->maxProgressIsZero) { $this->finish(); + return; }