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:
1 | <?php |
||
16 | class EnumSet32Bench |
||
17 | { |
||
18 | /** |
||
19 | * @var mixed[] |
||
20 | */ |
||
21 | private $values; |
||
22 | |||
23 | /** |
||
24 | * @var Enum32[] |
||
25 | */ |
||
26 | private $enumerators; |
||
27 | |||
28 | /** |
||
29 | * @var EnumSet |
||
30 | */ |
||
31 | private $emptySet; |
||
32 | |||
33 | /** |
||
34 | * @var EnumSet |
||
35 | */ |
||
36 | private $fullSet; |
||
37 | |||
38 | /** |
||
39 | * Will be called before every subject |
||
40 | */ |
||
41 | View Code Duplication | public function init() |
|
|
|||
42 | { |
||
43 | $this->values = Enum32::getValues(); |
||
44 | $this->enumerators = Enum32::getEnumerators(); |
||
45 | |||
46 | $this->emptySet = new EnumSet(Enum32::class); |
||
47 | $this->fullSet = new EnumSet(Enum32::class); |
||
48 | foreach ($this->enumerators as $enumerator) { |
||
49 | $this->fullSet->attach($enumerator); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public function benchAttachEnumerator() |
||
59 | |||
60 | public function benchAttachValue() |
||
66 | |||
67 | public function benchDetachEnumerator() |
||
73 | |||
74 | public function benchDetachValue() |
||
80 | |||
81 | public function benchContainsEnumeratorTrue() |
||
87 | |||
88 | public function benchContainsEnumeratorFalse() |
||
94 | |||
95 | public function benchContainsValueTrue() |
||
101 | |||
102 | public function benchContainsValueFalse() |
||
108 | |||
109 | public function benchIterateFull() |
||
115 | |||
116 | public function benchIterateEmpty() |
||
122 | |||
123 | public function benchCountFull() |
||
127 | |||
128 | public function benchCountEmpty() |
||
132 | |||
133 | public function benchIsEqual() |
||
137 | |||
138 | public function benchIsSubset() |
||
142 | |||
143 | public function benchIsSuperset() |
||
147 | |||
148 | public function benchUnion() |
||
152 | |||
153 | public function benchIntersect() |
||
157 | |||
158 | public function benchDiff() |
||
162 | |||
163 | public function benchSymDiff() |
||
167 | |||
168 | public function benchGetOrdinals() |
||
172 | |||
173 | public function benchGetValues() |
||
177 | |||
178 | public function benchGetNames() |
||
182 | |||
183 | public function benchGetEnumerators() |
||
187 | } |
||
188 |
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.