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 |
||
| 15 | class ArrayHelper |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Builds a map (key => value) from a multidimensional array or an array of objects. |
||
| 19 | * |
||
| 20 | * @param $array |
||
| 21 | * @param string|\Closure $from |
||
| 22 | * @param string|\Closure $to |
||
| 23 | * @param string|\Closure|null $group |
||
| 24 | * @return array |
||
| 25 | */ |
||
| 26 | public static function map($array, $from, $to, $group = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Merges two or more arrays into one recursively. |
||
| 44 | * |
||
| 45 | * @param $a |
||
| 46 | * @param $b |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public static function merge($a, $b) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Checks if the given array contains the specified key. |
||
| 75 | * |
||
| 76 | * @param $key |
||
| 77 | * @param $array |
||
| 78 | * @param bool $caseSensitive |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public static function keyExists($key, $array, $caseSensitive = true) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Indexes an array according to a specified key. |
||
| 98 | * |
||
| 99 | * @param $array |
||
| 100 | * @param $key |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public static function index($array, $key) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Converts an object or an array of objects into an array. |
||
| 116 | * |
||
| 117 | * @param $object |
||
| 118 | * @param array $properties |
||
| 119 | * @param bool $recursive |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public static function toArray($object, $properties = [], $recursive = true) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Retrieves the value of an array element or object property with the given key or property name. |
||
| 164 | * |
||
| 165 | * @param $array |
||
| 166 | * @param $key |
||
| 167 | * @param null $default |
||
| 168 | * @return mixed|null |
||
| 169 | */ |
||
| 170 | public static function getValue($array, $key, $default = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Removes an item from an array and returns the value. |
||
| 204 | * |
||
| 205 | * @param $array |
||
| 206 | * @param $key |
||
| 207 | * @param null $default |
||
| 208 | * @return mixed|null |
||
| 209 | */ |
||
| 210 | public static function remove(&$array, $key, $default = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the first value in a given array. |
||
| 224 | * |
||
| 225 | * @param $arr |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | public static function getFirstValue($arr) |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Finds matching values from two arrays |
||
| 245 | * |
||
| 246 | * @param array $keys |
||
| 247 | * @param array $array |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public static function matches($keys = [], $array = []): array |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $array |
||
| 269 | * @param $findKey |
||
| 270 | * @param $newKey |
||
| 271 | * @param $newValue |
||
| 272 | * @return array|bool |
||
| 273 | */ |
||
| 274 | public static function insertBefore(array $array, $findKey, $newKey, $newValue) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param array $array |
||
| 291 | * @param $findKey |
||
| 292 | * @param $newKey |
||
| 293 | * @param $newValue |
||
| 294 | * @return array|bool |
||
| 295 | */ |
||
| 296 | public static function insertAfter(array $array, $findKey, $newKey, $newValue) |
||
| 310 | } |
||
| 311 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.