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 Transform 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 Transform, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 8 | class Transform | ||
| 9 | { | ||
| 10 | /** | ||
| 11 | * Dotted array cache. | ||
| 12 | * | ||
| 13 | * @var array | ||
| 14 | */ | ||
| 15 | protected $dotted = []; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * A instance of Access | ||
| 19 | * | ||
| 20 | * @var \Narrowspark\Arr\Access | ||
| 21 | */ | ||
| 22 | protected $access; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * A instance of Access | ||
| 26 | * | ||
| 27 | * @var \Narrowspark\Arr\Enumerator | ||
| 28 | */ | ||
| 29 | protected $enumerator; | ||
| 30 | |||
| 31 | public function __construct() | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Pop value from sub array. | ||
| 39 | * | ||
| 40 | * @param array $array | ||
| 41 | * @param string $key | ||
| 42 | * | ||
| 43 | * @return mixed | ||
| 44 | */ | ||
| 45 | public static function pop(array $array, $key) | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Swap two elements between positions. | ||
| 66 | * | ||
| 67 | * @param array $array array to swap | ||
| 68 | * @param string $swapA | ||
| 69 | * @param string $swapB | ||
| 70 | * | ||
| 71 | * @return array|null | ||
| 72 | */ | ||
| 73 | public function swap(array $array, $swapA, $swapB) | ||
| 79 | |||
| 80 | /** | ||
| 81 | * Create a new array consisting of every n-th element. | ||
| 82 | * | ||
| 83 | * @param array $array | ||
| 84 | * @param int $step | ||
| 85 | * @param int $offset | ||
| 86 | * | ||
| 87 | * @return array | ||
| 88 | */ | ||
| 89 | public static function every($array, $step, $offset = 0) | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Indexes an array depending on the values it contains. | ||
| 108 | * | ||
| 109 | * @param array $array | ||
| 110 | * @param callable $callback Function to combine values. | ||
| 111 | * @param bool $overwrite Should duplicate keys be overwritten? | ||
| 112 | * | ||
| 113 | * @return array Indexed values. | ||
| 114 | */ | ||
| 115 | public function combine(array $array, callable $callback, $overwrite = true) | ||
| 134 | |||
| 135 | /** | ||
| 136 | * Collapse a nested array down to an array of flat key=>value pairs | ||
| 137 | */ | ||
| 138 | public function collapse(array $array) | ||
| 157 | |||
| 158 | /** | ||
| 159 | * Divide an array into two arrays. One with keys and the other with values. | ||
| 160 | * | ||
| 161 | * @param array $array | ||
| 162 | * | ||
| 163 | * @return array[] | ||
| 164 | */ | ||
| 165 | public function divide($array) | ||
| 169 | |||
| 170 | /** | ||
| 171 | * Stripe all empty items. | ||
| 172 | * | ||
| 173 | * @param array $array | ||
| 174 | * | ||
| 175 | * @return array | ||
| 176 | */ | ||
| 177 | public function stripEmpty(array $array) | ||
| 191 | |||
| 192 | /** | ||
| 193 | * Remove all instances of $ignore found in $elements (=== is used). | ||
| 194 | * | ||
| 195 | * @param array $array | ||
| 196 | * @param array $ignore | ||
| 197 | * | ||
| 198 | * @return array | ||
| 199 | */ | ||
| 200 | public function without(array $array, array $ignore) | ||
| 210 | |||
| 211 | /** | ||
| 212 | * Reindexes a list of values. | ||
| 213 | * | ||
| 214 | * @param array $array | ||
| 215 | * @param array $map An map of correspondances of the form | ||
| 216 | * ['currentIndex' => 'newIndex']. | ||
| 217 | * @param bool $unmapped Whether or not to keep keys that are not | ||
| 218 | * remapped. | ||
| 219 | * | ||
| 220 | * @return array | ||
| 221 | */ | ||
| 222 | public function reindex(array $array, array $map, $unmapped = true) | ||
| 236 | |||
| 237 | /** | ||
| 238 | * Merges two or more arrays into one recursively. | ||
| 239 | * | ||
| 240 | * @param array $arrays. | ||
| 241 | * | ||
| 242 | * @return array | ||
| 243 | */ | ||
| 244 | public function merge(array $arrays) | ||
| 265 | |||
| 266 | /** | ||
| 267 | * Makes every value that is numerically indexed a key, given $default | ||
| 268 | * as value. | ||
| 269 | * | ||
| 270 | * @param array $array | ||
| 271 | * @param mixed $default | ||
| 272 | * | ||
| 273 | * @return array | ||
| 274 | */ | ||
| 275 | public function normalize(array $array, $default) | ||
| 290 | |||
| 291 | /** | ||
| 292 | * Extend one array with another. | ||
| 293 | * | ||
| 294 | * @param array $arrays | ||
| 295 | * | ||
| 296 | * @return array | ||
| 297 | */ | ||
| 298 | public function extend(array $arrays) | ||
| 314 | |||
| 315 | /** | ||
| 316 | * Transforms a 1-dimensional array into a multi-dimensional one, | ||
| 317 | * exploding keys according to a separator. | ||
| 318 | * | ||
| 319 | * @param array $array | ||
| 320 | * | ||
| 321 | * @return array | ||
| 322 | */ | ||
| 323 | public function asHierarchy(array $array) | ||
| 345 | |||
| 346 | /** | ||
| 347 | * Separates elements from an array into groups. | ||
| 348 | * The function maps an element to the key that will be used for grouping. | ||
| 349 | * If no function is passed, the element itself will be used as key. | ||
| 350 | * | ||
| 351 | * @param array $array | ||
| 352 | * @param callable|null $callback | ||
| 353 | * | ||
| 354 | * @return array | ||
| 355 | */ | ||
| 356 | public function groupBy(array $array, callable $callback = null) | ||
| 378 | |||
| 379 | /** | ||
| 380 | * Flatten a multi-dimensional associative array with dots. | ||
| 381 | * | ||
| 382 | * @param array $array | ||
| 383 | * @param string $prepend | ||
| 384 | * | ||
| 385 | * @return array | ||
| 386 | */ | ||
| 387 | public function dot($array, $prepend = '') | ||
| 407 | |||
| 408 | /** | ||
| 409 | * Flatten a nested array to a separated key. | ||
| 410 | * | ||
| 411 | * @param array $array | ||
| 412 | * @param string|null $separator | ||
| 413 | * @param string $prepend | ||
| 414 | * | ||
| 415 | * @return array | ||
| 416 | */ | ||
| 417 | public function flatten(array $array, $separator = null, $prepend = '') | ||
| 431 | |||
| 432 | /** | ||
| 433 | * Expand a flattened array with dots to a multi-dimensional associative array. | ||
| 434 | * | ||
| 435 | * @param array $array | ||
| 436 | * @param string $prepend | ||
| 437 | * | ||
| 438 | * @return array | ||
| 439 | */ | ||
| 440 | public function expand(array $array, $prepend = '') | ||
| 462 | |||
| 463 | /** | ||
| 464 | * Reset all numerical indexes of an array (start from zero). | ||
| 465 | * Non-numerical indexes will stay untouched. Returns a new array. | ||
| 466 | * | ||
| 467 | * @param array $array | ||
| 468 | * @param bool|false $deep | ||
| 469 | * | ||
| 470 | * @return array | ||
| 471 | */ | ||
| 472 | public function reset(array $array, $deep = false) | ||
| 490 | |||
| 491 | /** | ||
| 492 | * Extend one array with another. Non associative arrays will not be merged | ||
| 493 | * but rather replaced. | ||
| 494 | * | ||
| 495 | * @param array $arrays | ||
| 496 | * | ||
| 497 | * @return array | ||
| 498 | */ | ||
| 499 | public function extendDistinct(array $arrays) | ||
| 519 | |||
| 520 | /** | ||
| 521 | * Sort the array using the given callback. | ||
| 522 | * | ||
| 523 | * @param array $array | ||
| 524 | * @param callable $callback | ||
| 525 | * @param int $options | ||
| 526 | * @param bool $descending | ||
| 527 | * | ||
| 528 | * @return array | ||
| 529 | */ | ||
| 530 | public function sort(array $array, callable $callback, $options = SORT_REGULAR, $descending = false) | ||
| 553 | |||
| 554 | /** | ||
| 555 | * Recursively sort an array by keys and values. | ||
| 556 | * | ||
| 557 | * @param array $array | ||
| 558 | * | ||
| 559 | * @return array | ||
| 560 | */ | ||
| 561 | public function sortRecursive(array $array) | ||
| 579 | |||
| 580 | /** | ||
| 581 | * Will turn each element in $arr into an array then appending | ||
| 582 | * the associated indexs from the other arrays into this array as well. | ||
| 583 | * | ||
| 584 | * @param array $array | ||
| 585 | * @param array $arrays | ||
| 586 | * | ||
| 587 | * @return array | ||
| 588 | */ | ||
| 589 | public function zip(array $array, array $arrays) | ||
| 608 | |||
| 609 | /** | ||
| 610 | * Recurse through an array, add the leaf items to the $newArray var | ||
| 611 | * | ||
| 612 | * @param array $subject | ||
| 613 | * @param array &$newArray | ||
| 614 | * @param array $stack | ||
| 615 | * | ||
| 616 | * @return string[]|null | ||
| 617 | */ | ||
| 618 | private function recurseCollapse(array $subject, array &$newArray, $stack = []) | ||
| 632 | } | ||
| 633 |