Arr::mirror()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 44
    public static function map(array $array, callable $callback): array
13
    {
14 44
        $keys = array_keys($array);
15
16 44
        $items = array_map($callback, $array, $keys);
17
18 44
        return array_combine($keys, $items);
19
    }
20
21 13
    public static function flatMap(array $array, callable $callback): array
22
    {
23 13
        $mapped = self::map($array, $callback);
24
25 13
        $flattened = [];
26
27 13
        foreach ($mapped as $item) {
28 13
            if (is_array($item)) {
29 13
                $flattened = array_merge($flattened, $item);
30
            } else {
31 13
                $flattened[] = $item;
32
            }
33
        }
34
35 13
        return $flattened;
36
    }
37
38 41
    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 41
        $value = $array[$key] ?? $default;
41
42 41
        unset($array[$key]);
43
44 41
        return $value;
45
    }
46
47 41
    public static function mirror(array $array): array
48
    {
49 41
        return array_combine($array, $array);
50
    }
51
52 38
    public static function createUniquePairs(array $array): array
53
    {
54 38
        $pairs = [];
55
56 38
        while ($a = array_shift($array)) {
57 37
            foreach ($array as $b) {
58 21
                $pairs[] = [$a, $b];
59
            }
60
        }
61
62 38
        return $pairs;
63
    }
64
}
65