Passed
Pull Request — master (#4493)
by Owen
13:58
created

TorowTocol::torow()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 32
rs 7.3166
c 1
b 0
f 0
cc 11
nc 12
nop 3

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