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 | * Stripe all empty items. |
||
167 | * |
||
168 | * @param array $source |
||
|
|||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | public function stripEmpty(array $array) |
||
188 | |||
189 | /** |
||
190 | * Remove duplicated values. |
||
191 | * |
||
192 | * @param array $elements |
||
193 | * @param Closure|null $iterator |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function unique(array $elements, Closure $iterator = null) |
||
207 | |||
208 | /** |
||
209 | * Remove all instances of $ignore found in $elements (=== is used). |
||
210 | * |
||
211 | * @param array $elements |
||
212 | * @param array $ignore |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | public function without(array $array, array $ignore) |
||
226 | |||
227 | /** |
||
228 | * Reindexes a list of values. |
||
229 | * |
||
230 | * @param array $array |
||
231 | * @param array $map An map of correspondances of the form |
||
232 | * ['currentIndex' => 'newIndex']. |
||
233 | * @param bool $keepUnmapped Whether or not to keep keys that are not |
||
234 | * remapped. |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function reindex(array $array, array $map, $keepUnmapped = true) |
||
252 | |||
253 | /** |
||
254 | * Merges two arrays recursively. |
||
255 | * |
||
256 | * @param array $first Original data. |
||
257 | * @param array $second Data to be merged. |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | public function merge(array $first, array $second) |
||
277 | |||
278 | /** |
||
279 | * Extend one array with another. |
||
280 | * |
||
281 | * @param array $arrays |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function extend(array $arrays) |
||
301 | |||
302 | /** |
||
303 | * Transforms a 1-dimensional array into a multi-dimensional one, |
||
304 | * exploding keys according to a separator. |
||
305 | * |
||
306 | * @param array $array |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function asHierarchy(array $array) |
||
332 | |||
333 | /** |
||
334 | * Separates elements from an array into groups. |
||
335 | * The function maps an element to the key that will be used for grouping. |
||
336 | * If no function is passed, the element itself will be used as key. |
||
337 | * |
||
338 | * @param array $array |
||
339 | * @param callable|null $callback |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | public function groupBy(array $array, callable $callback = null) |
||
365 | |||
366 | /** |
||
367 | * Flatten a multi-dimensional associative array with dots. |
||
368 | * |
||
369 | * @param array $array |
||
370 | * @param string $prepend |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function dot($array, $prepend = '') |
||
394 | |||
395 | /** |
||
396 | * Flatten a nested array to a separated key. |
||
397 | * |
||
398 | * @param array $array |
||
399 | * @param string|null $separator |
||
400 | * @param string $prepend |
||
401 | * |
||
402 | * @return array |
||
403 | */ |
||
404 | public function flatten(array $array, $separator = null, $prepend = '') |
||
418 | |||
419 | /** |
||
420 | * Expand a flattened array with dots to a multi-dimensional associative array. |
||
421 | * |
||
422 | * @param array $array |
||
423 | * @param string $prepend |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | public function expand(array $array, $prepend = '') |
||
449 | |||
450 | /** |
||
451 | * Reset all numerical indexes of an array (start from zero). |
||
452 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
453 | * |
||
454 | * @param array $array |
||
455 | * @param bool|false $deep |
||
456 | * |
||
457 | * @return array |
||
458 | */ |
||
459 | public function reset(array $array, $deep = false) |
||
477 | |||
478 | /** |
||
479 | * Extend one array with another. Non associative arrays will not be merged |
||
480 | * but rather replaced. |
||
481 | * |
||
482 | * @param array $arrays |
||
483 | * |
||
484 | * @return array |
||
485 | */ |
||
486 | public function extendDistinct(array $arrays) |
||
506 | |||
507 | /** |
||
508 | * Recursively sort an array by keys and values. |
||
509 | * |
||
510 | * @param array $array |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | public function sortRecursive(array $array) |
||
532 | |||
533 | /** |
||
534 | * Will turn each element in $arr into an array then appending |
||
535 | * the associated indexs from the other arrays into this array as well. |
||
536 | * |
||
537 | * @param array $array |
||
538 | * @param array $arrays |
||
539 | * |
||
540 | * @return array |
||
541 | */ |
||
542 | public function zip(array $array, array $arrays) |
||
561 | } |
||
562 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.