Passed
Push — master ( bd792e...df3a06 )
by
unknown
15:51 queued 08:04
created

TorowTocol::torow()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 11.353

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 32
ccs 18
cts 21
cp 0.8571
rs 7.3166
cc 11
nc 12
nop 3
crap 11.353

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9
class TorowTocol
10
{
11
    /**
12
     * Excel function TOCOL.
13
     *
14
     * @return mixed[]|string
15
     */
16 9
    public static function tocol(mixed $array, mixed $ignore = 0, mixed $byColumn = false): array|string
17
    {
18 9
        $result = self::torow($array, $ignore, $byColumn);
19 9
        if (is_array($result)) {
20 7
            return array_map((fn ($x) => [$x]), $result);
21
        }
22
23 2
        return $result;
24
    }
25
26
    /**
27
     * Excel function TOROW.
28
     *
29
     * @return mixed[]|string
30
     */
31 18
    public static function torow(mixed $array, mixed $ignore = 0, mixed $byColumn = false): array|string
32
    {
33 18
        if (!is_numeric($ignore)) {
34 2
            return ExcelError::VALUE();
35
        }
36 16
        $ignore = (int) $ignore;
37 16
        if ($ignore < 0 || $ignore > 3) {
38 2
            return ExcelError::VALUE();
39
        }
40 14
        if (is_int($byColumn) || is_float($byColumn)) {
41 2
            $byColumn = (bool) $byColumn;
42
        }
43 14
        if (!is_bool($byColumn)) {
44
            return ExcelError::VALUE();
45
        }
46 14
        if (!is_array($array)) {
47
            $array = [$array];
48
        }
49 14
        if ($byColumn) {
50 4
            $temp = [];
51 4
            foreach ($array as $row) {
52 4
                if (!is_array($row)) {
53
                    $row = [$row];
54
                }
55 4
                $temp[] = Functions::flattenArray($row);
56
            }
57 4
            $array = ChooseRowsEtc::transpose($temp);
58
        } else {
59 10
            $array = Functions::flattenArray($array);
60
        }
61
62 14
        return self::byRow($array, $ignore);
63
    }
64
65
    /**
66
     * @param mixed[] $array
67
     *
68
     * @return mixed[]
69
     */
70 14
    private static function byRow(array $array, int $ignore): array
71
    {
72 14
        $returnMatrix = [];
73 14
        foreach ($array as $row) {
74 14
            if (!is_array($row)) {
75 10
                $row = [$row];
76
            }
77 14
            foreach ($row as $cell) {
78 14
                if ($cell === null) {
79 14
                    if ($ignore === 1 || $ignore === 3) {
80 5
                        continue;
81
                    }
82 9
                    $cell = 0;
83 14
                } elseif (ErrorValue::isError($cell)) {
84 14
                    if ($ignore === 2 || $ignore === 3) {
85 5
                        continue;
86
                    }
87
                }
88 14
                $returnMatrix[] = $cell;
89
            }
90
        }
91
92 14
        return $returnMatrix;
93
    }
94
}
95