data_get()   C
last analyzed

Complexity

Conditions 14
Paths 29

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 23
c 1
b 0
f 0
nc 29
nop 3
dl 0
loc 41
rs 6.2666

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 MuCTS\Collections\Arr;
4
use MuCTS\Collections\Collection;
5
6
if (!function_exists('collect')) {
7
    /**
8
     * Create a collection from the given value.
9
     *
10
     * @param mixed $value
11
     * @return Collection
12
     */
13
    function collect($value = null)
14
    {
15
        return new Collection($value);
16
    }
17
}
18
19
if (!function_exists('data_fill')) {
20
    /**
21
     * Fill in data where it's missing.
22
     *
23
     * @param mixed $target
24
     * @param string|array $key
25
     * @param mixed $value
26
     * @return mixed
27
     */
28
    function data_fill(&$target, $key, $value)
29
    {
30
        return data_set($target, $key, $value, false);
31
    }
32
}
33
34
if (!function_exists('data_get')) {
35
    /**
36
     * Get an item from an array or object using "dot" notation.
37
     *
38
     * @param mixed $target
39
     * @param string|array|int|null $key
40
     * @param mixed $default
41
     * @return mixed
42
     */
43
    function data_get($target, $key, $default = null)
44
    {
45
        if (is_null($key)) {
46
            return $target;
47
        }
48
49
        $key = is_array($key) ? $key : explode('.', $key);
50
51
        foreach ($key as $i => $segment) {
52
            unset($key[$i]);
53
54
            if (is_null($segment)) {
55
                return $target;
56
            }
57
58
            if ($segment === '*') {
59
                if ($target instanceof Collection) {
60
                    $target = $target->all();
61
                } elseif (!is_array($target)) {
62
                    return value($default);
63
                }
64
65
                $result = [];
66
67
                foreach ($target as $item) {
68
                    $result[] = data_get($item, $key);
69
                }
70
71
                return in_array('*', $key) ? Arr::collapse($result) : $result;
72
            }
73
74
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
75
                $target = $target[$segment];
76
            } elseif (is_object($target) && isset($target->{$segment})) {
77
                $target = $target->{$segment};
78
            } else {
79
                return value($default);
80
            }
81
        }
82
83
        return $target;
84
    }
85
}
86
87
if (!function_exists('data_set')) {
88
    /**
89
     * Set an item on an array or object using dot notation.
90
     *
91
     * @param mixed $target
92
     * @param string|array $key
93
     * @param mixed $value
94
     * @param bool $overwrite
95
     * @return mixed
96
     */
97
    function data_set(&$target, $key, $value, $overwrite = true)
98
    {
99
        $segments = is_array($key) ? $key : explode('.', $key);
100
101
        if (($segment = array_shift($segments)) === '*') {
102
            if (!Arr::accessible($target)) {
103
                $target = [];
104
            }
105
106
            if ($segments) {
107
                foreach ($target as &$inner) {
108
                    data_set($inner, $segments, $value, $overwrite);
109
                }
110
            } elseif ($overwrite) {
111
                foreach ($target as &$inner) {
112
                    $inner = $value;
113
                }
114
            }
115
        } elseif (Arr::accessible($target)) {
116
            if ($segments) {
117
                if (!Arr::exists($target, $segment)) {
118
                    $target[$segment] = [];
119
                }
120
121
                data_set($target[$segment], $segments, $value, $overwrite);
122
            } elseif ($overwrite || !Arr::exists($target, $segment)) {
123
                $target[$segment] = $value;
124
            }
125
        } elseif (is_object($target)) {
126
            if ($segments) {
127
                if (!isset($target->{$segment})) {
128
                    $target->{$segment} = [];
129
                }
130
131
                data_set($target->{$segment}, $segments, $value, $overwrite);
132
            } elseif ($overwrite || !isset($target->{$segment})) {
133
                $target->{$segment} = $value;
134
            }
135
        } else {
136
            $target = [];
137
138
            if ($segments) {
139
                data_set($target[$segment], $segments, $value, $overwrite);
140
            } elseif ($overwrite) {
141
                $target[$segment] = $value;
142
            }
143
        }
144
145
        return $target;
146
    }
147
}
148
149
if (!function_exists('head')) {
150
    /**
151
     * Get the first element of an array. Useful for method chaining.
152
     *
153
     * @param array $array
154
     * @return mixed
155
     */
156
    function head($array)
157
    {
158
        return reset($array);
159
    }
160
}
161
162
if (!function_exists('last')) {
163
    /**
164
     * Get the last element from an array.
165
     *
166
     * @param array $array
167
     * @return mixed
168
     */
169
    function last($array)
170
    {
171
        return end($array);
172
    }
173
}
174
175
if (!function_exists('value')) {
176
    /**
177
     * Return the default value of the given value.
178
     *
179
     * @param mixed $value
180
     * @return mixed
181
     */
182
    function value($value)
183
    {
184
        return $value instanceof Closure ? $value() : $value;
185
    }
186
}
187