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 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 |
||
29 | class Arr |
||
30 | { |
||
31 | /** |
||
32 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
33 | * |
||
34 | * @param array $array |
||
35 | * @param string $key |
||
36 | * @param mixed $value |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public static function add($array, $key, $value) |
||
41 | { |
||
42 | if (is_null(static::get($array, $key))) { |
||
43 | static::set($array, $key, $value); |
||
44 | } |
||
45 | |||
46 | return $array; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Build a new array using a callback. |
||
51 | * |
||
52 | * @param array $array |
||
53 | * @param \Closure $callback |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | View Code Duplication | public static function build($array, Closure $callback) |
|
58 | { |
||
59 | $results = []; |
||
60 | |||
61 | foreach ($array as $key => $value) { |
||
62 | list($innerKey, $innerValue) = call_user_func($callback, $key, $value); |
||
63 | $results[$innerKey] = $innerValue; |
||
64 | } |
||
65 | |||
66 | return $results; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Divide an array into two arrays. One with keys and the other with values. |
||
71 | * |
||
72 | * @param array $array |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public static function divide($array) |
||
77 | { |
||
78 | return [ |
||
79 | array_keys($array), |
||
80 | array_values($array), |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Flatten a multi-dimensional associative array with dots. |
||
86 | * |
||
87 | * @param array $array |
||
88 | * @param string $prepend |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public static function dot($array, $prepend = '') |
||
93 | { |
||
94 | $results = []; |
||
95 | |||
96 | foreach ($array as $key => $value) { |
||
97 | if (is_array($value)) { |
||
98 | $results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
||
99 | } else { |
||
100 | $results[$prepend.$key] = $value; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $results; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get all of the given array except for a specified array of items. |
||
109 | * |
||
110 | * @param array $array |
||
111 | * @param array|string $keys |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | public static function except($array, $keys) |
||
119 | |||
120 | /** |
||
121 | * Fetch a flattened array of a nested array element. |
||
122 | * |
||
123 | * @param array $array |
||
124 | * @param string $key |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public static function fetch($array, $key) |
||
129 | { |
||
130 | $results = []; |
||
131 | |||
132 | foreach (explode('.', $key) as $segment) { |
||
133 | $results = []; |
||
134 | foreach ($array as $value) { |
||
135 | $value = (array) $value; |
||
136 | $results[] = $value[$segment]; |
||
137 | } |
||
138 | $array = array_values($results); |
||
139 | } |
||
140 | |||
141 | return array_values($results); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the first element in an array passing a given truth test. |
||
146 | * |
||
147 | * @param array $array |
||
148 | * @param \Closure $callback |
||
149 | * @param mixed $default |
||
150 | * |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public static function first($array, $callback, $default = null) |
||
154 | { |
||
155 | foreach ($array as $key => $value) { |
||
156 | if (call_user_func($callback, $key, $value)) { |
||
157 | return $value; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $default; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Return the last element in an array passing a given truth test. |
||
166 | * |
||
167 | * @param array $array |
||
168 | * @param \Closure $callback |
||
169 | * @param mixed $default |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | public static function last($array, $callback, $default = null) |
||
177 | |||
178 | /** |
||
179 | * Flatten a multi-dimensional array into a single level. |
||
180 | * |
||
181 | * @param array $array |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public static function flatten($array) |
||
186 | { |
||
187 | $return = []; |
||
188 | array_walk_recursive( |
||
189 | $array, |
||
190 | function ($x) use (&$return) { |
||
191 | $return[] = $x; |
||
192 | } |
||
193 | ); |
||
194 | |||
195 | return $return; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Remove one or many array items from a given array using "dot" notation. |
||
200 | * |
||
201 | * @param array $array |
||
202 | * @param array|string $keys |
||
203 | */ |
||
204 | public static function forget(&$array, $keys) |
||
205 | { |
||
206 | $original = &$array; |
||
207 | |||
208 | foreach ((array) $keys as $key) { |
||
209 | $parts = explode('.', $key); |
||
210 | while (count($parts) > 1) { |
||
211 | $part = array_shift($parts); |
||
212 | if (isset($array[$part]) && is_array($array[$part])) { |
||
213 | $array = &$array[$part]; |
||
214 | } |
||
215 | } |
||
216 | unset($array[array_shift($parts)]); |
||
217 | // clean up after each pass |
||
218 | $array = &$original; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Get an item from an array using "dot" notation. |
||
224 | * |
||
225 | * @param array $array |
||
226 | * @param string $key |
||
227 | * @param mixed $default |
||
228 | * |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public static function get($array, $key, $default = null) |
||
232 | { |
||
233 | if (is_null($key)) { |
||
234 | return $array; |
||
235 | } |
||
236 | |||
237 | if (isset($array[$key])) { |
||
238 | return $array[$key]; |
||
239 | } |
||
240 | |||
241 | foreach (explode('.', $key) as $segment) { |
||
242 | if (!is_array($array) || !array_key_exists($segment, $array)) { |
||
243 | return $default; |
||
244 | } |
||
245 | $array = $array[$segment]; |
||
246 | } |
||
247 | |||
248 | return $array; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get a subset of the items from the given array. |
||
253 | * |
||
254 | * @param array $array |
||
255 | * @param array|string $keys |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | public static function only($array, $keys) |
||
263 | |||
264 | /** |
||
265 | * Pluck an array of values from an array. |
||
266 | * |
||
267 | * @param array $array |
||
268 | * @param string $value |
||
269 | * @param string $key |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | public static function pluck($array, $value, $key = null) |
||
292 | |||
293 | /** |
||
294 | * Get a value from the array, and remove it. |
||
295 | * |
||
296 | * @param array $array |
||
297 | * @param string $key |
||
298 | * @param mixed $default |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public static function pull(&$array, $key, $default = null) |
||
309 | |||
310 | /** |
||
311 | * Set an array item to a given value using "dot" notation. |
||
312 | * |
||
313 | * If no key is given to the method, the entire array will be replaced. |
||
314 | * |
||
315 | * @param array $array |
||
316 | * @param string $key |
||
317 | * @param mixed $value |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public static function set(&$array, $key, $value) |
||
343 | |||
344 | /** |
||
345 | * Sort the array using the given Closure. |
||
346 | * |
||
347 | * @param array $array |
||
348 | * @param \Closure $callback |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public static function sort($array, Closure $callback) |
||
362 | |||
363 | /** |
||
364 | * Filter the array using the given Closure. |
||
365 | * |
||
366 | * @param array $array |
||
367 | * @param \Closure $callback |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | View Code Duplication | public static function where($array, Closure $callback) |
|
383 | } |
||
384 |