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 CommonMutableContainerTrait 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 CommonMutableContainerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait CommonMutableContainerTrait |
||
11 | { |
||
12 | /** |
||
13 | * {@inheritdoc} |
||
14 | */ |
||
15 | 1 | public function values() |
|
19 | |||
20 | /** |
||
21 | * @return array |
||
22 | */ |
||
23 | 2 | View Code Duplication | public function toValuesArray() |
|
|||
24 | { |
||
25 | 1 | $arr = []; |
|
26 | 1 | foreach ($this as $value) { |
|
27 | 1 | if ($value instanceof Iterable) { |
|
28 | $arr[] = $value->toArray(); |
||
29 | } else { |
||
30 | 1 | $arr[] = $value; |
|
31 | } |
||
32 | 1 | } |
|
33 | |||
34 | 2 | return $arr; |
|
35 | } |
||
36 | |||
37 | 3 | public function toKeysArray() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 17 | View Code Duplication | public function toArray() |
51 | { |
||
52 | 17 | $arr = []; |
|
53 | 17 | foreach ($this as $key => $value) { |
|
54 | 16 | if ($value instanceof Iterable) { |
|
55 | 6 | $arr[$key] = $value->toArray(); |
|
56 | 6 | } else { |
|
57 | 16 | $arr[$key] = $value; |
|
58 | 1 | } |
|
59 | 17 | } |
|
60 | |||
61 | 17 | return $arr; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 1 | public static function fromArray(array $arr) |
|
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | * @return $this |
||
84 | */ |
||
85 | 2 | public function groupBy($callback) |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | * @return $this |
||
105 | */ |
||
106 | 2 | public function indexBy($callback) |
|
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | * @return $this |
||
120 | */ |
||
121 | View Code Duplication | public function concat($iterable) |
|
131 | |||
132 | /** |
||
133 | * {@inheritDoc} |
||
134 | * @return $this |
||
135 | */ |
||
136 | View Code Duplication | public function replace($iterable) |
|
146 | |||
147 | private function concatRecurse($array, $array1) |
||
171 | |||
172 | private function replaceRecurse($array, $array1) |
||
189 | } |
||
190 |
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.