Passed
Push — master ( ac5b96...b01a48 )
by Mark
08:23
created

Sum::product()   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 0
Metric Value
cc 9
eloc 16
nc 15
nop 1
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
7
class Sum
8
{
9
    /**
10
     * SUM, ignoring non-numeric non-error strings. This is eventually used by SUMIF.
11
     *
12
     * SUM computes the sum of all the values and cells referenced in the argument list.
13
     *
14
     * Excel Function:
15
     *        SUM(value1[,value2[, ...]])
16
     *
17
     * @param mixed ...$args Data values
18
     *
19
     * @return float|string
20
     */
21 28
    public static function sumIgnoringStrings(...$args)
22
    {
23 28
        $returnValue = 0;
24
25
        // Loop through the arguments
26 28
        foreach (Functions::flattenArray($args) as $arg) {
27
            // Is it a numeric value?
28 28
            if (is_numeric($arg)) {
29 27
                $returnValue += $arg;
30 4
            } elseif (Functions::isError($arg)) {
31 1
                return $arg;
32
            }
33
        }
34
35 27
        return $returnValue;
36
    }
37
38
    /**
39
     * SUM, returning error for non-numeric strings. This is used by Excel SUM function.
40
     *
41
     * SUM computes the sum of all the values and cells referenced in the argument list.
42
     *
43
     * Excel Function:
44
     *        SUM(value1[,value2[, ...]])
45
     *
46
     * @param mixed ...$args Data values
47
     *
48
     * @return float|string
49
     */
50 155
    public static function sumErroringStrings(...$args)
51
    {
52 155
        $returnValue = 0;
53
        // Loop through the arguments
54 155
        $aArgs = Functions::flattenArrayIndexed($args);
55 155
        foreach ($aArgs as $k => $arg) {
56
            // Is it a numeric value?
57 155
            if (is_numeric($arg) || empty($arg)) {
58 144
                if (is_string($arg)) {
59 17
                    $arg = (int) $arg;
60
                }
61 144
                $returnValue += $arg;
62 17
            } elseif (is_bool($arg)) {
63 3
                $returnValue += (int) $arg;
64 15
            } elseif (Functions::isError($arg)) {
65 11
                return $arg;
66
            // ignore non-numerics from cell, but fail as literals (except null)
67 4
            } elseif ($arg !== null && !Functions::isCellValue($k)) {
68 1
                return Functions::VALUE();
69
            }
70
        }
71
72 143
        return $returnValue;
73
    }
74
75
    /**
76
     * SUMPRODUCT.
77
     *
78
     * Excel Function:
79
     *        SUMPRODUCT(value1[,value2[, ...]])
80
     *
81
     * @param mixed ...$args Data values
82
     *
83
     * @return float|string The result, or a string containing an error
84
     */
85 14
    public static function product(...$args)
86
    {
87 14
        $arrayList = $args;
88
89 14
        $wrkArray = Functions::flattenArray(array_shift($arrayList));
90 14
        $wrkCellCount = count($wrkArray);
91
92 14
        for ($i = 0; $i < $wrkCellCount; ++$i) {
93 14
            if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
94 3
                $wrkArray[$i] = 0;
95
            }
96
        }
97
98 14
        foreach ($arrayList as $matrixData) {
99 14
            $array2 = Functions::flattenArray($matrixData);
100 14
            $count = count($array2);
101 14
            if ($wrkCellCount != $count) {
102 1
                return Functions::VALUE();
103
            }
104
105 13
            foreach ($array2 as $i => $val) {
106 13
                if ((!is_numeric($val)) || (is_string($val))) {
107 3
                    $val = 0;
108
                }
109 13
                $wrkArray[$i] *= $val;
110
            }
111
        }
112
113 13
        return array_sum($wrkArray);
114
    }
115
}
116