Passed
Pull Request — master (#4492)
by Owen
13:35
created

Hstack   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 1
b 0
f 0
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B hstack() 0 32 8
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
8
class Hstack
9
{
10
    /**
11
     * Excel function HSTACK.
12
     *
13
     * @return mixed[]|string
14
     */
15
    public static function hstack(mixed ...$inputData): array|string
16
    {
17
        $maxRow = 0;
18
        foreach ($inputData as $matrix) {
19
            if (!is_array($matrix)) {
20
                $count = 1;
21
            } else {
22
                $count = count($matrix);
23
            }
24
            $maxRow = max($maxRow, $count);
25
        }
26
        /** @var mixed[] $inputData */
27
        foreach ($inputData as &$matrix) {
28
            if (!is_array($matrix)) {
29
                $matrix = [$matrix];
30
            }
31
            $rows = count($matrix);
32
            $reset = reset($matrix);
33
            $columns = is_array($reset) ? count($reset) : 1;
34
            while ($maxRow > $rows) {
35
                $matrix[] = array_pad([], $columns, ExcelError::NA());
36
                ++$rows;
37
            }
38
        }
39
40
        $transpose = array_map(null, ...$inputData); //* @phpstan-ignore-line
41
        $returnMatrix = [];
42
        foreach ($transpose as $array) {
43
            $returnMatrix[] = Functions::flattenArray($array);
44
        }
45
46
        return $returnMatrix;
47
    }
48
}
49