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 |
||
18 | class ArrayHelper |
||
19 | { |
||
20 | /** |
||
21 | * Recursive safe merge. |
||
22 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
23 | * |
||
24 | * Merges two or more arrays into one recursively. |
||
25 | * If each array has an element with the same string key value, the latter |
||
26 | * will overwrite the former (different from array_merge_recursive). |
||
27 | * Recursive merging will be conducted if both arrays have an element of array |
||
28 | * type and are having the same key. |
||
29 | * For integer-keyed elements, the elements from the latter array will |
||
30 | * be appended to the former array. |
||
31 | * |
||
32 | * @param array $a array to be merged to |
||
33 | * @param array $b array to be merged from |
||
34 | * @return array the merged array |
||
35 | */ |
||
36 | public static function merge($a, $b) |
||
37 | { |
||
38 | $args = func_get_args(); |
||
39 | $res = array_shift($args); |
||
40 | foreach ($args as $items) { |
||
41 | if (!is_array($items)) { |
||
42 | continue; |
||
43 | } |
||
44 | foreach ($items as $k => $v) { |
||
45 | if (is_int($k)) { |
||
46 | if (isset($res[$k])) { |
||
47 | $res[] = $v; |
||
48 | } else { |
||
49 | $res[$k] = $v; |
||
50 | } |
||
51 | } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) { |
||
52 | $res[$k] = self::merge($res[$k], $v); |
||
53 | } else { |
||
54 | $res[$k] = $v; |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | return $res; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get specified items as array. |
||
64 | * @param mixed $keys specification |
||
65 | * @return array |
||
66 | */ |
||
67 | 2 | public static function getItems($array, $keys = null) |
|
68 | { |
||
69 | 2 | if (is_null($keys)) { |
|
70 | 2 | return $array; |
|
71 | } elseif (is_scalar($keys)) { |
||
72 | $keys = [$keys => $array[$keys]]; |
||
73 | } |
||
74 | $res = []; |
||
75 | foreach ($keys as $k) { |
||
76 | if (array_key_exists($k, $array)) { |
||
77 | $res[$k] = $array[$k]; |
||
78 | } |
||
79 | } |
||
80 | return $res; |
||
81 | } |
||
82 | |||
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 | * @param array $array source array |
||
117 | * @param array $items array of items |
||
118 | * @param string|array $where where to insert |
||
119 | * @return array new items list |
||
120 | * @see add() |
||
121 | */ |
||
122 | public static function insertInside(array $array, $items, $where) |
||
155 | |||
156 | /** |
||
157 | * Internal function to prepare where list for insertInside. |
||
158 | * @param array $array source array |
||
159 | * @param array|string $list array to convert |
||
160 | * @return array |
||
161 | */ |
||
162 | protected static function prepareWhere(array $array, $list) |
||
175 | |||
176 | /** |
||
177 | * Recursively removes duplicate values from non-associative arrays. |
||
178 | */ |
||
179 | public static function unique($array) |
||
192 | |||
193 | /** |
||
194 | * Non-recursively removes duplicate values from non-associative arrays. |
||
195 | */ |
||
196 | public static function uniqueFlat($array) |
||
211 | |||
212 | 1 | public static function toArray($object) |
|
223 | } |
||
224 |