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 |
||
9 | class Arr |
||
10 | { |
||
11 | use Macroable; |
||
12 | |||
13 | /** |
||
14 | * Determine whether the given value is array accessible. |
||
15 | * |
||
16 | * @param mixed $value |
||
17 | * @return bool |
||
18 | */ |
||
19 | 47 | public static function accessible($value) |
|
23 | |||
24 | /** |
||
25 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
26 | * |
||
27 | * @param array $array |
||
28 | * @param string $key |
||
29 | * @param mixed $value |
||
30 | * @return array |
||
31 | */ |
||
32 | 1 | public static function add($array, $key, $value) |
|
40 | |||
41 | /** |
||
42 | * Collapse an array of arrays into a single array. |
||
43 | * |
||
44 | * @param array $array |
||
45 | * @return array |
||
46 | */ |
||
47 | 5 | public static function collapse($array) |
|
63 | |||
64 | /** |
||
65 | * Cross join the given arrays, returning all possible permutations. |
||
66 | * |
||
67 | * @param array ...$arrays |
||
68 | * @return array |
||
69 | */ |
||
70 | 2 | public static function crossJoin(...$arrays) |
|
90 | |||
91 | /** |
||
92 | * Divide an array into two arrays. One with keys and the other with values. |
||
93 | * |
||
94 | * @param array $array |
||
95 | * @return array |
||
96 | */ |
||
97 | 1 | public static function divide($array) |
|
101 | |||
102 | /** |
||
103 | * Flatten a multi-dimensional associative array with dots. |
||
104 | * |
||
105 | * @param array $array |
||
106 | * @param string $prepend |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | public static function dot($array, $prepend = '') |
|
123 | |||
124 | /** |
||
125 | * Get all of the given array except for a specified array of keys. |
||
126 | * |
||
127 | * @param array $array |
||
128 | * @param array|string $keys |
||
129 | * @return array |
||
130 | */ |
||
131 | 3 | public static function except($array, $keys) |
|
137 | |||
138 | /** |
||
139 | * Determine if the given key exists in the provided array. |
||
140 | * |
||
141 | * @param \ArrayAccess|array $array |
||
142 | * @param string|int $key |
||
143 | * @return bool |
||
144 | */ |
||
145 | 45 | public static function exists($array, $key) |
|
153 | |||
154 | /** |
||
155 | * Return the first element in an array passing a given truth test. |
||
156 | * |
||
157 | * @param array $array |
||
158 | * @param callable|null $callback |
||
159 | * @param mixed $default |
||
160 | * @return mixed |
||
161 | */ |
||
162 | 15 | public static function first($array, callable $callback = null, $default = null) |
|
182 | |||
183 | /** |
||
184 | * Return the last element in an array passing a given truth test. |
||
185 | * |
||
186 | * @param array $array |
||
187 | * @param callable|null $callback |
||
188 | * @param mixed $default |
||
189 | * @return mixed |
||
190 | */ |
||
191 | 8 | public static function last($array, callable $callback = null, $default = null) |
|
199 | |||
200 | /** |
||
201 | * Flatten a multi-dimensional array into a single level. |
||
202 | * |
||
203 | * @param array $array |
||
204 | * @param int $depth |
||
205 | * @return array |
||
206 | */ |
||
207 | 5 | public static function flatten($array, $depth = INF) |
|
225 | |||
226 | /** |
||
227 | * Remove one or many array items from a given array using "dot" notation. |
||
228 | * |
||
229 | * @param array $array |
||
230 | * @param array|string $keys |
||
231 | * @return void |
||
232 | */ |
||
233 | 8 | public static function forget(&$array, $keys) |
|
269 | |||
270 | /** |
||
271 | * Get an item from an array using "dot" notation. |
||
272 | * |
||
273 | * @param \ArrayAccess|array $array |
||
274 | * @param string $key |
||
275 | * @param mixed $default |
||
276 | * @return mixed |
||
277 | */ |
||
278 | 6 | public static function get($array, $key, $default = null) |
|
306 | |||
307 | /** |
||
308 | * Check if an item or items exist in an array using "dot" notation. |
||
309 | * |
||
310 | * @param \ArrayAccess|array $array |
||
311 | * @param string|array $keys |
||
312 | * @return bool |
||
313 | */ |
||
314 | 1 | public static function has($array, $keys) |
|
348 | |||
349 | /** |
||
350 | * Determines if an array is associative. |
||
351 | * |
||
352 | * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
||
353 | * |
||
354 | * @param array $array |
||
355 | * @return bool |
||
356 | */ |
||
357 | 2 | public static function isAssoc(array $array) |
|
363 | |||
364 | /** |
||
365 | * Get a subset of the items from the given array. |
||
366 | * |
||
367 | * @param array $array |
||
368 | * @param array|string $keys |
||
369 | * @return array |
||
370 | */ |
||
371 | 2 | public static function only($array, $keys) |
|
375 | |||
376 | /** |
||
377 | * Pluck an array of values from an array. |
||
378 | * |
||
379 | * @param array $array |
||
380 | * @param string|array $value |
||
381 | * @param string|array|null $key |
||
382 | * @return array |
||
383 | */ |
||
384 | 13 | public static function pluck($array, $value, $key = null) |
|
411 | |||
412 | /** |
||
413 | * Explode the "value" and "key" arguments passed to "pluck". |
||
414 | * |
||
415 | * @param string|array $value |
||
416 | * @param string|array|null $key |
||
417 | * @return array |
||
418 | */ |
||
419 | 13 | protected static function explodePluckParameters($value, $key) |
|
427 | |||
428 | /** |
||
429 | * Push an item onto the beginning of an array. |
||
430 | * |
||
431 | * @param array $array |
||
432 | * @param mixed $value |
||
433 | * @param mixed $key |
||
434 | * @return array |
||
435 | */ |
||
436 | 2 | public static function prepend($array, $value, $key = null) |
|
446 | |||
447 | /** |
||
448 | * Get a value from the array, and remove it. |
||
449 | * |
||
450 | * @param array $array |
||
451 | * @param string $key |
||
452 | * @param mixed $default |
||
453 | * @return mixed |
||
454 | */ |
||
455 | 4 | public static function pull(&$array, $key, $default = null) |
|
463 | |||
464 | /** |
||
465 | * Get one or a specified number of random values from an array. |
||
466 | * |
||
467 | * @param array $array |
||
468 | * @param int|null $number |
||
469 | * @return mixed |
||
470 | * |
||
471 | * @throws \InvalidArgumentException |
||
472 | */ |
||
473 | 6 | public static function random($array, $number = null) |
|
503 | |||
504 | /** |
||
505 | * Set an array item to a given value using "dot" notation. |
||
506 | * |
||
507 | * If no key is given to the method, the entire array will be replaced. |
||
508 | * |
||
509 | * @param array $array |
||
510 | * @param string $key |
||
511 | * @param mixed $value |
||
512 | * @return array |
||
513 | */ |
||
514 | 3 | public static function set(&$array, $key, $value) |
|
539 | |||
540 | /** |
||
541 | * Shuffle the given array and return the result. |
||
542 | * |
||
543 | * @param array $array |
||
544 | * @return array |
||
545 | */ |
||
546 | 1 | public static function shuffle($array) |
|
552 | |||
553 | /** |
||
554 | * Sort the array using the given callback or "dot" notation. |
||
555 | * |
||
556 | * @param array $array |
||
557 | * @param callable|string|null $callback |
||
558 | * @return array |
||
559 | */ |
||
560 | 1 | public static function sort($array, $callback = null) |
|
564 | |||
565 | /** |
||
566 | * Recursively sort an array by keys and values. |
||
567 | * |
||
568 | * @param array $array |
||
569 | * @return array |
||
570 | */ |
||
571 | 1 | public static function sortRecursive($array) |
|
587 | |||
588 | /** |
||
589 | * Filter the array using the given callback. |
||
590 | * |
||
591 | * @param array $array |
||
592 | * @param callable $callback |
||
593 | * @return array |
||
594 | */ |
||
595 | 23 | public static function where($array, callable $callback) |
|
599 | |||
600 | /** |
||
601 | * If the given value is not an array and not null, wrap it in one. |
||
602 | * |
||
603 | * @param mixed $value |
||
604 | * @return array |
||
605 | */ |
||
606 | 6 | public static function wrap($value) |
|
614 | } |
||
615 |
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.