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 an array of arrays into a single array. | ||
| 133 | * | ||
| 134 | * @param array $array | ||
| 135 | * | ||
| 136 | * @return array | ||
| 137 | */ | ||
| 138 | public function collapse(array $array) | ||
| 152 | |||
| 153 | /** | ||
| 154 | * Divide an array into two arrays. One with keys and the other with values. | ||
| 155 | * | ||
| 156 | * @param array $array | ||
| 157 | * | ||
| 158 | * @return array[] | ||
| 159 | */ | ||
| 160 | public function divide($array) | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Remove duplicated values. | ||
| 167 | * | ||
| 168 | * @param array $elements | ||
| 169 | * @param Closure|null $iterator | ||
| 170 | * | ||
| 171 | * @return array | ||
| 172 | */ | ||
| 173 | public function unique(array $elements, Closure $iterator = null) | ||
| 183 | |||
| 184 | /** | ||
| 185 | * Remove all instances of $ignore found in $elements (=== is used). | ||
| 186 | * | ||
| 187 | * @param array $elements | ||
|  | |||
| 188 | * @param array $ignore | ||
| 189 | * @return array | ||
| 190 | */ | ||
| 191 | public function without(array $array, array $ignore) | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Reindexes a list of values. | ||
| 204 | * | ||
| 205 | * @param array $array | ||
| 206 | * @param array $map An map of correspondances of the form | ||
| 207 | * ['currentIndex' => 'newIndex']. | ||
| 208 | * @param bool $keepUnmapped Whether or not to keep keys that are not | ||
| 209 | * remapped. | ||
| 210 | * | ||
| 211 | * @return array | ||
| 212 | */ | ||
| 213 | public function reindex(array $array, array $map, $keepUnmapped = true) | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Merges two arrays recursively. | ||
| 230 | * | ||
| 231 | * @param array $first Original data. | ||
| 232 | * @param array $second Data to be merged. | ||
| 233 | * | ||
| 234 | * @return array | ||
| 235 | */ | ||
| 236 | public function merge(array $first, array $second) | ||
| 252 | |||
| 253 | /** | ||
| 254 | * Extend one array with another. | ||
| 255 | * | ||
| 256 | * @param array $arrays | ||
| 257 | * | ||
| 258 | * @return array | ||
| 259 | */ | ||
| 260 | public function extend(array $arrays) | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Transforms a 1-dimensional array into a multi-dimensional one, | ||
| 279 | * exploding keys according to a separator. | ||
| 280 | * | ||
| 281 | * @param array $array | ||
| 282 | * | ||
| 283 | * @return array | ||
| 284 | */ | ||
| 285 | public function asHierarchy(array $array) | ||
| 307 | |||
| 308 | /** | ||
| 309 | * Separates elements from an array into groups. | ||
| 310 | * The function maps an element to the key that will be used for grouping. | ||
| 311 | * If no function is passed, the element itself will be used as key. | ||
| 312 | * | ||
| 313 | * @param array $array | ||
| 314 | * @param callable|null $callback | ||
| 315 | * | ||
| 316 | * @return array | ||
| 317 | */ | ||
| 318 | public function groupBy(array $array, callable $callback = null) | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Flatten a multi-dimensional associative array with dots. | ||
| 343 | * | ||
| 344 | * @param array $array | ||
| 345 | * @param string $prepend | ||
| 346 | * | ||
| 347 | * @return array | ||
| 348 | */ | ||
| 349 | public function dot($array, $prepend = '') | ||
| 369 | |||
| 370 | /** | ||
| 371 | * Flatten a nested array to a separated key. | ||
| 372 | * | ||
| 373 | * @param array $array | ||
| 374 | * @param string|null $separator | ||
| 375 | * @param string $prepend | ||
| 376 | * | ||
| 377 | * @return array | ||
| 378 | */ | ||
| 379 | public function flatten(array $array, $separator = null, $prepend = '') | ||
| 393 | |||
| 394 | /** | ||
| 395 | * Expand a flattened array with dots to a multi-dimensional associative array. | ||
| 396 | * | ||
| 397 | * @param array $array | ||
| 398 | * @param string $prepend | ||
| 399 | * | ||
| 400 | * @return array | ||
| 401 | */ | ||
| 402 | public function expand(array $array, $prepend = '') | ||
| 424 | |||
| 425 | /** | ||
| 426 | * Reset all numerical indexes of an array (start from zero). | ||
| 427 | * Non-numerical indexes will stay untouched. Returns a new array. | ||
| 428 | * | ||
| 429 | * @param array $array | ||
| 430 | * @param bool|false $deep | ||
| 431 | * | ||
| 432 | * @return array | ||
| 433 | */ | ||
| 434 | public function reset(array $array, $deep = false) | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Extend one array with another. Non associative arrays will not be merged | ||
| 455 | * but rather replaced. | ||
| 456 | * | ||
| 457 | * @param array $arrays | ||
| 458 | * | ||
| 459 | * @return array | ||
| 460 | */ | ||
| 461 | public function extendDistinct(array $arrays) | ||
| 481 | |||
| 482 | /** | ||
| 483 | * Recursively sort an array by keys and values. | ||
| 484 | * | ||
| 485 | * @param array $array | ||
| 486 | * | ||
| 487 | * @return array | ||
| 488 | */ | ||
| 489 | public function sortRecursive(array $array) | ||
| 507 | |||
| 508 | /** | ||
| 509 | * Will turn each element in $arr into an array then appending | ||
| 510 | * the associated indexs from the other arrays into this array as well. | ||
| 511 | * | ||
| 512 | * @param array $array | ||
| 513 | * @param array $arrays | ||
| 514 | * | ||
| 515 | * @return array | ||
| 516 | */ | ||
| 517 | public function zip(array $array, array $arrays) | ||
| 536 | } | ||
| 537 | 
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.