A free open source IT asset/license management system
Fixed division by zero in new label engine
snipee7312801ac6f
diff --git a/app/Models/Labels/DefaultLabel.php b/app/Models/Labels/DefaultLabel.phpindex 21d0d97079..37b6a497ff 100644--- a/app/Models/Labels/DefaultLabel.php+++ b/app/Models/Labels/DefaultLabel.php@@ -68,17 +68,8 @@ class DefaultLabel extends RectangleSheet$usableWidth = $this->pageWidth - $this->pageMarginLeft - $this->pageMarginRight;$usableHeight = $this->pageHeight - $this->pageMarginTop - $this->pageMarginBottom;- $this->columns = ($usableWidth + $this->labelSpacingH) / ($this->labelWidth + $this->labelSpacingH);- $this->rows = ($usableHeight + $this->labelSpacingV) / ($this->labelHeight + $this->labelSpacingV);-- // Make sure the columns and rows are never zero, since that scenario should never happen- if ($this->columns == 0) {- $this->columns = 1;- }-- if ($this->rows == 0) {- $this->rows = 1;- }+ $this->columns = $this->calculateGridCount($usableWidth, $this->labelWidth, $this->labelSpacingH);+ $this->rows = $this->calculateGridCount($usableHeight, $this->labelHeight, $this->labelSpacingV);}@@ -299,4 +290,17 @@ class DefaultLabel extends RectangleSheetreturn $labelHeight;}++ private function calculateGridCount(float $usableSize, float $labelSize, float $spacing): int+ {+ $denominator = $labelSize + $spacing;++ if ($denominator <= 0.0) {+ return 1;+ }++ $count = (int) floor(($usableSize + $spacing) / $denominator);++ return max(1, $count);+ }}diff --git a/tests/Unit/Models/Labels/DefaultLabelTest.php b/tests/Unit/Models/Labels/DefaultLabelTest.phpindex 264c97c8c3..6298058a99 100644--- a/tests/Unit/Models/Labels/DefaultLabelTest.php+++ b/tests/Unit/Models/Labels/DefaultLabelTest.php@@ -35,4 +35,28 @@ class DefaultLabelTest extends TestCase// simply ensuring constructor didn't throw exception...$this->assertInstanceOf(DefaultLabel::class, new DefaultLabel);}++ public function test_handles_column_denominator_that_resolves_to_zero_gracefully()+ {+ $this->settings->set([+ 'labels_width' => 0.1,+ 'labels_display_sgutter' => -0.1,+ ]);++ $label = new DefaultLabel;++ $this->assertSame(1, $label->getColumns());+ }++ public function test_handles_row_denominator_that_resolves_to_zero_gracefully()+ {+ $this->settings->set([+ 'labels_height' => 0.1,+ 'labels_display_bgutter' => -0.1,+ ]);++ $label = new DefaultLabel;++ $this->assertSame(1, $label->getRows());+ }}