Passed
Push — master ( e915bc...3ac872 )
by Alec
03:03
created

array_unset_first()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
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 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 31
            $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 11
    $key = \array_key_first($data);
80 11
    if (null !== $key) {
81 10
        unset($data[$key]);
82
    }
83 11
    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
 * @param array $arr
101
 * @return bool
102
 */
103
function is_homogeneous(array $arr): bool
104
{
105 6
    $firstValue = current($arr);
106 6
    foreach ($arr as $val) {
107 6
        if ($firstValue !== $val) {
108 6
            return false;
109
        }
110
    }
111 4
    return true;
112
}
113