Skip to content

Commit

Permalink
Merge pull request #13857 from akemidx/bug/sc-20532
Browse files Browse the repository at this point in the history
Bug Fix:  Undefined array key 266
  • Loading branch information
snipe authored Nov 9, 2023
2 parents d2bfa9a + 49136a4 commit 9e1cfac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ public static function formatCurrencyOutput($cost)
*
* @author [A. Gianotto] [<[email protected]>]
* @since [v3.3]
* @return array
* @return string
*/
public static function defaultChartColors($index = 0)
public static function defaultChartColors(int $index = 0)
{
if ($index < 0) {
$index = 0;
}

$colors = [
'#008941',
'#FF4A46',
Expand Down Expand Up @@ -349,7 +353,19 @@ public static function defaultChartColors($index = 0)
$total_colors = count($colors);

if ($index >= $total_colors) {
$index = $index - $total_colors;

\Log::error('Status label count is '.$index.' and exceeds the allowed count of 266.');
//patch fix for array key overflow (color count starts at 1, array starts at 0)
$index = $index - $total_colors - 1;

//constraints to keep result in 0-265 range. This should never be needed, but if something happens
//to create this many status labels and it DOES happen, this will keep it from failing at least.
if($index < 0) {
$index = 0;
}
elseif($index >($total_colors - 1)) {
$index = $total_colors - 1;
}
}

return $colors[$index];
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Reports/CustomReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;


class CustomReportTest extends TestCase
{
use InteractsWithSettings;
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Helpers/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Unit\Helpers;

use App\Helpers\Helper;
use Tests\TestCase;

class HelperTest extends TestCase
{
public function testDefaultChartColorsMethodHandlesHighValues()
{
$this->assertIsString(Helper::defaultChartColors(1000));
}

public function testDefaultChartColorsMethodHandlesNegativeNumbers()
{
$this->assertIsString(Helper::defaultChartColors(-1));
}
}

0 comments on commit 9e1cfac

Please sign in to comment.