Failed Conditions
Pull Request — master (#4328)
by Owen
15:26 queued 04:43
created

Counts::COUNTBLANK()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.048

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 8.048
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
8
class Counts extends AggregateBase
9
{
10
    /**
11
     * COUNT.
12
     *
13
     * Counts the number of cells that contain numbers within the list of arguments
14
     *
15
     * Excel Function:
16
     *        COUNT(value1[,value2[, ...]])
17
     *
18
     * @param mixed ...$args Data values
19
     */
20 109
    public static function COUNT(mixed ...$args): int
21
    {
22 109
        $returnValue = 0;
23
24
        // Loop through arguments
25 109
        $aArgs = Functions::flattenArrayIndexed($args);
26 109
        foreach ($aArgs as $k => $arg) {
27 106
            $arg = self::testAcceptedBoolean($arg, $k);
28
            // Is it a numeric value?
29
            // Strings containing numeric values are only counted if they are string literals (not cell values)
30
            //    and then only in MS Excel and in Open Office, not in Gnumeric
31 106
            if (self::isAcceptedCountable($arg, $k, true)) {
32 103
                ++$returnValue;
33
            }
34
        }
35
36 109
        return $returnValue;
37
    }
38
39
    /**
40
     * COUNTA.
41
     *
42
     * Counts the number of cells that are not empty within the list of arguments
43
     *
44
     * Excel Function:
45
     *        COUNTA(value1[,value2[, ...]])
46
     *
47
     * @param mixed ...$args Data values
48
     */
49 19
    public static function COUNTA(mixed ...$args): int
50
    {
51 19
        $returnValue = 0;
52
53
        // Loop through arguments
54 19
        $aArgs = Functions::flattenArrayIndexed($args);
55 19
        foreach ($aArgs as $k => $arg) {
56
            // Nulls are counted if literals, but not if cell values
57 19
            if ($arg !== null || (!Functions::isCellValue($k))) {
58 19
                ++$returnValue;
59
            }
60
        }
61
62 19
        return $returnValue;
63
    }
64
65
    /**
66
     * COUNTBLANK.
67
     *
68
     * Counts the number of empty cells within the list of arguments
69
     *
70
     * Excel Function:
71
     *        COUNTBLANK(value1[,value2[, ...]])
72
     *
73
     * @param mixed $range Data values
74
     */
75 3
    public static function COUNTBLANK(mixed $range): int
76
    {
77 3
        if ($range === null) {
78
            return 1;
79
        }
80 3
        if (!is_array($range) || array_key_exists(0, $range)) {
81 1
            throw new CalcException('Must specify range of cells, not any kind of literal');
82
        }
83 3
        $returnValue = 0;
84
85
        // Loop through arguments
86 3
        $aArgs = Functions::flattenArray($range);
87 3
        foreach ($aArgs as $arg) {
88
            // Is it a blank cell?
89 3
            if (($arg === null) || ((is_string($arg)) && ($arg == ''))) {
90 3
                ++$returnValue;
91
            }
92
        }
93
94 3
        return $returnValue;
95
    }
96
}
97