Passed
Branch fuck-54 (abba45)
by Kacper
02:46
created

ArrayHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 8
c 9
b 0
f 0
lcom 0
cbo 1
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10

4 Methods

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