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 | ||
| 4 | class Transform | ||
| 5 | { | ||
| 6 | /** | ||
| 7 | * Dotted array cache. | ||
| 8 | * | ||
| 9 | * @var array | ||
| 10 | */ | ||
| 11 | protected $dotted = []; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * A instance of Access | ||
| 15 | * | ||
| 16 | * @var \Narrowspark\Arr\Access | ||
| 17 | */ | ||
| 18 | protected $access; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * A instance of Access | ||
| 22 | * | ||
| 23 | * @var \Narrowspark\Arr\Enumerator | ||
| 24 | */ | ||
| 25 | protected $enumerator; | ||
| 26 | |||
| 27 | 50 | public function __construct() | |
| 32 | |||
| 33 | /** | ||
| 34 | * Pop value from sub array. | ||
| 35 | * | ||
| 36 | * @param array $array | ||
| 37 | * @param string $key | ||
| 38 | * | ||
| 39 | * @return mixed | ||
| 40 | */ | ||
| 41 | 1 | public static function pop(array $array, $key) | |
| 42 |     { | ||
| 43 | 1 |         $keys = explode('.', $key); | |
| 44 | |||
| 45 | 1 |         foreach ($keys as $key) { | |
| 46 | 1 |             if (! isset($array[$key])) { | |
| 47 | 1 | return; | |
| 48 | } | ||
| 49 | |||
| 50 | 1 | $array = $array[$key]; | |
| 51 | 1 | } | |
| 52 | |||
| 53 | 1 |         if (! is_array($array)) { | |
| 54 | 1 | return; | |
| 55 | } | ||
| 56 | |||
| 57 | 1 | return array_pop($array); | |
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Swap two elements between positions. | ||
| 62 | * | ||
| 63 | * @param array $array array to swap | ||
| 64 | * @param string $swapA | ||
| 65 | * @param string $swapB | ||
| 66 | * | ||
| 67 | * @return array|null | ||
| 68 | */ | ||
| 69 | 1 | public function swap(array $array, $swapA, $swapB) | |
| 75 | |||
| 76 | /** | ||
| 77 | * Create a new array consisting of every n-th element. | ||
| 78 | * | ||
| 79 | * @param array $array | ||
| 80 | * @param int $step | ||
| 81 | * @param int $offset | ||
| 82 | * | ||
| 83 | * @return array | ||
| 84 | */ | ||
| 85 | 1 | public static function every($array, $step, $offset = 0) | |
| 101 | |||
| 102 | /** | ||
| 103 | * Indexes an array depending on the values it contains. | ||
| 104 | * | ||
| 105 | * @param array $array | ||
| 106 | * @param callable $callback Function to combine values. | ||
| 107 | * @param bool $overwrite Should duplicate keys be overwritten? | ||
| 108 | * | ||
| 109 | * @return array Indexed values. | ||
| 110 | */ | ||
| 111 | 1 | public function combine(array $array, callable $callback, $overwrite = true) | |
| 132 | |||
| 133 | /** | ||
| 134 | * Collapse a nested array down to an array of flat key=>value pairs | ||
| 135 | */ | ||
| 136 | 3 | public function collapse(array $array) | |
| 155 | |||
| 156 | /** | ||
| 157 | * Divide an array into two arrays. One with keys and the other with values. | ||
| 158 | * | ||
| 159 | * @param array $array | ||
| 160 | * | ||
| 161 | * @return array[] | ||
| 162 | */ | ||
| 163 | 1 | public function divide($array) | |
| 167 | |||
| 168 | /** | ||
| 169 | * Stripe all empty items. | ||
| 170 | * | ||
| 171 | * @param array $array | ||
| 172 | * | ||
| 173 | * @return array | ||
| 174 | */ | ||
| 175 | 1 | public function stripEmpty(array $array) | |
| 189 | |||
| 190 | /** | ||
| 191 | * Remove all instances of $ignore found in $elements (=== is used). | ||
| 192 | * | ||
| 193 | * @param array $array | ||
| 194 | * @param array $ignore | ||
| 195 | * | ||
| 196 | * @return array | ||
| 197 | */ | ||
| 198 | 1 | public function without(array $array, array $ignore) | |
| 208 | |||
| 209 | /** | ||
| 210 | * Reindexes a list of values. | ||
| 211 | * | ||
| 212 | * @param array $array | ||
| 213 | * @param array $map An map of correspondances of the form | ||
| 214 | * ['currentIndex' => 'newIndex']. | ||
| 215 | * @param bool $unmapped Whether or not to keep keys that are not | ||
| 216 | * remapped. | ||
| 217 | * | ||
| 218 | * @return array | ||
| 219 | */ | ||
| 220 | 1 | public function reindex(array $array, array $map, $unmapped = true) | |
| 234 | |||
| 235 | /** | ||
| 236 | * Merges two or more arrays into one recursively. | ||
| 237 | * | ||
| 238 | * @param array $arrays. | ||
| 239 | * | ||
| 240 | * @return array | ||
| 241 | */ | ||
| 242 | 4 | public function merge(array $arrays) | |
| 267 | |||
| 268 | /** | ||
| 269 | * Makes every value that is numerically indexed a key, given $default | ||
| 270 | * as value. | ||
| 271 | * | ||
| 272 | * @param array $array | ||
| 273 | * @param mixed $default | ||
| 274 | * | ||
| 275 | * @return array | ||
| 276 | */ | ||
| 277 | 1 | public function normalize(array $array, $default) | |
| 292 | |||
| 293 | /** | ||
| 294 | * Extend one array with another. | ||
| 295 | * | ||
| 296 | * @param array $arrays | ||
| 297 | * | ||
| 298 | * @return array | ||
| 299 | */ | ||
| 300 | 3 | public function extend(array $arrays) | |
| 316 | |||
| 317 | /** | ||
| 318 | * Transforms a 1-dimensional array into a multi-dimensional one, | ||
| 319 | * exploding keys according to a separator. | ||
| 320 | * | ||
| 321 | * @param array $array | ||
| 322 | * | ||
| 323 | * @return array | ||
| 324 | */ | ||
| 325 | 1 | public function asHierarchy(array $array) | |
| 347 | |||
| 348 | /** | ||
| 349 | * Separates elements from an array into groups. | ||
| 350 | * The function maps an element to the key that will be used for grouping. | ||
| 351 | * If no function is passed, the element itself will be used as key. | ||
| 352 | * | ||
| 353 | * @param array $array | ||
| 354 | * @param callable|null $callback | ||
| 355 | * | ||
| 356 | * @return array | ||
| 357 | */ | ||
| 358 | 1 | public function groupBy(array $array, callable $callback = null) | |
| 380 | |||
| 381 | /** | ||
| 382 | * Flatten a multi-dimensional associative array with dots. | ||
| 383 | * | ||
| 384 | * @param array $array | ||
| 385 | * @param string $prepend | ||
| 386 | * | ||
| 387 | * @return array | ||
| 388 | */ | ||
| 389 | 4 | public function dot($array, $prepend = '') | |
| 409 | |||
| 410 | /** | ||
| 411 | * Expand a dotted array. Acts the opposite way of Arr::dot(). | ||
| 412 | * | ||
| 413 | * @param array $array | ||
| 414 | * @param bool $depth | ||
| 415 | * | ||
| 416 | * @return array | ||
| 417 | */ | ||
| 418 | 10 | public function unDot($array, $depth = INF) | |
| 438 | |||
| 439 | /** | ||
| 440 | * Flatten a nested array to a separated key. | ||
| 441 | * | ||
| 442 | * @param array $array | ||
| 443 | * @param string|null $separator | ||
| 444 | * @param string $prepend | ||
| 445 | * | ||
| 446 | * @return array | ||
| 447 | */ | ||
| 448 | 1 | public function flatten(array $array, $separator = null, $prepend = '') | |
| 462 | |||
| 463 | /** | ||
| 464 | * Expand a flattened array with dots to a multi-dimensional associative array. | ||
| 465 | * | ||
| 466 | * @param array $array | ||
| 467 | * @param string $prepend | ||
| 468 | * | ||
| 469 | * @return array | ||
| 470 | */ | ||
| 471 | 4 | public function expand(array $array, $prepend = '') | |
| 493 | |||
| 494 | /** | ||
| 495 | * Reset all numerical indexes of an array (start from zero). | ||
| 496 | * Non-numerical indexes will stay untouched. Returns a new array. | ||
| 497 | * | ||
| 498 | * @param array $array | ||
| 499 | * @param bool|false $deep | ||
| 500 | * | ||
| 501 | * @return array | ||
| 502 | */ | ||
| 503 | 3 | public function reset(array $array, $deep = false) | |
| 521 | |||
| 522 | /** | ||
| 523 | * Extend one array with another. Non associative arrays will not be merged | ||
| 524 | * but rather replaced. | ||
| 525 | * | ||
| 526 | * @param array $arrays | ||
| 527 | * | ||
| 528 | * @return array | ||
| 529 | */ | ||
| 530 | 4 | public function extendDistinct(array $arrays) | |
| 550 | |||
| 551 | /** | ||
| 552 | * Sort the array using the given callback. | ||
| 553 | * | ||
| 554 | * @param array $array | ||
| 555 | * @param callable $callback | ||
| 556 | * @param int $options | ||
| 557 | * @param bool $descending | ||
| 558 | * | ||
| 559 | * @return array | ||
| 560 | */ | ||
| 561 | 1 | public function sort(array $array, callable $callback, $options = SORT_REGULAR, $descending = false) | |
| 584 | |||
| 585 | /** | ||
| 586 | * Recursively sort an array by keys and values. | ||
| 587 | * | ||
| 588 | * @param array $array | ||
| 589 | * | ||
| 590 | * @return array | ||
| 591 | */ | ||
| 592 | 1 | public function sortRecursive(array $array) | |
| 610 | |||
| 611 | /** | ||
| 612 | * Will turn each element in $arr into an array then appending | ||
| 613 | * the associated indexs from the other arrays into this array as well. | ||
| 614 | * | ||
| 615 | * @param array $array | ||
| 616 | * @param array $arrays | ||
| 617 | * | ||
| 618 | * @return array | ||
| 619 | */ | ||
| 620 | 2 | public function zip(array $array, array $arrays) | |
| 639 | |||
| 640 | /** | ||
| 641 | * Recurse through an array, add the leaf items to the $newArray var | ||
| 642 | * | ||
| 643 | * @param array $subject | ||
| 644 | * @param array &$newArray | ||
| 645 | * @param array $stack | ||
| 646 | * | ||
| 647 | * @return string[]|null | ||
| 648 | */ | ||
| 649 | 3 | private function recurseCollapse(array $subject, array &$newArray, $stack = []) | |
| 663 | } | ||
| 664 |