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 |
||
7 | class Transform |
||
8 | { |
||
9 | /** |
||
10 | * Dotted array cache. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $dotted = []; |
||
15 | |||
16 | /** |
||
17 | * A instance of Access |
||
18 | * |
||
19 | * @var \Narrowspark\Arr\Access |
||
20 | */ |
||
21 | protected $access; |
||
22 | |||
23 | /** |
||
24 | * A instance of Access |
||
25 | * |
||
26 | * @var \Narrowspark\Arr\Enumerator |
||
27 | */ |
||
28 | protected $enumerator; |
||
29 | |||
30 | public function __construct() |
||
35 | |||
36 | /** |
||
37 | * Swap two elements between positions. |
||
38 | * |
||
39 | * @param array $array array to swap |
||
40 | * @param string $swapA |
||
41 | * @param string $swapB |
||
42 | * |
||
43 | * @return array|null |
||
44 | */ |
||
45 | public function swap(array $array, $swapA, $swapB) |
||
51 | |||
52 | /** |
||
53 | * Create a new array consisting of every n-th element. |
||
54 | * |
||
55 | * @param array $array |
||
56 | * @param int $step |
||
57 | * @param int $offset |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public static function every($array, $step, $offset = 0) |
||
77 | |||
78 | /** |
||
79 | * Indexes an array depending on the values it contains. |
||
80 | * |
||
81 | * @param array $array |
||
82 | * @param callable $callback Function to combine values. |
||
83 | * @param bool $overwrite Should duplicate keys be overwritten? |
||
84 | * |
||
85 | * @return array Indexed values. |
||
86 | */ |
||
87 | public function combine(array $array, callable $callback, $overwrite = true) |
||
102 | |||
103 | /** |
||
104 | * Collapse an array of arrays into a single array. |
||
105 | * |
||
106 | * @param array $array |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function collapse(array $array) |
||
124 | |||
125 | /** |
||
126 | * Divide an array into two arrays. One with keys and the other with values. |
||
127 | * |
||
128 | * @param array $array |
||
129 | * |
||
130 | * @return array[] |
||
131 | */ |
||
132 | public function divide($array) |
||
136 | |||
137 | /** |
||
138 | * Reindexes a list of values. |
||
139 | * |
||
140 | * @param array $array |
||
141 | * @param array $map An map of correspondances of the form |
||
142 | * ['currentIndex' => 'newIndex']. |
||
143 | * @param bool $keepUnmapped Whether or not to keep keys that are not |
||
144 | * remapped. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function reindex(array $array, array $map, $keepUnmapped = true) |
||
162 | |||
163 | /** |
||
164 | * Merges two arrays recursively. |
||
165 | * |
||
166 | * @param array $first Original data. |
||
167 | * @param array $second Data to be merged. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function merge(array $first, array $second) |
||
187 | |||
188 | /** |
||
189 | * Extend one array with another. |
||
190 | * |
||
191 | * @param array $arrays |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | public function extend(array $arrays) |
||
211 | |||
212 | /** |
||
213 | * Transforms a 1-dimensional array into a multi-dimensional one, |
||
214 | * exploding keys according to a separator. |
||
215 | * |
||
216 | * @param array $array |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function asHierarchy(array $array) |
||
242 | |||
243 | /** |
||
244 | * Separates elements from an array into groups. |
||
245 | * The function maps an element to the key that will be used for grouping. |
||
246 | * If no function is passed, the element itself will be used as key. |
||
247 | * |
||
248 | * @param array $array |
||
249 | * @param callable|null $callback |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | public function groupBy(array $array, callable $callback = null) |
||
275 | |||
276 | /** |
||
277 | * Flatten a multi-dimensional associative array with dots. |
||
278 | * |
||
279 | * @param array $array |
||
280 | * @param string $prepend |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | public function dot($array, $prepend = '') |
||
304 | |||
305 | /** |
||
306 | * Flatten a nested array to a separated key. |
||
307 | * |
||
308 | * @param array $array |
||
309 | * @param string|null $separator |
||
310 | * @param string $prepend |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | public function flatten(array $array, $separator = null, $prepend = '') |
||
328 | |||
329 | /** |
||
330 | * Expand a flattened array with dots to a multi-dimensional associative array. |
||
331 | * |
||
332 | * @param array $array |
||
333 | * @param string $prepend |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | public function expand(array $array, $prepend = '') |
||
359 | |||
360 | /** |
||
361 | * Reset all numerical indexes of an array (start from zero). |
||
362 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
363 | * |
||
364 | * @param array $array |
||
365 | * @param bool|false $deep |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public function reset(array $array, $deep = false) |
||
387 | |||
388 | /** |
||
389 | * Extend one array with another. Non associative arrays will not be merged |
||
390 | * but rather replaced. |
||
391 | * |
||
392 | * @param array $arrays |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | public function extendDistinct(array $arrays) |
||
416 | |||
417 | /** |
||
418 | * Recursively sort an array by keys and values. |
||
419 | * |
||
420 | * @param array $array |
||
421 | * |
||
422 | * @return array |
||
423 | */ |
||
424 | public function sortRecursive(array $array) |
||
442 | |||
443 | public function zip(array $array, array $arrays) |
||
462 | } |
||
463 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.