data_get()   C
last analyzed

Complexity

Conditions 16
Paths 32

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 124

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 25
c 3
b 0
f 2
dl 0
loc 38
ccs 2
cts 8
cp 0.25
rs 5.5666
cc 16
nc 32
nop 3
crap 124

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
use Nip\Utility\Arr;
4
5
if (!function_exists('env')) {
6
    /**
7
     * Gets the value of an environment variable.
8
     *
9
     * @param  string $key
10
     * @param  mixed $default
11
     * @return mixed
12
     */
13
    function env($key, $default = null)
14
    {
15
        return \Nip\Utility\Env::get($key, $default);
16
    }
17
}
18
19
if (! function_exists('data_get')) {
20
    /**
21
     * Get an item from an array or object using "dot" notation.
22
     *
23
     * @param  mixed   $target
24
     * @param  string|array|int|callable  $key
25
     * @param  mixed   $default
26
     * @return mixed
27
     */
28
    function data_get($target, $key, $default = null)
29
    {
30
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
31
            return $target;
32
        }
33
34
        if (is_callable($key)) {
35
            return call_user_func($key, $target);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type integer; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            return call_user_func(/** @scrutinizer ignore-type */ $key, $target);
Loading history...
36
        }
37
38
        $key = is_array($key) ? $key : explode('.', $key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type callable; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $key = is_array($key) ? $key : explode('.', /** @scrutinizer ignore-type */ $key);
Loading history...
39
40
        while (! is_null($segment = array_shift($key))) {
41
            if ($segment === '*') {
42
                if (is_object($target) && method_exists($target, 'all')) {
43
                    $target = $target->all();
44
                } elseif (! is_array($target)) {
45
                    return value($default);
46
                }
47
                $result = [];
48
                foreach ($target as $item) {
49 1
                    $result[] = data_get($item, $key);
50
                }
51
                return in_array('*', $key) ? Arr::collapse($result) : $result;
52
            }
53
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
54
                $target = $target[$segment];
55
            } elseif (is_object($target)) {
56
                if (isset($target->{$segment})) {
57
                    $target = $target->{$segment};
58
                } elseif (method_exists($target, $segment)) {
59
                    $target = $target->{$segment}();
60
                }
61
            } else {
62
                return value($default);
63
            }
64 1
        }
65
        return $target;
66
    }
67 1
}
68
if (! function_exists('head')) {
69 1
    /**
70 1
     * Get the first element of an array. Useful for method chaining.
71
     *
72
     * @param  array  $array
73
     * @return mixed
74
     */
75
    function head($array)
76
    {
77
        return reset($array);
78
    }
79
}
80
81
if (! function_exists('last')) {
82 1
    /**
83 1
     * Get the last element from an array.
84 1
     *
85
     * @param  array  $array
86
     * @return mixed
87 1
     */
88
    function last($array)
89
    {
90 1
        return end($array);
91
    }
92
}
93
94
if (!function_exists('value')) {
95
    /**
96
     * Return the default value of the given value.
97
     *
98
     * @param mixed $value
99
     * @return mixed
100
     */
101
    function value($value)
102
    {
103
        return $value instanceof Closure ? $value() : $value;
104
    }
105
}