Passed
Push — master ( 1d0ea2...0f9d68 )
by Kacper
05:34
created

ArrayHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 16
c 6
b 0
f 0
dl 0
loc 41
ccs 17
cts 19
cp 0.8947
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 11 3
A find() 0 9 3
A get() 0 3 2
A column() 0 3 1
A rearrange() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Highlighter
7
 *1
8
 * Copyright (C) 2016, Some right reserved.
9
 *
10
 * @author Kacper "Kadet" Donat <[email protected]>
11
 *
12
 * Contact with author:
13
 * Xmpp: [email protected]
14
 * E-mail: [email protected]
15
 *
16
 * From Kadet with love.
17
 */
18
19
namespace Kadet\Highlighter\Utils;
20
21
class ArrayHelper
22
{
23 1
    public static function rearrange(array $array, array $keys)
24
    {
25 1
        return array_combine($keys, array_map(function ($key) use ($array) {
26 1
            return $array[$key];
27 1
        }, $keys));
28
    }
29
30 1
    public static function column(array $array, $index)
31
    {
32 1
        return array_map(function ($e) use ($index) { return $e[$index]; }, $array);
33
    }
34
35 1
    public static function find(array $array, callable $tester)
36
    {
37 1
        foreach ($array as $key => $value) {
38 1
            if ($tester($key, $value)) {
39 1
                return $key;
40
            }
41
        }
42
43 1
        return false;
44
    }
45
46 2
    public static function resolve(array $array, $key, $fallback = null)
47
    {
48
        do {
49 2
            if (isset($array[$key])) {
50 2
                return $array[$key];
51
            }
52
53 2
            $key = StringHelper::pop($key);
54 2
        } while (!empty($key));
55
56 1
        return $fallback;
57
    }
58
59
    public static function get(array $array, $key, $fallback = null)
60
    {
61
        return isset($array[$key]) ? $array[$key] : $fallback;
62
    }
63
}
64