Passed
Push — develop ( 02d75f...d9e5d3 )
by Alec
02:17
created

update_result()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
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 $data
9
 * @param int $columns
10
 * @param callable|null $callback
11
 * @param int $pad
12
 * @return array
13
 */
14
function formatted_array(
15
    array $data,
16
    int $columns = 10,
17
    ?callable $callback = null,
18
    int $pad = STR_PAD_RIGHT
19
): array {
20 22
    $result = $tmp = [];
21 22
    if ($callback) {
22 3
        \array_walk($data, $callback);
23
    }
24 22
    $maxLength = arr_el_max_length($data);
25 21
    $rowEmpty = true;
26 21
    foreach ($data 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 20
            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 31
    $maxLength = 0;
48 31
    foreach ($data as &$element) {
49 30
        if (\is_array($element)) {
50 1
            throw new \RuntimeException('Array to string conversion');
51
        }
52 29
        $len = \strlen($element = (string)$element);
53 29
        if ($maxLength < $len) {
54 29
            $maxLength = $len;
55
        }
56
    }
57 30
    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 20
    $result[] = \implode($rowEmpty ? '' : ' ', $tmp);
69 20
    $rowEmpty = true;
70 20
    $tmp = [];
71 20
}
72
73
74
///******
75
76
77
//
78
//
79
//function formatted_array_3(
80
//}
81
82
///******
83
84
85
/**
86
 * @param array $data
87
 * @return array
88
 */
89
function unset_first(array $data): array
90
{
91
    // TODO for PHP >=7.3 this line should use \array_key_first
92 11
    $key = array_key_first($data);
93 11
    if (null !== $key) {
94 10
        unset($data[$key]);
95
    }
96 11
    return $data;
97
}
98
99
/**
100
 * @param array $data
101
 * @return int|null|string
102
 */
103
function array_key_first(array $data)
104
{
105 14
    foreach ($data as $key => $value) {
106
        // this loop does not loop
107 12
        return $key;
108
    }
109 2
}
110
111
/**
112
 * @param array $data
113
 * @return int|null|string
114
 */
115
function array_key_last(array $data)
116
{
117 3
    end($data);
118 3
    return key($data);
119
}
120
121
/**
122
 * @param array $arr
123
 * @return bool
124
 */
125
function is_homogeneous(array $arr): bool
126
{
127 6
    $firstValue = current($arr);
128 6
    foreach ($arr as $val) {
129 6
        if ($firstValue !== $val) {
130 6
            return false;
131
        }
132
    }
133 4
    return true;
134
}
135