Arr   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 355
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 0
dl 0
loc 355
rs 8.3673
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 2
A build() 0 11 2
A divide() 0 7 1
A dot() 0 14 3
A except() 0 4 1
A fetch() 0 15 3
A first() 0 10 3
A last() 0 4 1
A flatten() 0 12 1
B forget() 0 17 5
B get() 0 19 6
A only() 0 4 1
B pluck() 0 19 5
A pull() 0 7 1
B set() 0 22 5
A sort() 0 10 2
A where() 0 12 3

How to fix   Complexity   

Complex Class

Complex classes like Arr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Arr, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the light/easemob.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\Easemob\Support;
13
14
use Closure;
15
16
/**
17
 * Array helper from Illuminate\Support\Arr.
18
 */
19
class Arr
20
{
21
    /**
22
     * Add an element to an array using "dot" notation if it doesn't exist.
23
     *
24
     * @param array  $array
25
     * @param string $key
26
     * @param mixed  $value
27
     *
28
     * @return array
29
     */
30
    public static function add($array, $key, $value)
31
    {
32
        if (is_null(static::get($array, $key))) {
33
            static::set($array, $key, $value);
34
        }
35
36
        return $array;
37
    }
38
39
    /**
40
     * Build a new array using a callback.
41
     *
42
     * @param array    $array
43
     * @param \Closure $callback
44
     *
45
     * @return array
46
     */
47
    public static function build($array, Closure $callback)
48
    {
49
        $results = [];
50
51
        foreach ($array as $key => $value) {
52
            list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
53
            $results[$innerKey] = $innerValue;
54
        }
55
56
        return $results;
57
    }
58
59
    /**
60
     * Divide an array into two arrays. One with keys and the other with values.
61
     *
62
     * @param array $array
63
     *
64
     * @return array
65
     */
66
    public static function divide($array)
67
    {
68
        return [
69
            array_keys($array),
70
            array_values($array),
71
        ];
72
    }
73
74
    /**
75
     * Flatten a multi-dimensional associative array with dots.
76
     *
77
     * @param array  $array
78
     * @param string $prepend
79
     *
80
     * @return array
81
     */
82
    public static function dot($array, $prepend = '')
83
    {
84
        $results = [];
85
86
        foreach ($array as $key => $value) {
87
            if (is_array($value)) {
88
                $results = array_merge($results, static::dot($value, $prepend . $key . '.'));
89
            } else {
90
                $results[$prepend . $key] = $value;
91
            }
92
        }
93
94
        return $results;
95
    }
96
97
    /**
98
     * Get all of the given array except for a specified array of items.
99
     *
100
     * @param array        $array
101
     * @param array|string $keys
102
     *
103
     * @return array
104
     */
105
    public static function except($array, $keys)
106
    {
107
        return array_diff_key($array, array_flip((array) $keys));
108
    }
109
110
    /**
111
     * Fetch a flattened array of a nested array element.
112
     *
113
     * @param array  $array
114
     * @param string $key
115
     *
116
     * @return array
117
     */
118
    public static function fetch($array, $key)
119
    {
120
        $results = [];
121
122
        foreach (explode('.', $key) as $segment) {
123
            $results = [];
124
            foreach ($array as $value) {
125
                $value = (array) $value;
126
                $results[] = $value[$segment];
127
            }
128
            $array = array_values($results);
129
        }
130
131
        return array_values($results);
132
    }
133
134
    /**
135
     * Return the first element in an array passing a given truth test.
136
     *
137
     * @param array    $array
138
     * @param \Closure $callback
139
     * @param mixed    $default
140
     *
141
     * @return mixed
142
     */
143
    public static function first($array, $callback, $default = null)
144
    {
145
        foreach ($array as $key => $value) {
146
            if (call_user_func($callback, $key, $value)) {
147
                return $value;
148
            }
149
        }
150
151
        return $default;
152
    }
153
154
    /**
155
     * Return the last element in an array passing a given truth test.
156
     *
157
     * @param array    $array
158
     * @param \Closure $callback
159
     * @param mixed    $default
160
     *
161
     * @return mixed
162
     */
163
    public static function last($array, $callback, $default = null)
164
    {
165
        return static::first(array_reverse($array), $callback, $default);
166
    }
167
168
    /**
169
     * Flatten a multi-dimensional array into a single level.
170
     *
171
     * @param array $array
172
     *
173
     * @return array
174
     */
175
    public static function flatten($array)
176
    {
177
        $return = [];
178
        array_walk_recursive(
179
            $array,
180
            function ($x) use (&$return) {
181
                $return[] = $x;
182
            }
183
        );
184
185
        return $return;
186
    }
187
188
    /**
189
     * Remove one or many array items from a given array using "dot" notation.
190
     *
191
     * @param array        $array
192
     * @param array|string $keys
193
     */
194
    public static function forget(&$array, $keys)
195
    {
196
        $original = &$array;
197
198
        foreach ((array) $keys as $key) {
199
            $parts = explode('.', $key);
200
            while (count($parts) > 1) {
201
                $part = array_shift($parts);
202
                if (isset($array[$part]) && is_array($array[$part])) {
203
                    $array = &$array[$part];
204
                }
205
            }
206
            unset($array[array_shift($parts)]);
207
            // clean up after each pass
208
            $array = &$original;
209
        }
210
    }
211
212
    /**
213
     * Get an item from an array using "dot" notation.
214
     *
215
     * @param array  $array
216
     * @param string $key
217
     * @param mixed  $default
218
     *
219
     * @return mixed
220
     */
221
    public static function get($array, $key, $default = null)
222
    {
223
        if (is_null($key)) {
224
            return $array;
225
        }
226
227
        if (isset($array[$key])) {
228
            return $array[$key];
229
        }
230
231
        foreach (explode('.', $key) as $segment) {
232
            if (!is_array($array) || !array_key_exists($segment, $array)) {
233
                return $default;
234
            }
235
            $array = $array[$segment];
236
        }
237
238
        return $array;
239
    }
240
241
    /**
242
     * Get a subset of the items from the given array.
243
     *
244
     * @param array        $array
245
     * @param array|string $keys
246
     *
247
     * @return array
248
     */
249
    public static function only($array, $keys)
250
    {
251
        return array_intersect_key($array, array_flip((array) $keys));
252
    }
253
254
    /**
255
     * Pluck an array of values from an array.
256
     *
257
     * @param array  $array
258
     * @param string $value
259
     * @param string $key
260
     *
261
     * @return array
262
     */
263
    public static function pluck($array, $value, $key = null)
264
    {
265
        $results = [];
266
267
        foreach ($array as $item) {
268
            $itemValue = is_object($item) ? $item->{$value} : $item[$value];
269
            // If the key is "null", we will just append the value to the array and keep
270
            // looping. Otherwise we will key the array using the value of the key we
271
            // received from the developer. Then we'll return the final array form.
272
            if (is_null($key)) {
273
                $results[] = $itemValue;
274
            } else {
275
                $itemKey = is_object($item) ? $item->{$key} : $item[$key];
276
                $results[$itemKey] = $itemValue;
277
            }
278
        }
279
280
        return $results;
281
    }
282
283
    /**
284
     * Get a value from the array, and remove it.
285
     *
286
     * @param array  $array
287
     * @param string $key
288
     * @param mixed  $default
289
     *
290
     * @return mixed
291
     */
292
    public static function pull(&$array, $key, $default = null)
293
    {
294
        $value = static::get($array, $key, $default);
295
        static::forget($array, $key);
296
297
        return $value;
298
    }
299
300
    /**
301
     * Set an array item to a given value using "dot" notation.
302
     *
303
     * If no key is given to the method, the entire array will be replaced.
304
     *
305
     * @param array  $array
306
     * @param string $key
307
     * @param mixed  $value
308
     *
309
     * @return array
310
     */
311
    public static function set(&$array, $key, $value)
312
    {
313
        if (is_null($key)) {
314
            return $array = $value;
315
        }
316
317
        $keys = explode('.', $key);
318
319
        while (count($keys) > 1) {
320
            $key = array_shift($keys);
321
            // If the key doesn't exist at this depth, we will just create an empty array
322
            // to hold the next value, allowing us to create the arrays to hold final
323
            // values at the correct depth. Then we'll keep digging into the array.
324
            if (!isset($array[$key]) || !is_array($array[$key])) {
325
                $array[$key] = [];
326
            }
327
            $array = &$array[$key];
328
        }
329
        $array[array_shift($keys)] = $value;
330
331
        return $array;
332
    }
333
334
    /**
335
     * Sort the array using the given Closure.
336
     *
337
     * @param array    $array
338
     * @param \Closure $callback
339
     *
340
     * @return array
341
     */
342
    public static function sort($array, Closure $callback)
343
    {
344
        $results = [];
345
346
        foreach ($array as $key => $value) {
347
            $results[$key] = $callback($value);
348
        }
349
350
        return $results;
351
    }
352
353
    /**
354
     * Filter the array using the given Closure.
355
     *
356
     * @param array    $array
357
     * @param \Closure $callback
358
     *
359
     * @return array
360
     */
361
    public static function where($array, Closure $callback)
362
    {
363
        $filtered = [];
364
365
        foreach ($array as $key => $value) {
366
            if (call_user_func($callback, $key, $value)) {
367
                $filtered[$key] = $value;
368
            }
369
        }
370
371
        return $filtered;
372
    }
373
}
374