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) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Collapse a nested array down to an array of flat key=>value pairs |
||
| 133 | */ |
||
| 134 | public function collapse(array $array) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 156 | * |
||
| 157 | * @param array $array |
||
| 158 | * |
||
| 159 | * @return array[] |
||
| 160 | */ |
||
| 161 | public function divide($array) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Stripe all empty items. |
||
| 168 | * |
||
| 169 | * @param array $source |
||
|
|
|||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function stripEmpty(array $array) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove all instances of $ignore found in $elements (=== is used). |
||
| 190 | * |
||
| 191 | * @param array $elements |
||
| 192 | * @param array $ignore |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function without(array $array, array $ignore) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Reindexes a list of values. |
||
| 209 | * |
||
| 210 | * @param array $array |
||
| 211 | * @param array $map An map of correspondances of the form |
||
| 212 | * ['currentIndex' => 'newIndex']. |
||
| 213 | * @param bool $unmapped Whether or not to keep keys that are not |
||
| 214 | * remapped. |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function reindex(array $array, array $map, $unmapped = true) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Merges two or more arrays into one recursively. |
||
| 235 | * |
||
| 236 | * @param array $arrays. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function merge(array $arrays) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Makes every value that is numerically indexed a key, given $default |
||
| 264 | * as value. |
||
| 265 | * |
||
| 266 | * @param array $array |
||
| 267 | * @param mixed $default |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function normalize(array $array, $default) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Extend one array with another. |
||
| 289 | * |
||
| 290 | * @param array $arrays |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function extend(array $arrays) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Transforms a 1-dimensional array into a multi-dimensional one, |
||
| 313 | * exploding keys according to a separator. |
||
| 314 | * |
||
| 315 | * @param array $array |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function asHierarchy(array $array) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Separates elements from an array into groups. |
||
| 344 | * The function maps an element to the key that will be used for grouping. |
||
| 345 | * If no function is passed, the element itself will be used as key. |
||
| 346 | * |
||
| 347 | * @param array $array |
||
| 348 | * @param callable|null $callback |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function groupBy(array $array, callable $callback = null) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Flatten a multi-dimensional associative array with dots. |
||
| 377 | * |
||
| 378 | * @param array $array |
||
| 379 | * @param string $prepend |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function dot($array, $prepend = '') |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Flatten a nested array to a separated key. |
||
| 406 | * |
||
| 407 | * @param array $array |
||
| 408 | * @param string|null $separator |
||
| 409 | * @param string $prepend |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | public function flatten(array $array, $separator = null, $prepend = '') |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Expand a flattened array with dots to a multi-dimensional associative array. |
||
| 430 | * |
||
| 431 | * @param array $array |
||
| 432 | * @param string $prepend |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function expand(array $array, $prepend = '') |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Reset all numerical indexes of an array (start from zero). |
||
| 461 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
| 462 | * |
||
| 463 | * @param array $array |
||
| 464 | * @param bool|false $deep |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | public function reset(array $array, $deep = false) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Extend one array with another. Non associative arrays will not be merged |
||
| 489 | * but rather replaced. |
||
| 490 | * |
||
| 491 | * @param array $arrays |
||
| 492 | * |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function extendDistinct(array $arrays) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Sort the array using the given callback. |
||
| 518 | * |
||
| 519 | * @param array $array |
||
| 520 | * @param callable $callback |
||
| 521 | * @param int $options |
||
| 522 | * @param bool $descending |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function sort(array $array, callable $callback, $options = SORT_REGULAR, $descending = false) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Recursively sort an array by keys and values. |
||
| 552 | * |
||
| 553 | * @param array $array |
||
| 554 | * |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | public function sortRecursive(array $array) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Will turn each element in $arr into an array then appending |
||
| 578 | * the associated indexs from the other arrays into this array as well. |
||
| 579 | * |
||
| 580 | * @param array $array |
||
| 581 | * @param array $arrays |
||
| 582 | * |
||
| 583 | * @return array |
||
| 584 | */ |
||
| 585 | public function zip(array $array, array $arrays) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Recurse through an array, add the leaf items to the $newArray var |
||
| 607 | * |
||
| 608 | * @param array $subject |
||
| 609 | * @param array &$newArray |
||
| 610 | * @param array $stack |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | private function recurseCollapse(array $subject, array &$newArray, $stack = []) |
||
| 628 | } |
||
| 629 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.