formatted_array()   A
last analyzed

Complexity

Conditions 6
Paths 20

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 20
nop 4
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit;
4
5
use const AlecRabbit\Helpers\Constants\EMPTY_ELEMENTS;
6
7
/**
8
 * @param array $arr
9
 * @param int $columns
10
 * @param callable|null $preprocessor
11
 * @param int $pad
12
 * @return array
13
 */
14
function formatted_array(
15
    array $arr,
16
    int $columns = 10,
17
    ?callable $preprocessor = null,
18
    int $pad = STR_PAD_RIGHT
19
): array {
20 22
    $result = $tmp = [];
21 22
    if ($preprocessor) {
22 3
        \array_walk($arr, $preprocessor);
23
    }
24 22
    $maxLength = arr_el_max_length($arr);
25 21
    $rowEmpty = true;
26 21
    foreach ($arr as $element) {
27 20
        $tmp[] = \str_pad((string)$element, $maxLength, ' ', $pad);
28 20
        $rowEmpty = $rowEmpty && \in_array($element, EMPTY_ELEMENTS, true);
29 20
        if (\count($tmp) >= $columns) {
30 14
            update_result($result, $rowEmpty, $tmp);
31
        }
32
    }
33 21
    if (!empty($tmp)) {
34 12
        update_result($result, $rowEmpty, $tmp);
35
    }
36 21
    return $result;
37
}
38
39
40
/**
41
 * @param array $data
42
 * @internal
43
 * @return int
44
 */
45
function arr_el_max_length(array &$data): int
46
{
47 33
    $maxLength = 0;
48 33
    foreach ($data as &$element) {
49 32
        if (\is_array($element)) {
50 1
            throw new \RuntimeException('Array to string conversion');
51
        }
52 31
        $len = \strlen($element = (string)$element);
53 31
        if ($maxLength < $len) {
54 23
            $maxLength = $len;
55
        }
56
    }
57 32
    return $maxLength;
58
}
59
60
/**
61
 * @param array $result
62
 * @param bool $rowEmpty
63
 * @param array $tmp
64
 * @internal
65
 */
66
function update_result(array &$result, bool &$rowEmpty, array &$tmp): void
67
{
68 24
    $result[] = \implode($rowEmpty ? '' : ' ', $tmp);
69 24
    $rowEmpty = true;
70 24
    $tmp = [];
71 24
}
72
73
/**
74
 * @param array $data
75
 * @return array
76
 */
77
function array_unset_first(array $data): array
78
{
79 12
    $key = \array_key_first($data);
80 12
    if (null !== $key) {
81 11
        unset($data[$key]);
82
    }
83 12
    return $data;
84
}
85
86
/**
87
 * @param array $data
88
 * @return array
89
 */
90
function array_unset_last(array $data): array
91
{
92 12
    $key = \array_key_last($data);
93 12
    if (null !== $key) {
94 11
        unset($data[$key]);
95
    }
96 12
    return $data;
97
}
98
99
/**
100
 * Known issue `array_is_homogeneous` can't handle cyclic references in arrays
101
 *      $a = [];
102
 *      $a[1] = &$a;
103
 *      $b = [];
104
 *      $b[1] = &$b;
105
 *      var_dump($b === $a); // 'PHP Fatal error:  Nesting level too deep - recursive dependency?'
106
 *
107
 * @param array $a
108
 * @return bool
109
 */
110
function array_is_homogeneous(array $a): bool
111
{
112 6
    $firstElement = current($a);
113 6
    foreach ($a as $element) {
114 6
        if ($firstElement !== $element) {
115 2
            return false;
116
        }
117
    }
118 4
    return true;
119
}
120