ArrayHelper::getValue()   B
last analyzed

Complexity

Conditions 10
Paths 19

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 18
c 0
b 0
f 0
nc 19
nop 3
dl 0
loc 25
ccs 20
cts 20
cp 1
crap 10
rs 7.6666

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
namespace Da\Mailer\Helper;
4
5
use Closure;
6
7
class ArrayHelper
8
{
9
    /**
10
     * Retrieves the value of an array element or object property with the given key or property name.
11
     * If the key does not exist in the array or object, the default value will be returned instead.
12
     *
13
     * The key may be specified in a dot format to retrieve the value of a sub-array or the property
14
     * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
15
     * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
16
     * or `$array->x` is neither an array nor an object, the default value will be returned.
17
     * Note that if the array already has an element `x.y.z`, then its value will be returned
18
     * instead of going through the sub-arrays. So it is better to be done specifying an array of key names
19
     * like `['x', 'y', 'z']`.
20
     *
21
     * Below are some usage examples,
22
     *
23
     * ~~~
24
     * // working with array
25
     * $username = ArrayHelper::getValue($_POST, 'username');
26
     * // working with object
27
     * $username = ArrayHelper::getValue($user, 'username');
28
     * // working with anonymous function
29
     * $fullName = ArrayHelper::getValue($user, function ($user, $defaultValue) {
30
     *     return $user->firstName . ' ' . $user->lastName;
31
     * });
32
     * // using dot format to retrieve the property of embedded object
33
     * $street = ArrayHelper::getValue($users, 'address.street');
34
     * // using an array of keys to retrieve the value
35
     * $value = ArrayHelper::getValue($versions, ['1.0', 'date']);
36
     * ~~~
37
     *
38
     * @param array|object $array array or object to extract value from
39
     * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object,
40
     * or an anonymous function returning the value. The anonymous function signature should be:
41
     * `function($array, $defaultValue)`.
42
     * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when
43
     * getting value from an object.
44
     *
45
     * @return mixed the value of the element if found, default value otherwise
46 36
     */
47
    public static function getValue($array, $key, $default = null)
48 36
    {
49 1
        if ($key instanceof Closure) {
50
            return $key($array, $default);
51 35
        }
52 2
        if (is_array($key)) {
53 2
            $lastKey = array_pop($key);
54 2
            foreach ($key as $keyPart) {
55 2
                $array = static::getValue($array, $keyPart);
56 2
            }
57 2
            $key = $lastKey;
58 35
        }
59 29
        if (is_array($array) && array_key_exists($key, $array)) {
60
            return $array[$key];
61 21
        }
62 8
        if (($pos = strrpos($key, '.')) !== false) {
63 8
            $array = static::getValue($array, substr($key, 0, $pos), $default);
64 8
            $key = substr($key, $pos + 1);
65 21
        }
66 1
        if (is_object($array)) {
67 20
            return $array->$key;
68 20
        } elseif (is_array($array)) {
69
            return array_key_exists($key, $array) ? $array[$key] : $default;
70 2
        } else {
71
            return $default;
72
        }
73
    }
74
75
    /**
76
     * Removes an item from an array and returns the value. If the key does not exist in the array, the default value
77
     * will be returned instead.
78
     *
79
     * Usage examples,
80
     *
81
     * ~~~
82
     * // $array = ['type' => 'A', 'options' => [1, 2]];
83
     * // working with array
84
     *  $type = ArrayHelper::remove($array, 'type');
85
     * // $array content
86
     * // $array = ['options' => [1, 2]];
87
     * ~~~
88
     *
89
     * @param array $array the array to extract value from
90
     * @param string $key key name of the array element
91
     * @param mixed $default the default value to be returned if the specified key does not exist
92
     *
93
     * @return mixed|null the value of the element if found, default value otherwise
94 3
     */
95
    public static function remove(&$array, $key, $default = null)
96 3
    {
97 3
        if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
98 3
            $value = $array[$key];
99
            unset($array[$key]);
100 3
            return $value;
101
        }
102
103 1
        return $default;
104
    }
105
}
106