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

SumProduct::funcSumProduct()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
7
class SumProduct
8
{
9
    /**
10
     * SUMPRODUCT.
11
     *
12
     * Excel Function:
13
     *        SUMPRODUCT(value1[,value2[, ...]])
14
     *
15
     * @param mixed ...$args Data values
16
     *
17
     * @return float|string The result, or a string containing an error
18
     */
19 14
    public static function funcSumProduct(...$args)
20
    {
21 14
        $arrayList = $args;
22
23 14
        $wrkArray = Functions::flattenArray(array_shift($arrayList));
24 14
        $wrkCellCount = count($wrkArray);
25
26 14
        for ($i = 0; $i < $wrkCellCount; ++$i) {
27 14
            if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
28 3
                $wrkArray[$i] = 0;
29
            }
30
        }
31
32 14
        foreach ($arrayList as $matrixData) {
33 14
            $array2 = Functions::flattenArray($matrixData);
34 14
            $count = count($array2);
35 14
            if ($wrkCellCount != $count) {
36 1
                return Functions::VALUE();
37
            }
38
39 13
            foreach ($array2 as $i => $val) {
40 13
                if ((!is_numeric($val)) || (is_string($val))) {
41 3
                    $val = 0;
42
                }
43 13
                $wrkArray[$i] *= $val;
44
            }
45
        }
46
47 13
        return array_sum($wrkArray);
48
    }
49
}
50