ArrayHelper::getValue()   B
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 7.6666
cc 10
nc 14
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created for IG Client.
4
 * User: jakim <[email protected]>
5
 * Date: 14.03.2018
6
 */
7
8
namespace Jakim\Helper;
9
10
11
class ArrayHelper
12
{
13
    public static function getValue($array, $key, $default = null)
14
    {
15
        if ($key instanceof \Closure) {
16
            return $key($array, $default);
17
        }
18
19
        if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
20
            return $array[$key];
21
        }
22
23
        if (($pos = strrpos($key, '.')) !== false) {
24
            $array = static::getValue($array, substr($key, 0, $pos), $default);
25
            $key = substr($key, $pos + 1);
26
        }
27
28
        if (is_object($array)) {
29
            return $array->$key;
30
        } elseif (is_array($array)) {
31
            return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
32
        }
33
34
        return $default;
35
    }
36
37
    public static function getColumn($array, $name)
38
    {
39
        $result = [];
40
        foreach ($array as $k => $element) {
41
            $result[$k] = static::getValue($element, $name);
42
        }
43
44
        return $result;
45
    }
46
}