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 |
||
| 20 | final class Arr |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Gets a dot-notated key from an array, with a default value if it does |
||
| 24 | * not exist. |
||
| 25 | * |
||
| 26 | * @param array $array The search array |
||
| 27 | * @param mixed $key The dot-notated key or array of keys |
||
| 28 | * @param string $default The default value |
||
| 29 | * @return mixed |
||
| 30 | * @codeCoverageIgnore |
||
| 31 | * @since 0.1.0 |
||
| 32 | */ |
||
| 33 | public static function get(array $array, $key, $default = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set an array item (dot-notated) to the value. |
||
| 68 | * |
||
| 69 | * @param array $array The array to insert it into |
||
| 70 | * @param mixed $key The dot-notated key to set or array of keys |
||
| 71 | * @param mixed $value The value |
||
| 72 | * @return void |
||
| 73 | * @codeCoverageIgnore |
||
| 74 | * @since 0.1.0 |
||
| 75 | */ |
||
| 76 | public static function set(array &$array, $key, $value = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Merge two arrays recursively, differs in two important ways from array_merge_recursive() |
||
| 106 | * - When there's two different values and not both arrays, the latter value overwrites the earlier |
||
| 107 | * instead of merging both into an array |
||
| 108 | * - Numeric keys that don't conflict aren't changed, only when a numeric key already exists is the |
||
| 109 | * value added using array_push() |
||
| 110 | * |
||
| 111 | * @param array multiple variables all of which must be arrays |
||
| 112 | * @throws InvalidArgumentException |
||
| 113 | * @return array |
||
| 114 | * @codeCoverageIgnore |
||
| 115 | * @since 0.1.0 |
||
| 116 | */ |
||
| 117 | public static function merge() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Merge 2 arrays recursively, differs in 2 important ways from array_merge_recursive() |
||
| 148 | * - When there's 2 different values and not both arrays, the latter value overwrites the earlier |
||
| 149 | * instead of merging both into an array |
||
| 150 | * - Numeric keys are never changed |
||
| 151 | * |
||
| 152 | * @param array multiple variables all of which must be arrays |
||
| 153 | * @throws InvalidArgumentException |
||
| 154 | * @return array |
||
| 155 | * @codeCoverageIgnore |
||
| 156 | * @since 0.1.0 |
||
| 157 | */ |
||
| 158 | public static function mergeAssoc() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Un-sets dot-notated key from an array |
||
| 186 | * |
||
| 187 | * @param array $array The search array |
||
| 188 | * @param mixed $key The dot-notated key or array of keys |
||
| 189 | * @return mixed |
||
| 190 | * @codeCoverageIgnore |
||
| 191 | * @since 0.1.0 |
||
| 192 | */ |
||
| 193 | public static function delete(array &$array, $key) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get array keys recursively |
||
| 229 | * |
||
| 230 | * @param array $array The search array |
||
| 231 | * @param int $maxDepth The search maximum depth |
||
| 232 | * @param int $depth The search depth |
||
| 233 | * @param array $arraykeys The array keys |
||
| 234 | * @return array |
||
| 235 | * @codeCoverageIgnore |
||
| 236 | * @since 0.1.0 |
||
| 237 | */ |
||
| 238 | public static function keys(array $array, $maxDepth = INF, $depth = 0, array $arraykeys = []) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get array keys recursively |
||
| 256 | * |
||
| 257 | * @param array $array The search array |
||
| 258 | * @param string $search The search value |
||
| 259 | * @return array |
||
| 260 | * @codeCoverageIgnore |
||
| 261 | * @since 0.1.2 |
||
| 262 | */ |
||
| 263 | public static function recursiveKeys(array $array, $search = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Pluck an array of values from an array. |
||
| 286 | * |
||
| 287 | * @param array $array collection of arrays to pluck from |
||
| 288 | * @param string $key key of the value to pluck |
||
| 289 | * @param string $index optional return array index key, true for original index |
||
| 290 | * @return array array of plucked values |
||
| 291 | * @codeCoverageIgnore |
||
| 292 | * @since 0.2.4 |
||
| 293 | */ |
||
| 294 | public static function pluck($array, $key, $index = null) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Sorts a multi-dimensional array by it's values. |
||
| 317 | * |
||
| 318 | * @access public |
||
| 319 | * @param array The array to fetch from |
||
| 320 | * @param string The key to sort by |
||
| 321 | * @param string The order (asc or desc) |
||
| 322 | * @param int The php sort type flag |
||
| 323 | * @return array |
||
| 324 | * @codeCoverageIgnore |
||
| 325 | * @since 0.2.4 |
||
| 326 | */ |
||
| 327 | public static function sort(array $array, $key, $order = 'asc', $sort_flags = SORT_REGULAR) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Sorts an array on multitiple values, with deep sorting support. |
||
| 367 | * |
||
| 368 | * @param array $array collection of arrays/objects to sort |
||
| 369 | * @param array $conditions sorting conditions |
||
| 370 | * @param bool @ignore_case wether to sort case insensitive |
||
| 371 | */ |
||
| 372 | public static function multiSort(array $array, $conditions, $ignore_case = false) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @codeCoverageIgnore |
||
| 400 | * @return string |
||
| 401 | * @since 0.1.2 |
||
| 402 | */ |
||
| 403 | public function __toString() |
||
| 407 | } |
||
| 408 | |||
| 410 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: