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 EnumSet66Bench 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 EnumSet66Bench, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | View Code Duplication | class EnumSet66Bench |
|
|
|||
15 | { |
||
16 | private static $constants; |
||
17 | private static $names; |
||
18 | private static $values; |
||
19 | private static $ordinals; |
||
20 | private static $enumerators; |
||
21 | |||
22 | private $emptySet; |
||
23 | private $fullSet; |
||
24 | |||
25 | public static function initClass() |
||
35 | |||
36 | public function init() |
||
44 | |||
45 | public function benchAttachEnumeratorOnEmpty() |
||
51 | |||
52 | public function benchAttachValueOnEmpty() |
||
58 | |||
59 | public function benchAttachEnumeratorOnFull() |
||
65 | |||
66 | public function benchAttachValueOnFull() |
||
72 | |||
73 | public function benchDetachEnumeratorOnEmpty() |
||
79 | |||
80 | public function benchDetachValueOnEmpty() |
||
86 | |||
87 | public function benchDetachEnumeratorOnFull() |
||
93 | |||
94 | public function benchDetachValueOnFull() |
||
100 | |||
101 | public function benchContainsEnumeratorTrue() |
||
107 | |||
108 | public function benchContainsValueTrue() |
||
114 | |||
115 | public function benchContainsEnumeratorFalse() |
||
121 | |||
122 | public function benchContainsValueFalse() |
||
128 | |||
129 | public function benchIterateFull() |
||
135 | |||
136 | public function benchIterateEmpty() |
||
142 | |||
143 | public function benchCountFull() |
||
147 | |||
148 | public function benchCountEmpty() |
||
152 | |||
153 | public function benchIsEqual() |
||
157 | |||
158 | public function benchIsSubset() |
||
162 | |||
163 | public function benchIsSuperset() |
||
167 | |||
168 | public function benchUnion() |
||
172 | |||
173 | public function benchIntersect() |
||
177 | |||
178 | public function benchDiff() |
||
182 | |||
183 | public function benchSymDiff() |
||
187 | |||
188 | public function benchGetOrdinals() |
||
192 | |||
193 | public function benchGetValues() |
||
197 | |||
198 | public function benchGetNames() |
||
202 | |||
203 | public function benchGetEnumerators() |
||
207 | |||
208 | public function benchGetBit() |
||
214 | |||
215 | public function benchSetBit() |
||
221 | |||
222 | public function benchUnsetBit() |
||
228 | } |
||
229 |
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.