Arr   B
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 292
Duplicated Lines 14.04 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 70.15%

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 0
dl 41
loc 292
ccs 94
cts 134
cp 0.7015
rs 8.2769
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A divide() 0 4 1
A dot() 0 18 3
A except() 0 6 1
A first() 0 9 3
A last() 0 4 1
A flatten() 0 8 1
B forget() 9 29 6
B get() 9 18 6
B has() 9 18 7
A only() 0 4 1
A pull() 0 8 1
B set() 14 25 5
A where() 0 11 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
namespace Omnipay\Spreedly;
4
5
class Arr {
6
7
    /**
8
     * Add an element to an array using "dot" notation if it doesn't exist.
9
     *
10
     * @param  array   $array
11
     * @param  string  $key
12
     * @param  mixed   $value
13
     * @return array
14
     */
15 2
    public static function add($array, $key, $value)
16
    {
17 2
        if (is_null(static::get($array, $key)))
18 2
        {
19 2
            static::set($array, $key, $value);
20 2
        }
21
22 2
        return $array;
23
    }
24
25
    /**
26
     * Divide an array into two arrays. One with keys and the other with values.
27
     *
28
     * @param  array  $array
29
     * @return array
30
     */
31 2
    public static function divide($array)
32
    {
33 2
        return array(array_keys($array), array_values($array));
34
    }
35
36
    /**
37
     * Flatten a multi-dimensional associative array with dots.
38
     *
39
     * @param  array   $array
40
     * @param  string  $prepend
41
     * @return array
42
     */
43 2
    public static function dot($array, $prepend = '')
44
    {
45 2
        $results = array();
46
47 2
        foreach ($array as $key => $value)
48
        {
49 2
            if (is_array($value))
50 2
            {
51 2
                $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
52 2
            }
53
            else
54
            {
55 2
                $results[$prepend.$key] = $value;
56
            }
57 2
        }
58
59 2
        return $results;
60
    }
61
62
    /**
63
     * Get all of the given array except for a specified array of items.
64
     *
65
     * @param  array  $array
66
     * @param  array|string  $keys
67
     * @return array
68
     */
69 2
    public static function except($array, $keys)
70
    {
71 2
        static::forget($array, $keys);
72
73 2
        return $array;
74
    }
75
76
    /**
77
     * Return the first element in an array passing a given truth test.
78
     *
79
     * @param  array  $array
80
     * @param  callable  $callback
81
     * @param  mixed  $default
82
     * @return mixed
83
     */
84 4
    public static function first($array, callable $callback, $default = null)
85
    {
86 4
        foreach ($array as $key => $value)
87
        {
88 4
            if (call_user_func($callback, $key, $value)) return $value;
89 2
        }
90
91 2
        return $default;
92
    }
93
94
    /**
95
     * Return the last element in an array passing a given truth test.
96
     *
97
     * @param  array  $array
98
     * @param  callable  $callback
99
     * @param  mixed  $default
100
     * @return mixed
101
     */
102 2
    public static function last($array, callable $callback, $default = null)
103
    {
104 2
        return static::first(array_reverse($array), $callback, $default);
105
    }
106
107
    /**
108
     * Flatten a multi-dimensional array into a single level.
109
     *
110
     * @param  array  $array
111
     * @return array
112
     */
113 2
    public static function flatten($array)
114
    {
115 2
        $return = array();
116
117
        array_walk_recursive($array, function($x) use (&$return) { $return[] = $x; });
118
119 2
        return $return;
120
    }
121
122
    /**
123
     * Remove one or many array items from a given array using "dot" notation.
124
     *
125
     * @param  array  $array
126
     * @param  array|string  $keys
127
     * @return void
128
     */
129 6
    public static function forget(&$array, $keys)
130
    {
131 6
        $original = &$array;
132
133 6
        $keys = (array) $keys;
134
135 6
        if (count($keys) === 0) {
136 2
            return;
137
        }
138
139 6
        foreach ($keys as $key) {
140 6
            $parts = explode('.', $key);
141
142 6 View Code Duplication
            while (count($parts) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143 2
                $part = array_shift($parts);
144
145 2
                if (isset($array[$part]) && is_array($array[$part])) {
146 2
                    $array = &$array[$part];
147 2
                } else {
148 2
                    $parts = array();
149
                }
150 2
            }
151
152 6
            unset($array[array_shift($parts)]);
153
154
            // clean up after each pass
155 6
            $array = &$original;
156 6
        }
157 6
    }
158
159
    /**
160
     * Get an item from an array using "dot" notation.
161
     *
162
     * @param  array   $array
163
     * @param  string  $key
164
     * @param  mixed   $default
165
     * @return mixed
166
     */
167 56
    public static function get($array, $key, $default = null)
168
    {
169 56
        if (is_null($key)) return $array;
170
171 52
        if (isset($array[$key])) return $array[$key];
172
173 32 View Code Duplication
        foreach (explode('.', $key) as $segment)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
        {
175 32
            if ( ! is_array($array) || ! array_key_exists($segment, $array))
176 32
            {
177 28
                return $default;
178
            }
179
180 20
            $array = $array[$segment];
181 20
        }
182
183 20
        return $array;
184
    }
185
186
    /**
187
     * Check if an item exists in an array using "dot" notation.
188
     *
189
     * @param  array   $array
190
     * @param  string  $key
191
     * @return bool
192
     */
193 2
    public static function has($array, $key)
194
    {
195 2
        if (empty($array) || is_null($key)) return false;
196
197 2
        if (array_key_exists($key, $array)) return true;
198
199 2 View Code Duplication
        foreach (explode('.', $key) as $segment)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
        {
201 2
            if ( ! is_array($array) || ! array_key_exists($segment, $array))
202 2
            {
203 2
                return false;
204
            }
205
206 2
            $array = $array[$segment];
207 2
        }
208
209 2
        return true;
210
    }
211
212
    /**
213
     * Get a subset of the items from the given array.
214
     *
215
     * @param  array  $array
216
     * @param  array|string  $keys
217
     * @return array
218
     */
219 2
    public static function only($array, $keys)
220
    {
221 2
        return array_intersect_key($array, array_flip((array) $keys));
222
    }
223
224
    /**
225
     * Get a value from the array, and remove it.
226
     *
227
     * @param  array   $array
228
     * @param  string  $key
229
     * @param  mixed   $default
230
     * @return mixed
231
     */
232 2
    public static function pull(&$array, $key, $default = null)
233
    {
234 2
        $value = static::get($array, $key, $default);
235
236 2
        static::forget($array, $key);
237
238 2
        return $value;
239
    }
240
241
    /**
242
     * Set an array item to a given value using "dot" notation.
243
     *
244
     * If no key is given to the method, the entire array will be replaced.
245
     *
246
     * @param  array   $array
247
     * @param  string  $key
248
     * @param  mixed   $value
249
     * @return array
250
     */
251 6
    public static function set(&$array, $key, $value)
252
    {
253 6
        if (is_null($key)) return $array = $value;
254
255 6
        $keys = explode('.', $key);
256
257 6 View Code Duplication
        while (count($keys) > 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
        {
259 4
            $key = array_shift($keys);
260
261
            // If the key doesn't exist at this depth, we will just create an empty array
262
            // to hold the next value, allowing us to create the arrays to hold final
263
            // values at the correct depth. Then we'll keep digging into the array.
264 4
            if ( ! isset($array[$key]) || ! is_array($array[$key]))
265 4
            {
266 4
                $array[$key] = array();
267 4
            }
268
269 4
            $array =& $array[$key];
270 4
        }
271
272 6
        $array[array_shift($keys)] = $value;
273
274 6
        return $array;
275
    }
276
277
    /**
278
     * Filter the array using the given callback.
279
     *
280
     * @param  array  $array
281
     * @param  callable  $callback
282
     * @return array
283
     */
284 2
    public static function where($array, callable $callback)
285
    {
286 2
        $filtered = array();
287
288 2
        foreach ($array as $key => $value)
289
        {
290 2
            if (call_user_func($callback, $key, $value)) $filtered[$key] = $value;
291 2
        }
292
293 2
        return $filtered;
294
    }
295
296
}