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 Arrays 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 Arrays, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Arrays |
||
26 | { |
||
27 | /** |
||
28 | * Flattens given parameters into an array |
||
29 | * |
||
30 | * @param array $uValues values |
||
31 | * |
||
32 | * @return array flatten array |
||
33 | */ |
||
34 | public static function flat(...$uValues) |
||
52 | |||
53 | /** |
||
54 | * Gets the first element in array, otherwise returns default value |
||
55 | * |
||
56 | * @param array $uArray array |
||
57 | * @param mixed|null $uDefault default value |
||
58 | * |
||
59 | * @return mixed|null first element of array |
||
60 | */ |
||
61 | public static function getFirst(array $uArray, $uDefault = null) |
||
70 | |||
71 | /** |
||
72 | * Gets the specified element in array, otherwise returns default value |
||
73 | * |
||
74 | * @param array $uArray array |
||
75 | * @param mixed $uElement key |
||
76 | * @param mixed $uDefault default value |
||
77 | * |
||
78 | * @return mixed|null extracted element |
||
79 | */ |
||
80 | public static function get(array $uArray, $uElement, $uDefault = null) |
||
88 | |||
89 | /** |
||
90 | * Gets the specified elements in array |
||
91 | * |
||
92 | * @param array $uArray array |
||
93 | * @param array $uElements elements |
||
94 | * |
||
95 | * @return array array of extracted elements |
||
96 | */ |
||
97 | public static function getArray(array $uArray, ...$uElements) |
||
107 | |||
108 | /** |
||
109 | * Accesses child element by path notation, otherwise returns default value |
||
110 | * |
||
111 | * @param array $uArray array |
||
112 | * @param mixed $uElement key |
||
113 | * @param mixed $uDefault default value |
||
114 | * @param string $uSeparator path separator |
||
115 | * |
||
116 | * @return mixed|null extracted element |
||
117 | */ |
||
118 | public static function getPath(array $uArray, $uElement, $uDefault = null, $uSeparator = "/") |
||
132 | |||
133 | /** |
||
134 | * Accesses child elements by path notation |
||
135 | * |
||
136 | * @param array $uArray array |
||
137 | * @param array $uElements elements |
||
138 | * |
||
139 | * @return array array of extracted elements |
||
140 | */ |
||
141 | public static function getArrayPath(array $uArray, ...$uElements) |
||
162 | |||
163 | /** |
||
164 | * Gets a random element in array |
||
165 | * |
||
166 | * @param array $uArray array |
||
167 | * |
||
168 | * @return mixed|null a random element in the set |
||
169 | */ |
||
170 | public static function getRandom(array $uArray) |
||
183 | |||
184 | /** |
||
185 | * Returns an array filled with the elements in specified range |
||
186 | * |
||
187 | * @param int|float $uMinimum minimum number |
||
188 | * @param int|float $uMaximum maximum number |
||
189 | * @param int|float $uStep step |
||
190 | * @param bool $uWithKeys whether set keys or not |
||
191 | * |
||
192 | * @return array a set contains sequence of numbers in given range |
||
193 | */ |
||
194 | public static function range($uMinimum, $uMaximum, $uStep = 1, $uWithKeys = false) |
||
209 | |||
210 | /** |
||
211 | * Sorts an array by key |
||
212 | * |
||
213 | * @param array $uArray array |
||
214 | * @param mixed $uField field |
||
215 | * @param string $uOrder order |
||
216 | * |
||
217 | * @return array sorted array |
||
218 | */ |
||
219 | public static function sortByKey(array $uArray, $uField, $uOrder = "asc") |
||
243 | |||
244 | /** |
||
245 | * Categorizes an array by key |
||
246 | * |
||
247 | * @param array $uArray array |
||
248 | * @param mixed $uKey key |
||
249 | * @param bool $uPreserveKeys preserves keys |
||
250 | * |
||
251 | * @return array categorized array |
||
252 | */ |
||
253 | public static function categorize(array $uArray, $uKey, $uPreserveKeys = false) |
||
281 | |||
282 | /** |
||
283 | * Assigns keys by key |
||
284 | * |
||
285 | * @param array $uArray array |
||
286 | * @param mixed $uKey key |
||
287 | * |
||
288 | * @return array array with new keys |
||
289 | */ |
||
290 | public static function assignKeys(array $uArray, $uKey) |
||
300 | |||
301 | /** |
||
302 | * Extracts specified column from the array |
||
303 | * |
||
304 | * @param array $uArray array |
||
305 | * @param mixed $uKey key |
||
306 | * @param bool $uSkipEmpties whether skip empty entries or not |
||
307 | * @param bool $uDistinct whether returns multiple instances of same entries or not |
||
308 | * |
||
309 | * @return array values of the specified column from a multi-dimensional array |
||
310 | */ |
||
311 | public static function column(array $uArray, $uKey, $uSkipEmpties = false, $uDistinct = false) |
||
329 | |||
330 | /** |
||
331 | * Extracts specified columns from the array |
||
332 | * |
||
333 | * @param array $uArray array |
||
334 | * @param array $uKeys keys |
||
335 | * |
||
336 | * @return array values of the specified column from a multi-dimensional array |
||
337 | */ |
||
338 | public static function columns(array $uArray, ...$uKeys) |
||
356 | |||
357 | /** |
||
358 | * Gets the first matching row from a multi-dimensional array |
||
359 | * |
||
360 | * @param array $uArray array |
||
361 | * @param mixed $uKey key |
||
362 | * @param mixed $uValue value |
||
363 | * |
||
364 | * @return array|bool entire row matches the condition |
||
365 | */ |
||
366 | View Code Duplication | public static function getRow(array $uArray, $uKey, $uValue) |
|
376 | |||
377 | /** |
||
378 | * Gets the first matching row's key |
||
379 | * |
||
380 | * @param array $uArray array |
||
381 | * @param mixed $uKey key |
||
382 | * @param mixed $uValue value |
||
383 | * |
||
384 | * @return mixed|bool key of row matches the condition |
||
385 | */ |
||
386 | View Code Duplication | public static function getRowKey(array $uArray, $uKey, $uValue) |
|
396 | |||
397 | /** |
||
398 | * Gets the matching rows |
||
399 | * |
||
400 | * @param array $uArray array |
||
401 | * @param mixed $uKey key |
||
402 | * @param mixed $uValue value |
||
403 | * |
||
404 | * @return array set of elements matches the condition |
||
405 | */ |
||
406 | View Code Duplication | public static function getRows(array $uArray, $uKey, $uValue) |
|
418 | |||
419 | /** |
||
420 | * Gets the not matching rows |
||
421 | * |
||
422 | * @param array $uArray array |
||
423 | * @param mixed $uKey key |
||
424 | * @param mixed $uValue value |
||
425 | * |
||
426 | * @return array set of elements not matches the condition |
||
427 | */ |
||
428 | View Code Duplication | public static function getRowsBut(array $uArray, $uKey, $uValue) |
|
440 | |||
441 | /** |
||
442 | * Combines two arrays properly |
||
443 | * |
||
444 | * @param array $uArray1 first array |
||
445 | * @param array $uArray2 second array |
||
446 | * |
||
447 | * @return array combined array |
||
448 | */ |
||
449 | public static function combine(array $uArray1, array $uArray2) |
||
464 | |||
465 | /** |
||
466 | * Combines two or more arrays |
||
467 | * |
||
468 | * @param array $uArgs arguments |
||
469 | * |
||
470 | * @return array combined array |
||
471 | */ |
||
472 | public static function combine2(...$uArgs) |
||
499 | |||
500 | /** |
||
501 | * Sorts an array by priority list |
||
502 | * |
||
503 | * @param array $uArray array |
||
504 | * @param array $uPriorities list of priorities |
||
505 | * |
||
506 | * @return array sorted array |
||
507 | */ |
||
508 | public static function sortByPriority(array $uArray, $uPriorities) |
||
523 | } |
||
524 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.