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 ArrayHelper 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 ArrayHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class ArrayHelper |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Recursive safe merge. |
||
| 23 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
| 24 | * |
||
| 25 | * Merges two or more arrays into one recursively. |
||
| 26 | * If each array has an element with the same string key value, the latter |
||
| 27 | * will overwrite the former (different from array_merge_recursive). |
||
| 28 | * Recursive merging will be conducted if both arrays have an element of array |
||
| 29 | * type and are having the same key. |
||
| 30 | * For integer-keyed elements, the elements from the latter array will |
||
| 31 | * be appended to the former array. |
||
| 32 | * |
||
| 33 | * @param array $a array to be merged to |
||
| 34 | * @param array $b array to be merged from |
||
| 35 | * @return array the merged array |
||
| 36 | */ |
||
| 37 | public static function merge($a, $b) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get specified items as array. |
||
| 65 | * @param mixed $keys specification |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | 2 | public static function getItems($array, $keys = null) |
|
| 83 | /** |
||
| 84 | * Inserts items in front of array. |
||
| 85 | * rough method: unset and then set, think of better. |
||
| 86 | */ |
||
| 87 | 2 | public static function insertLast(array $array, array $items) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Inserts items in front of array. |
||
| 101 | * rough method: unset and then set, think of better. |
||
| 102 | */ |
||
| 103 | 2 | public static function insertFirst(array $array, array $items) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Inserts items inside of array. |
||
| 115 | * rough method: unset and then set, think of better. |
||
| 116 | * |
||
| 117 | * @param array $array source array |
||
| 118 | * @param array $items array of items. |
||
| 119 | * @param string|array $where where to insert |
||
| 120 | * @return array new items list |
||
| 121 | * @see add() |
||
| 122 | */ |
||
| 123 | public static function insertInside(array $array, $items, $where) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Internal function to prepare where list for insertInside. |
||
| 159 | * |
||
| 160 | * @param array $array source array |
||
| 161 | * @param array|string $list array to convert |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected static function prepareWhere(array $array, $list) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Recursively removes duplicate values from non-associative arrays. |
||
| 180 | */ |
||
| 181 | public static function unique($array) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Non-recursively removes duplicate values from non-associative arrays. |
||
| 197 | */ |
||
| 198 | public static function uniqueFlat($array) |
||
| 213 | |||
| 214 | public static function toArray($object) |
||
| 225 | } |
||
| 226 |