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 |
||
16 | class EnumSet66Bench |
||
17 | { |
||
18 | /** |
||
19 | * @var mixed[] |
||
20 | */ |
||
21 | private $values; |
||
22 | |||
23 | /** |
||
24 | * @var Enum66[] |
||
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 | public function init() |
||
42 | { |
||
43 | $this->values = Enum66::getValues(); |
||
44 | $this->enumerators = Enum66::getEnumerators(); |
||
45 | |||
46 | $this->emptySet = new EnumSet(Enum66::class); |
||
47 | $this->fullSet = new EnumSet(Enum66::class); |
||
48 | foreach ($this->enumerators as $enumerator) { |
||
49 | $this->fullSet->attach($enumerator); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public function benchAttachEnumeratorOnEmpty() |
||
54 | { |
||
55 | foreach ($this->enumerators as $enumerator) { |
||
56 | $this->emptySet->attach($enumerator); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public function benchAttachValueOnEmpty() |
||
61 | { |
||
62 | foreach ($this->values as $value) { |
||
63 | $this->emptySet->attach($value); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function benchAttachEnumeratorOnFull() |
||
68 | { |
||
69 | foreach ($this->enumerators as $enumerator) { |
||
70 | $this->fullSet->attach($enumerator); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | public function benchAttachValueOnFull() |
||
75 | { |
||
76 | foreach ($this->values as $value) { |
||
77 | $this->fullSet->attach($value); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | public function benchDetachEnumeratorOnEmpty() |
||
82 | { |
||
83 | foreach ($this->enumerators as $enumerator) { |
||
84 | $this->emptySet->detach($enumerator); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public function benchDetachValueOnEmpty() |
||
89 | { |
||
90 | foreach ($this->values as $value) { |
||
91 | $this->emptySet->detach($value); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | public function benchDetachEnumeratorOnFull() |
||
96 | { |
||
97 | foreach ($this->enumerators as $enumerator) { |
||
98 | $this->fullSet->detach($enumerator); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | public function benchDetachValueOnFull() |
||
103 | { |
||
104 | foreach ($this->values as $value) { |
||
105 | $this->fullSet->detach($value); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | public function benchContainsEnumeratorTrue() |
||
110 | { |
||
111 | foreach ($this->enumerators as $enumerator) { |
||
112 | $this->fullSet->contains($enumerator); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | public function benchContainsValueTrue() |
||
117 | { |
||
118 | foreach ($this->values as $value) { |
||
119 | $this->fullSet->contains($value); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public function benchContainsEnumeratorFalse() |
||
124 | { |
||
125 | foreach ($this->enumerators as $enumerator) { |
||
126 | $this->fullSet->contains($enumerator); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function benchContainsValueFalse() |
||
131 | { |
||
132 | foreach ($this->values as $value) { |
||
133 | $this->fullSet->contains($value); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function benchIterateFull() |
||
138 | { |
||
139 | foreach ($this->fullSet as $enumerator) { |
||
140 | $enumerator->getValue(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | public function benchIterateEmpty() |
||
145 | { |
||
146 | foreach ($this->emptySet as $enumerator) { |
||
147 | $enumerator->getValue(); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | public function benchCountFull() |
||
152 | { |
||
153 | $this->fullSet->count(); |
||
154 | } |
||
155 | |||
156 | public function benchCountEmpty() |
||
157 | { |
||
158 | $this->emptySet->count(); |
||
159 | } |
||
160 | |||
161 | public function benchIsEqual() |
||
162 | { |
||
163 | $this->fullSet->isEqual($this->fullSet); |
||
164 | } |
||
165 | |||
166 | public function benchIsSubset() |
||
167 | { |
||
168 | $this->fullSet->isEqual($this->fullSet); |
||
169 | } |
||
170 | |||
171 | public function benchIsSuperset() |
||
172 | { |
||
173 | $this->fullSet->isSuperset($this->fullSet); |
||
174 | } |
||
175 | |||
176 | public function benchUnion() |
||
177 | { |
||
178 | $this->fullSet->union($this->emptySet); |
||
179 | } |
||
180 | |||
181 | public function benchIntersect() |
||
182 | { |
||
183 | $this->fullSet->intersect($this->emptySet); |
||
184 | } |
||
185 | |||
186 | public function benchDiff() |
||
187 | { |
||
188 | $this->fullSet->diff($this->emptySet); |
||
189 | } |
||
190 | |||
191 | public function benchSymDiff() |
||
192 | { |
||
193 | $this->fullSet->symDiff($this->emptySet); |
||
194 | } |
||
195 | |||
196 | public function benchGetOrdinals() |
||
197 | { |
||
198 | $this->fullSet->getOrdinals(); |
||
199 | } |
||
200 | |||
201 | public function benchGetValues() |
||
202 | { |
||
203 | $this->fullSet->getValues(); |
||
204 | } |
||
205 | |||
206 | public function benchGetNames() |
||
207 | { |
||
208 | $this->fullSet->getNames(); |
||
209 | } |
||
210 | |||
211 | public function benchGetEnumerators() |
||
212 | { |
||
213 | $this->fullSet->getEnumerators(); |
||
214 | } |
||
215 | } |
||
216 |