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 |
||
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) |
|
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) |
|
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 = '') |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
295 | |||
296 | } |
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.