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 ArraySupport 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 ArraySupport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | abstract class ArraySupport |
||
6 | { |
||
7 | /** |
||
8 | * Check if given array is empty. |
||
9 | * |
||
10 | * @param array $array |
||
11 | * @return bool |
||
12 | */ |
||
13 | 7 | public static function isEmpty($array) |
|
17 | |||
18 | /** |
||
19 | * Check if given key exists in array with dot notation support. |
||
20 | * |
||
21 | * @param array $array |
||
22 | * @param string $key |
||
23 | * @return bool |
||
24 | */ |
||
25 | 5 | public static function exists($array, $key) |
|
49 | |||
50 | /** |
||
51 | * Return the value stored under given key in the array with dot notation support. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param array $array |
||
55 | * @param mixed $default |
||
56 | * @return mixed |
||
57 | */ |
||
58 | 9 | public static function get($array, $key, $default = null) |
|
82 | |||
83 | /** |
||
84 | * Set the value for given key in the array with dot notation support. |
||
85 | * |
||
86 | * @param array &$array |
||
87 | * @param string $key |
||
88 | * @param mixed $value |
||
89 | * @return array |
||
90 | */ |
||
91 | 6 | View Code Duplication | public static function set(&$array, $key, $value) |
118 | |||
119 | /** |
||
120 | * Remove the value stored under given key from the array with dot notation support. |
||
121 | * |
||
122 | * @param array &$array |
||
123 | * @param string $key |
||
124 | * @return bool |
||
125 | */ |
||
126 | 4 | View Code Duplication | public static function remove(&$array, $key) |
153 | |||
154 | /** |
||
155 | * Flatten a multi-dimensional array into a single level using dot notation. |
||
156 | * |
||
157 | * @param array $array |
||
158 | * @return array |
||
159 | */ |
||
160 | 3 | public static function flatten($array) |
|
164 | |||
165 | /** |
||
166 | * Expand flattened array into a multi-dimensional one. |
||
167 | * |
||
168 | * @param $array |
||
169 | * @return array |
||
170 | */ |
||
171 | 4 | public static function expand($array) |
|
196 | |||
197 | /** |
||
198 | * Merge several arrays, preserving dot notation. |
||
199 | * |
||
200 | * @param array[] $arrays |
||
201 | * @return array |
||
202 | */ |
||
203 | 3 | public static function merge($arrays) |
|
214 | |||
215 | /** |
||
216 | * Merge several arrays. |
||
217 | * |
||
218 | * @param array[] $arrays |
||
219 | * @return array |
||
220 | */ |
||
221 | 3 | public static function replace($arrays) |
|
232 | |||
233 | /** |
||
234 | * Normalize key to dot notation valid format. |
||
235 | * |
||
236 | * @param string $key |
||
237 | * @return string |
||
238 | */ |
||
239 | 23 | public static function normalizeKey($key) |
|
247 | |||
248 | /** |
||
249 | * Flatten a single recursion of array. |
||
250 | * |
||
251 | * @param array $recursion |
||
252 | * @param string $prefix |
||
253 | * @return array |
||
254 | */ |
||
255 | 3 | protected static function flattenRecursive(&$recursion, $prefix) |
|
273 | } |
||
274 |
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.