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 | 44 | 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) |
|
| 112 | { |
||
| 113 | 1 | $combined = []; |
|
| 114 | |||
| 115 | 1 | foreach ($array as $key => $value) { |
|
| 116 | 1 | $combinator = call_user_func($callback, $value, $key); |
|
| 117 | // fix for hhvm #1871 bug |
||
| 118 | 1 | if (defined('HHVM_VERSION')) { |
|
| 119 | $combinator->next(); |
||
| 120 | } |
||
| 121 | |||
| 122 | 1 | $index = $combinator->key(); |
|
| 123 | |||
| 124 | 1 | if ($overwrite || ! isset($combined[$index])) { |
|
| 125 | 1 | $combined[$index] = $combinator->current(); |
|
| 126 | 1 | } |
|
| 127 | 1 | } |
|
| 128 | |||
| 129 | 1 | return $combined; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Collapse a nested array down to an array of flat key=>value pairs |
||
| 134 | */ |
||
| 135 | 3 | public function collapse(array $array) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 157 | * |
||
| 158 | * @param array $array |
||
| 159 | * |
||
| 160 | * @return array[] |
||
| 161 | */ |
||
| 162 | 1 | public function divide($array) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Stripe all empty items. |
||
| 169 | * |
||
| 170 | * @param array $array |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 1 | public function stripEmpty(array $array) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Remove all instances of $ignore found in $elements (=== is used). |
||
| 191 | * |
||
| 192 | * @param array $array |
||
| 193 | * @param array $ignore |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | 1 | public function without(array $array, array $ignore) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Reindexes a list of values. |
||
| 210 | * |
||
| 211 | * @param array $array |
||
| 212 | * @param array $map An map of correspondances of the form |
||
| 213 | * ['currentIndex' => 'newIndex']. |
||
| 214 | * @param bool $unmapped Whether or not to keep keys that are not |
||
| 215 | * remapped. |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | 1 | public function reindex(array $array, array $map, $unmapped = true) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Merges two or more arrays into one recursively. |
||
| 236 | * |
||
| 237 | * @param array $arrays. |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 4 | public function merge(array $arrays) |
|
| 242 | { |
||
| 243 | 4 | $args = func_get_args(); |
|
| 244 | 4 | $array = array_shift($args); |
|
| 245 | |||
| 246 | 4 | while (! empty($args)) { |
|
| 247 | 4 | $next = array_shift($args); |
|
| 248 | |||
| 249 | 4 | foreach ($next as $key => $value) { |
|
| 250 | 4 | if (is_int($key)) { |
|
| 251 | 3 | if (isset($array[$key])) { |
|
| 252 | 2 | $array[] = $value; |
|
| 253 | 2 | } else { |
|
| 254 | 1 | $array[$key] = $value; |
|
| 255 | } |
||
| 256 | 4 | } elseif (is_array($value) && isset($array[$key]) && is_array($array[$key])) { |
|
| 257 | 1 | $array[$key] = $this->merge($array[$key], $value); |
|
| 258 | 1 | } else { |
|
| 259 | 2 | $array[$key] = $value; |
|
| 260 | } |
||
| 261 | 4 | } |
|
| 262 | 4 | } |
|
| 263 | |||
| 264 | 4 | return $array; |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Makes every value that is numerically indexed a key, given $default |
||
| 269 | * as value. |
||
| 270 | * |
||
| 271 | * @param array $array |
||
| 272 | * @param mixed $default |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | 1 | public function normalize(array $array, $default) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Extend one array with another. |
||
| 294 | * |
||
| 295 | * @param array $arrays |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 3 | public function extend(array $arrays) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Transforms a 1-dimensional array into a multi-dimensional one, |
||
| 318 | * exploding keys according to a separator. |
||
| 319 | * |
||
| 320 | * @param array $array |
||
| 321 | * |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | 1 | public function asHierarchy(array $array) |
|
| 325 | { |
||
| 326 | 1 | $hierarchy = []; |
|
| 327 | |||
| 328 | 1 | foreach ($array as $key => $value) { |
|
| 329 | 1 | $segments = explode('.', $key); |
|
| 330 | 1 | $valueSegment = array_pop($segments); |
|
| 331 | 1 | $branch = &$hierarchy; |
|
| 332 | |||
| 333 | 1 | foreach ($segments as $segment) { |
|
| 334 | 1 | if (! isset($branch[$segment])) { |
|
| 335 | 1 | $branch[$segment] = []; |
|
| 336 | 1 | } |
|
| 337 | |||
| 338 | 1 | $branch = &$branch[$segment]; |
|
| 339 | 1 | } |
|
| 340 | |||
| 341 | 1 | $branch[$valueSegment] = $value; |
|
| 342 | 1 | } |
|
| 343 | |||
| 344 | 1 | return $hierarchy; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Separates elements from an array into groups. |
||
| 349 | * The function maps an element to the key that will be used for grouping. |
||
| 350 | * If no function is passed, the element itself will be used as key. |
||
| 351 | * |
||
| 352 | * @param array $array |
||
| 353 | * @param callable|null $callback |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | 1 | public function groupBy(array $array, callable $callback = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Flatten a multi-dimensional associative array with dots. |
||
| 382 | * |
||
| 383 | * @param array $array |
||
| 384 | * @param string $prepend |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | 4 | public function dot($array, $prepend = '') |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Expand a dotted array. Acts the opposite way of Arr::dot(). |
||
| 411 | * |
||
| 412 | * @param array $array |
||
| 413 | * @param bool $recursively |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 4 | public static function unDot($array, $recursively = true) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Flatten a nested array to a separated key. |
||
| 442 | * |
||
| 443 | * @param array $array |
||
| 444 | * @param string|null $separator |
||
| 445 | * @param string $prepend |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 1 | public function flatten(array $array, $separator = null, $prepend = '') |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Expand a flattened array with dots to a multi-dimensional associative array. |
||
| 466 | * |
||
| 467 | * @param array $array |
||
| 468 | * @param string $prepend |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | 4 | public function expand(array $array, $prepend = '') |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Reset all numerical indexes of an array (start from zero). |
||
| 497 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
| 498 | * |
||
| 499 | * @param array $array |
||
| 500 | * @param bool|false $deep |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | 3 | public function reset(array $array, $deep = false) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Extend one array with another. Non associative arrays will not be merged |
||
| 525 | * but rather replaced. |
||
| 526 | * |
||
| 527 | * @param array $arrays |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | 4 | public function extendDistinct(array $arrays) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Sort the array using the given callback. |
||
| 554 | * |
||
| 555 | * @param array $array |
||
| 556 | * @param callable $callback |
||
| 557 | * @param int $options |
||
| 558 | * @param bool $descending |
||
| 559 | * |
||
| 560 | * @return array |
||
| 561 | */ |
||
| 562 | 1 | public function sort(array $array, callable $callback, $options = SORT_REGULAR, $descending = false) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Recursively sort an array by keys and values. |
||
| 588 | * |
||
| 589 | * @param array $array |
||
| 590 | * |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | 1 | public function sortRecursive(array $array) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Will turn each element in $arr into an array then appending |
||
| 614 | * the associated indexs from the other arrays into this array as well. |
||
| 615 | * |
||
| 616 | * @param array $array |
||
| 617 | * @param array $arrays |
||
| 618 | * |
||
| 619 | * @return array |
||
| 620 | */ |
||
| 621 | 2 | public function zip(array $array, array $arrays) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Recurse through an array, add the leaf items to the $newArray var |
||
| 643 | * |
||
| 644 | * @param array $subject |
||
| 645 | * @param array &$newArray |
||
| 646 | * @param array $stack |
||
| 647 | * |
||
| 648 | * @return string[]|null |
||
| 649 | */ |
||
| 650 | 3 | private function recurseCollapse(array $subject, array &$newArray, $stack = []) |
|
| 664 | } |
||
| 665 |