Skip to content

Commit d462fbf

Browse files
committed
Fix Phpstan and Scrutinizer Problems
1 parent 263a397 commit d462fbf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/PhpSpreadsheet/Shared/Trend/Trend.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
44

5+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
6+
57
class Trend
68
{
79
const TREND_LINEAR = 'Linear';
@@ -46,7 +48,7 @@ class Trend
4648
*/
4749
private static array $trendCache = [];
4850

49-
public static function calculate(string $trendType = self::TREND_BEST_FIT, array $yValues = [], array $xValues = [], bool $const = true): mixed
51+
public static function calculate(string $trendType = self::TREND_BEST_FIT, array $yValues = [], array $xValues = [], bool $const = true): BestFit
5052
{
5153
// Calculate number of points in each dataset
5254
$nY = count($yValues);
@@ -57,7 +59,7 @@ public static function calculate(string $trendType = self::TREND_BEST_FIT, array
5759
$xValues = range(1, $nY);
5860
} elseif ($nY !== $nX) {
5961
// Ensure both arrays of points are the same size
60-
trigger_error('Trend(): Number of elements in coordinate arrays do not match.', E_USER_ERROR);
62+
throw new SpreadsheetException('Trend(): Number of elements in coordinate arrays do not match.');
6163
}
6264

6365
$key = md5($trendType . $const . serialize($yValues) . serialize($xValues));
@@ -113,7 +115,7 @@ public static function calculate(string $trendType = self::TREND_BEST_FIT, array
113115

114116
return $bestFit[$bestFitType];
115117
default:
116-
return false;
118+
throw new SpreadsheetException("Unknown trend type $trendType");
117119
}
118120
}
119121
}

tests/PhpSpreadsheetTests/Shared/Trend/BestFitTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,26 @@ public function testBestFit(): void
6363

6464
try {
6565
$type = Trend::TREND_BEST_FIT;
66-
$result = Trend::calculate($type, $yValues, $xValues);
66+
Trend::calculate($type, $yValues, [0, 1, 2]);
67+
self::fail('should have failed - mismatched number of elements');
68+
} catch (SpreadsheetException $e) {
69+
self::assertStringContainsString('Number of elements', $e->getMessage());
70+
}
71+
72+
try {
73+
$type = Trend::TREND_BEST_FIT;
74+
Trend::calculate($type, $yValues, $xValues);
6775
self::fail('should have failed - TREND_BEST_FIT includes polynomials which are not implemented yet');
6876
} catch (SpreadsheetException $e) {
6977
self::assertStringContainsString('not yet implemented', $e->getMessage());
7078
}
79+
80+
try {
81+
$type = 'unknown';
82+
Trend::calculate($type, $yValues, $xValues);
83+
self::fail('should have failed - invalid trend type');
84+
} catch (SpreadsheetException $e) {
85+
self::assertStringContainsString('Unknown trend type', $e->getMessage());
86+
}
7187
}
7288
}

0 commit comments

Comments
 (0)