From 5a88437387663023c7a276aadfbd4ad2797571bc Mon Sep 17 00:00:00 2001 From: Stephen Hamilton Date: Wed, 8 Oct 2025 16:20:10 +0100 Subject: [PATCH] Fixes division by zero error. --- src/SparkLineEntry.php | 4 ++++ tests/SparkLineTest.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/SparkLineEntry.php b/src/SparkLineEntry.php index 74a380d..2683018 100644 --- a/src/SparkLineEntry.php +++ b/src/SparkLineEntry.php @@ -13,6 +13,10 @@ public function __construct( public function rebase(int $base, int $max): self { + if($max === 0) { + return $this; + } + return new self( count: (int) floor($this->count * ($base / $max)), ); diff --git a/tests/SparkLineTest.php b/tests/SparkLineTest.php index 47645ff..a32e836 100644 --- a/tests/SparkLineTest.php +++ b/tests/SparkLineTest.php @@ -6,6 +6,7 @@ use Brendt\SparkLine\SparkLine; use Brendt\SparkLine\SparkLineEntry; +use DivisionByZeroError; use PHPUnit\Framework\TestCase; final class SparkLineTest extends TestCase @@ -70,4 +71,12 @@ public function test_get_total(): void $this->assertEquals(3, $sparkLine->getTotal()); } + + /** @test */ + public function test_entries_all_with_zero_values_doesnt_cause_error(): void + { + $sparkLine = (new SparkLine(0, 0, 0, 0))->make(); + + $this->assertStringContainsString('