Passed
Push — master ( c380b2...9239b3 )
by Adrien
10:06
created

SeriesSum   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A funcSeriesSum() 0 23 4
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
8
class SeriesSum
9
{
10
    /**
11
     * SERIESSUM.
12
     *
13
     * Returns the sum of a power series
14
     *
15
     * @param mixed $x Input value
16
     * @param mixed $n Initial power
17
     * @param mixed $m Step
18
     * @param mixed[] $args An array of coefficients for the Data Series
19
     *
20
     * @return float|string The result, or a string containing an error
21
     */
22 15
    public static function funcSeriesSum($x, $n, $m, ...$args)
23
    {
24
        try {
25 15
            $x = Helpers::validateNumericNullSubstitution($x, 0);
26 13
            $n = Helpers::validateNumericNullSubstitution($n, 0);
27 12
            $m = Helpers::validateNumericNullSubstitution($m, 0);
28
29
            // Loop through arguments
30 11
            $aArgs = Functions::flattenArray($args);
31 11
            $returnValue = 0;
32 11
            $i = 0;
33 11
            foreach ($aArgs as $argx) {
34 11
                if ($argx !== null) {
35 11
                    $arg = Helpers::validateNumericNullSubstitution($argx, 0);
36 11
                    $returnValue += $arg * $x ** ($n + ($m * $i));
37 11
                    ++$i;
38
                }
39
            }
40 6
        } catch (Exception $e) {
41 6
            return $e->getMessage();
42
        }
43
44 9
        return $returnValue;
45
    }
46
}
47