Passed
Push — master ( a62b41...518ca5 )
by Oliver
03:36
created

Arr::flatMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Webfactor\Laravel\OpeningHours\Helpers;
4
5
class Arr
6
{
7 3
    public static function filter(array $array, callable $callback): array
8
    {
9 3
        return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
10
    }
11
12 32
    public static function map(array $array, callable $callback): array
13
    {
14 32
        $keys = array_keys($array);
15
16 32
        $items = array_map($callback, $array, $keys);
17
18 32
        return array_combine($keys, $items);
19
    }
20
21 2
    public static function flatMap(array $array, callable $callback): array
22
    {
23 2
        $mapped = self::map($array, $callback);
24
25 2
        $flattened = [];
26
27 2
        foreach ($mapped as $item) {
28 2
            if (is_array($item)) {
29 2
                $flattened = array_merge($flattened, $item);
30
            } else {
31 2
                $flattened[] = $item;
32
            }
33
        }
34
35 2
        return $flattened;
36
    }
37
38 29
    public static function pull(&$array, $key, $default = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
39
    {
40 29
        $value = $array[$key] ?? $default;
41
42 29
        unset($array[$key]);
43
44 29
        return $value;
45
    }
46
47 29
    public static function mirror(array $array): array
48
    {
49 29
        return array_combine($array, $array);
50
    }
51
52 26
    public static function createUniquePairs(array $array): array
53
    {
54 26
        $pairs = [];
55
56 26
        while ($a = array_shift($array)) {
57 25
            foreach ($array as $b) {
58 9
                $pairs[] = [$a, $b];
59
            }
60
        }
61
62 26
        return $pairs;
63
    }
64
}
65