Passed
Push — master ( 4bd506...ac5b96 )
by Adrien
09:31
created

Sum::sumIgnoringStrings()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
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