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 AbstractEnumSet 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 AbstractEnumSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class AbstractEnumSet implements Iterator, Countable |
||
17 | { |
||
18 | /** |
||
19 | * The classname of the Enumeration |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $enumeration; |
||
23 | |||
24 | /** |
||
25 | * Ordinal number of current iterator position |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $ordinal = 0; |
||
29 | |||
30 | /** |
||
31 | * Highest possible ordinal number |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $ordinalMax; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param string $enumeration The classname of the enumeration |
||
40 | * @throws InvalidArgumentException |
||
41 | */ |
||
42 | public function __construct($enumeration) |
||
55 | |||
56 | /** |
||
57 | * Get the classname of the enumeration |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getEnumeration() |
||
64 | |||
65 | /** |
||
66 | * Attach a new enumerator or overwrite an existing one |
||
67 | * @param Enum|null|boolean|int|float|string $enumerator |
||
68 | * @return void |
||
69 | * @throws InvalidArgumentException On an invalid given enumerator |
||
70 | */ |
||
71 | public function attach($enumerator) |
||
76 | |||
77 | /** |
||
78 | * Detach the given enumerator |
||
79 | * @param Enum|null|boolean|int|float|string $enumerator |
||
80 | * @return void |
||
81 | * @throws InvalidArgumentException On an invalid given enumerator |
||
82 | */ |
||
83 | public function detach($enumerator) |
||
88 | |||
89 | /** |
||
90 | * Test if the given enumerator was attached |
||
91 | * @param Enum|null|boolean|int|float|string $enumerator |
||
92 | * @return boolean |
||
93 | */ |
||
94 | public function contains($enumerator) |
||
99 | |||
100 | /* Iterator */ |
||
101 | |||
102 | /** |
||
103 | * Get the current enumerator |
||
104 | * @return Enum|null Returns the current enumerator or NULL on an invalid iterator position |
||
105 | */ |
||
106 | public function current() |
||
115 | |||
116 | /** |
||
117 | * Get the ordinal number of the current iterator position |
||
118 | * @return int |
||
119 | */ |
||
120 | public function key() |
||
124 | |||
125 | /** |
||
126 | * Go to the next valid iterator position. |
||
127 | * If no valid iterator position is found the iterator position will be the last possible + 1. |
||
128 | * @return void |
||
129 | */ |
||
130 | View Code Duplication | public function next() |
|
139 | |||
140 | /** |
||
141 | * Go to the first valid iterator position. |
||
142 | * If no valid iterator position in found the iterator position will be 0. |
||
143 | * @return void |
||
144 | */ |
||
145 | View Code Duplication | public function rewind() |
|
154 | |||
155 | /** |
||
156 | * Test if the iterator in a valid state |
||
157 | * @return boolean |
||
158 | */ |
||
159 | public function valid() |
||
163 | |||
164 | /* Countable */ |
||
165 | |||
166 | /** |
||
167 | * Count the number of elements |
||
168 | * @return int |
||
169 | */ |
||
170 | View Code Duplication | public function count() |
|
188 | |||
189 | /** |
||
190 | * Check if this EnumSet is the same as other |
||
191 | * @param EnumSet $other |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isEqual(EnumSet $other) |
||
199 | |||
200 | /** |
||
201 | * Check if this EnumSet is a subset of other |
||
202 | * @param EnumSet $other |
||
203 | * @return bool |
||
204 | */ |
||
205 | View Code Duplication | public function isSubset(EnumSet $other) |
|
213 | |||
214 | /** |
||
215 | * Check if this EnumSet is a superset of other |
||
216 | * @param EnumSet $other |
||
217 | * @return bool |
||
218 | */ |
||
219 | View Code Duplication | public function isSuperset(EnumSet $other) |
|
227 | |||
228 | /** |
||
229 | * Produce a new set with enumerators from both this and other (this | other) |
||
230 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
231 | * @return EnumSet |
||
232 | */ |
||
233 | View Code Duplication | public function union(EnumSet $other) |
|
252 | |||
253 | /** |
||
254 | * Produce a new set with enumerators common to both this and other (this & other) |
||
255 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
256 | * @return EnumSet |
||
257 | */ |
||
258 | View Code Duplication | public function intersect(EnumSet $other) |
|
277 | |||
278 | /** |
||
279 | * Produce a new set with enumerators in this but not in other (this - other) |
||
280 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
281 | * @return EnumSet |
||
282 | */ |
||
283 | View Code Duplication | public function diff(EnumSet $other) |
|
302 | |||
303 | /** |
||
304 | * Produce a new set with enumerators in either this and other but not in both (this ^ (other | other)) |
||
305 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
306 | * @return EnumSet |
||
307 | */ |
||
308 | View Code Duplication | public function symDiff(EnumSet $other) |
|
327 | |||
328 | /** |
||
329 | * Get ordinal numbers of the defined enumerators as array |
||
330 | * @return int[] |
||
331 | */ |
||
332 | View Code Duplication | public function getOrdinals() |
|
351 | |||
352 | /** |
||
353 | * Get values of the defined enumerators as array |
||
354 | * @return null[]|bool[]|int[]|float[]|string[] |
||
355 | */ |
||
356 | View Code Duplication | public function getValues() |
|
365 | |||
366 | /** |
||
367 | * Get names of the defined enumerators as array |
||
368 | * @return string[] |
||
369 | */ |
||
370 | View Code Duplication | public function getNames() |
|
379 | |||
380 | /** |
||
381 | * Get the defined enumerators as array |
||
382 | * @return Enum[] |
||
383 | */ |
||
384 | View Code Duplication | public function getEnumerators() |
|
393 | |||
394 | /** |
||
395 | * Get binary bitset in little-endian order |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getBinaryBitsetLe() |
||
403 | |||
404 | /** |
||
405 | * Set binary bitset in little-endian order |
||
406 | * |
||
407 | * NOTE: It resets the current position of the iterator |
||
408 | * |
||
409 | * @param string $bitset |
||
410 | * @return void |
||
411 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
412 | */ |
||
413 | View Code Duplication | public function setBinaryBitsetLe($bitset) |
|
449 | |||
450 | /** |
||
451 | * Get binary bitset in big-endian order |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | public function getBinaryBitsetBe() |
||
459 | |||
460 | /** |
||
461 | * Set binary bitset in big-endian order |
||
462 | * |
||
463 | * NOTE: It resets the current position of the iterator |
||
464 | * |
||
465 | * @param string $bitset |
||
466 | * @return void |
||
467 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
468 | */ |
||
469 | public function setBinaryBitsetBe($bitset) |
||
476 | |||
477 | /** |
||
478 | * Get a bit at the given ordinal number |
||
479 | * |
||
480 | * @param $ordinal int Ordinal number of bit to get |
||
481 | * @return boolean |
||
482 | */ |
||
483 | private function getBit($ordinal) |
||
487 | |||
488 | /** |
||
489 | * Set a bit at the given ordinal number |
||
490 | * |
||
491 | * @param $ordinal int Ordnal number of bit to set |
||
492 | * @return void |
||
493 | */ |
||
494 | private function setBit($ordinal) |
||
499 | |||
500 | /** |
||
501 | * Unset a bit at the given ordinal number |
||
502 | * |
||
503 | * @param $ordinal int Ordinal number of bit to unset |
||
504 | * @return void |
||
505 | */ |
||
506 | private function unsetBit($ordinal) |
||
511 | } |
||
512 |