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 |
||
| 19 | class Arr |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Add an element to an array using "dot" notation if it doesn't exist. |
||
| 23 | * |
||
| 24 | * @param array $array |
||
| 25 | * @param string $key |
||
| 26 | * @param mixed $value |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | public static function add($array, $key, $value) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Build a new array using a callback. |
||
| 41 | * |
||
| 42 | * @param array $array |
||
| 43 | * @param \Closure $callback |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public static function build($array, Closure $callback) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 61 | * |
||
| 62 | * @param array $array |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public static function divide($array) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Flatten a multi-dimensional associative array with dots. |
||
| 76 | * |
||
| 77 | * @param array $array |
||
| 78 | * @param string $prepend |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public static function dot($array, $prepend = '') |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get all of the given array except for a specified array of items. |
||
| 99 | * |
||
| 100 | * @param array $array |
||
| 101 | * @param array|string $keys |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public static function except($array, $keys) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Fetch a flattened array of a nested array element. |
||
| 112 | * |
||
| 113 | * @param array $array |
||
| 114 | * @param string $key |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public static function fetch($array, $key) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return the first element in an array passing a given truth test. |
||
| 136 | * |
||
| 137 | * @param array $array |
||
| 138 | * @param \Closure $callback |
||
| 139 | * @param mixed $default |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public static function first($array, $callback, $default = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the last element in an array passing a given truth test. |
||
| 156 | * |
||
| 157 | * @param array $array |
||
| 158 | * @param \Closure $callback |
||
| 159 | * @param mixed $default |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public static function last($array, $callback, $default = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Flatten a multi-dimensional array into a single level. |
||
| 170 | * |
||
| 171 | * @param array $array |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public static function flatten($array) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove one or many array items from a given array using "dot" notation. |
||
| 190 | * |
||
| 191 | * @param array $array |
||
| 192 | * @param array|string $keys |
||
| 193 | */ |
||
| 194 | public static function forget(&$array, $keys) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get an item from an array using "dot" notation. |
||
| 214 | * |
||
| 215 | * @param array $array |
||
| 216 | * @param string $key |
||
| 217 | * @param mixed $default |
||
| 218 | * |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | public static function get($array, $key, $default = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get a subset of the items from the given array. |
||
| 243 | * |
||
| 244 | * @param array $array |
||
| 245 | * @param array|string $keys |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public static function only($array, $keys) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Pluck an array of values from an array. |
||
| 256 | * |
||
| 257 | * @param array $array |
||
| 258 | * @param string $value |
||
| 259 | * @param string $key |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public static function pluck($array, $value, $key = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get a value from the array, and remove it. |
||
| 285 | * |
||
| 286 | * @param array $array |
||
| 287 | * @param string $key |
||
| 288 | * @param mixed $default |
||
| 289 | * |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public static function pull(&$array, $key, $default = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Set an array item to a given value using "dot" notation. |
||
| 302 | * |
||
| 303 | * If no key is given to the method, the entire array will be replaced. |
||
| 304 | * |
||
| 305 | * @param array $array |
||
| 306 | * @param string $key |
||
| 307 | * @param mixed $value |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public static function set(&$array, $key, $value) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Sort the array using the given Closure. |
||
| 336 | * |
||
| 337 | * @param array $array |
||
| 338 | * @param \Closure $callback |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public static function sort($array, Closure $callback) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Filter the array using the given Closure. |
||
| 355 | * |
||
| 356 | * @param array $array |
||
| 357 | * @param \Closure $callback |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public static function where($array, Closure $callback) |
||
| 373 | } |
||
| 374 |