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 EnumSet 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 EnumSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EnumSet implements Iterator, Countable |
||
17 | { |
||
18 | /** |
||
19 | * The classname of the Enumeration |
||
20 | * @var string |
||
21 | */ |
||
22 | private $enumeration; |
||
23 | |||
24 | /** |
||
25 | * Ordinal number of current iterator position |
||
26 | * @var int |
||
27 | */ |
||
28 | private $ordinal = 0; |
||
29 | |||
30 | /** |
||
31 | * Highest possible ordinal number |
||
32 | * @var int |
||
33 | */ |
||
34 | private $ordinalMax; |
||
35 | |||
36 | /** |
||
37 | * Integer or binary (little endian) bitset used to handle attached enumerations. |
||
38 | * @var int|string |
||
39 | */ |
||
40 | private $bitset; |
||
41 | |||
42 | /**#@+ |
||
43 | * Defined the private method names to be called dependend of |
||
44 | * how the bitset type was set too. |
||
45 | * ... Integer or binary bitset. |
||
46 | * ... *Int or *Bin method |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $fnDoRewind = 'doRewindBin'; |
||
51 | private $fnDoCount = 'doCountBin'; |
||
52 | private $fnDoGetOrdinals = 'doGetOrdinalsBin'; |
||
53 | private $fnDoGetBit = 'doGetBitBin'; |
||
54 | private $fnDoSetBit = 'doSetBitBin'; |
||
55 | private $fnDoUnsetBit = 'doUnsetBitBin'; |
||
56 | private $fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeBin'; |
||
57 | private $fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeBin'; |
||
58 | /**#@-*/ |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param string $enumeration The classname of the enumeration |
||
64 | * @throws InvalidArgumentException |
||
65 | */ |
||
66 | 49 | public function __construct($enumeration) |
|
95 | |||
96 | /** |
||
97 | * Get the classname of the enumeration |
||
98 | * @return string |
||
99 | */ |
||
100 | 1 | public function getEnumeration() |
|
104 | |||
105 | /** |
||
106 | * Attach a new enumerator or overwrite an existing one |
||
107 | * @param Enum|null|boolean|int|float|string $enumerator |
||
108 | * @return void |
||
109 | * @throws InvalidArgumentException On an invalid given enumerator |
||
110 | */ |
||
111 | 34 | public function attach($enumerator) |
|
116 | |||
117 | /** |
||
118 | * Detach the given enumerator |
||
119 | * @param Enum|null|boolean|int|float|string $enumerator |
||
120 | * @return void |
||
121 | * @throws InvalidArgumentException On an invalid given enumerator |
||
122 | */ |
||
123 | 6 | public function detach($enumerator) |
|
128 | |||
129 | /** |
||
130 | * Test if the given enumerator was attached |
||
131 | * @param Enum|null|boolean|int|float|string $enumerator |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 9 | public function contains($enumerator) |
|
139 | |||
140 | /* Iterator */ |
||
141 | |||
142 | /** |
||
143 | * Get the current enumerator |
||
144 | * @return Enum|null Returns the current enumerator or NULL on an invalid iterator position |
||
145 | */ |
||
146 | 9 | public function current() |
|
155 | |||
156 | /** |
||
157 | * Get the ordinal number of the current iterator position |
||
158 | * @return int |
||
159 | */ |
||
160 | 6 | public function key() |
|
164 | |||
165 | /** |
||
166 | * Go to the next valid iterator position. |
||
167 | * If no valid iterator position is found the iterator position will be the last possible + 1. |
||
168 | * @return void |
||
169 | */ |
||
170 | 14 | public function next() |
|
179 | |||
180 | /** |
||
181 | * Go to the first valid iterator position. |
||
182 | * If no valid iterator position was found the iterator position will be 0. |
||
183 | * @return void |
||
184 | * @see doRewindBin |
||
185 | * @see doRewindInt |
||
186 | */ |
||
187 | 8 | public function rewind() |
|
191 | |||
192 | /** |
||
193 | * Go to the first valid iterator position. |
||
194 | * If no valid iterator position was found the iterator position will be 0. |
||
195 | * |
||
196 | * This is the binary bitset implementation. |
||
197 | * |
||
198 | * @return void |
||
199 | * @see rewind |
||
200 | * @see doRewindInt |
||
201 | */ |
||
202 | 3 | private function doRewindBin() |
|
211 | |||
212 | /** |
||
213 | * Go to the first valid iterator position. |
||
214 | * If no valid iterator position was found the iterator position will be 0. |
||
215 | * |
||
216 | * This is the binary bitset implementation. |
||
217 | * |
||
218 | * @return void |
||
219 | * @see rewind |
||
220 | * @see doRewindBin |
||
221 | */ |
||
222 | 5 | private function doRewindInt() |
|
231 | |||
232 | /** |
||
233 | * Test if the iterator in a valid state |
||
234 | * @return boolean |
||
235 | */ |
||
236 | 10 | public function valid() |
|
240 | |||
241 | /* Countable */ |
||
242 | |||
243 | /** |
||
244 | * Count the number of elements |
||
245 | * |
||
246 | * @return int |
||
247 | * @see doCountBin |
||
248 | * @see doCountInt |
||
249 | */ |
||
250 | 9 | public function count() |
|
254 | |||
255 | /** |
||
256 | * Count the number of elements. |
||
257 | * |
||
258 | * This is the binary bitset implementation. |
||
259 | * |
||
260 | * @return int |
||
261 | * @see count |
||
262 | * @see doCountInt |
||
263 | */ |
||
264 | 4 | private function doCountBin() |
|
283 | |||
284 | /** |
||
285 | * Count the number of elements. |
||
286 | * |
||
287 | * This is the integer bitset implementation. |
||
288 | * |
||
289 | * @return int |
||
290 | * @see count |
||
291 | * @see doCountBin |
||
292 | */ |
||
293 | 5 | private function doCountInt() |
|
304 | |||
305 | /** |
||
306 | * Check if this EnumSet is the same as other |
||
307 | * @param EnumSet $other |
||
308 | * @return bool |
||
309 | */ |
||
310 | 3 | public function isEqual(EnumSet $other) |
|
315 | |||
316 | /** |
||
317 | * Check if this EnumSet is a subset of other |
||
318 | * @param EnumSet $other |
||
319 | * @return bool |
||
320 | */ |
||
321 | 4 | View Code Duplication | public function isSubset(EnumSet $other) |
329 | |||
330 | /** |
||
331 | * Check if this EnumSet is a superset of other |
||
332 | * @param EnumSet $other |
||
333 | * @return bool |
||
334 | */ |
||
335 | 4 | View Code Duplication | public function isSuperset(EnumSet $other) |
343 | |||
344 | /** |
||
345 | * Produce a new set with enumerators from both this and other (this | other) |
||
346 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
347 | * @return EnumSet |
||
348 | */ |
||
349 | 2 | View Code Duplication | public function union(EnumSet $other) |
368 | |||
369 | /** |
||
370 | * Produce a new set with enumerators common to both this and other (this & other) |
||
371 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
372 | * @return EnumSet |
||
373 | */ |
||
374 | 2 | View Code Duplication | public function intersect(EnumSet $other) |
393 | |||
394 | /** |
||
395 | * Produce a new set with enumerators in this but not in other (this - other) |
||
396 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
397 | * @return EnumSet |
||
398 | */ |
||
399 | 2 | View Code Duplication | public function diff(EnumSet $other) |
418 | |||
419 | /** |
||
420 | * Produce a new set with enumerators in either this and other but not in both (this ^ (other | other)) |
||
421 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
422 | * @return EnumSet |
||
423 | */ |
||
424 | 2 | View Code Duplication | public function symDiff(EnumSet $other) |
443 | |||
444 | /** |
||
445 | * Get ordinal numbers of the defined enumerators as array |
||
446 | * @return int[] |
||
447 | */ |
||
448 | 12 | public function getOrdinals() |
|
452 | |||
453 | /** |
||
454 | * Get ordinal numbers of the defined enumerators as array. |
||
455 | * |
||
456 | * This is the binary bitset implementation. |
||
457 | * |
||
458 | * @return int[] |
||
459 | * @see getOrdinals |
||
460 | * @see goGetOrdinalsInt |
||
461 | */ |
||
462 | private function doGetOrdinalsBin() |
||
481 | |||
482 | /** |
||
483 | * Get ordinal numbers of the defined enumerators as array. |
||
484 | * |
||
485 | * This is the integer bitset implementation. |
||
486 | * |
||
487 | * @return int[] |
||
488 | * @see getOrdinals |
||
489 | * @see doGetOrdinalsBin |
||
490 | */ |
||
491 | 12 | private function doGetOrdinalsInt() |
|
501 | |||
502 | /** |
||
503 | * Get values of the defined enumerators as array |
||
504 | * @return null[]|bool[]|int[]|float[]|string[] |
||
505 | */ |
||
506 | 6 | public function getValues() |
|
515 | |||
516 | /** |
||
517 | * Get names of the defined enumerators as array |
||
518 | * @return string[] |
||
519 | */ |
||
520 | 2 | public function getNames() |
|
529 | |||
530 | /** |
||
531 | * Get the defined enumerators as array |
||
532 | * @return Enum[] |
||
533 | */ |
||
534 | 2 | public function getEnumerators() |
|
543 | |||
544 | /** |
||
545 | * Get binary bitset in little-endian order |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | 3 | public function getBinaryBitsetLe() |
|
553 | |||
554 | /** |
||
555 | * Get binary bitset in little-endian order. |
||
556 | * |
||
557 | * This is the binary bitset implementation. |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | 3 | private function doGetBinaryBitsetLeBin() |
|
565 | |||
566 | /** |
||
567 | * Get binary bitset in little-endian order. |
||
568 | * |
||
569 | * This is the integer bitset implementation. |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | private function doGetBinaryBitsetLeInt() |
||
578 | |||
579 | /** |
||
580 | * Set binary bitset in little-endian order |
||
581 | * |
||
582 | * NOTE: It resets the current position of the iterator |
||
583 | * |
||
584 | * @param string $bitset |
||
585 | * @return void |
||
586 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
587 | */ |
||
588 | 7 | public function setBinaryBitsetLe($bitset) |
|
599 | |||
600 | /** |
||
601 | * Set binary bitset in little-endian order |
||
602 | * |
||
603 | * NOTE: It resets the current position of the iterator |
||
604 | * |
||
605 | * @param string $bitset |
||
606 | * @return void |
||
607 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
608 | */ |
||
609 | 4 | private function doSetBinaryBitsetLeBin($bitset) |
|
638 | |||
639 | /** |
||
640 | * Set binary bitset in little-endian order |
||
641 | * |
||
642 | * NOTE: It resets the current position of the iterator |
||
643 | * |
||
644 | * @param string $bitset |
||
645 | * @return void |
||
646 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
647 | */ |
||
648 | 2 | private function doSetBinaryBitsetLeInt($bitset) |
|
668 | |||
669 | /** |
||
670 | * Get binary bitset in big-endian order |
||
671 | * |
||
672 | * @return string |
||
673 | */ |
||
674 | 1 | public function getBinaryBitsetBe() |
|
678 | |||
679 | /** |
||
680 | * Set binary bitset in big-endian order |
||
681 | * |
||
682 | * NOTE: It resets the current position of the iterator |
||
683 | * |
||
684 | * @param string $bitset |
||
685 | * @return void |
||
686 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
687 | */ |
||
688 | 2 | public function setBinaryBitsetBe($bitset) |
|
695 | |||
696 | /** |
||
697 | * Get a bit at the given ordinal number |
||
698 | * |
||
699 | * @param int $ordinal Ordinal number of bit to get |
||
700 | * @return boolean |
||
701 | */ |
||
702 | public function getBit($ordinal) |
||
710 | |||
711 | /** |
||
712 | * Get a bit at the given ordinal number. |
||
713 | * |
||
714 | * This is the binary bitset implementation. |
||
715 | * |
||
716 | * @param int $ordinal Ordinal number of bit to get |
||
717 | * @return boolean |
||
718 | * @see getBit |
||
719 | * @see doGetBitInt |
||
720 | */ |
||
721 | 6 | private function doGetBitBin($ordinal) |
|
725 | |||
726 | /** |
||
727 | * Get a bit at the given ordinal number. |
||
728 | * |
||
729 | * This is the integer bitset implementation. |
||
730 | * |
||
731 | * @param int $ordinal Ordinal number of bit to get |
||
732 | * @return boolean |
||
733 | * @see getBit |
||
734 | * @see doGetBitBin |
||
735 | */ |
||
736 | 12 | private function doGetBitInt($ordinal) |
|
740 | |||
741 | /** |
||
742 | * Set a bit at the given ordinal number |
||
743 | * |
||
744 | * @param int $ordinal Ordnal number of bit to set |
||
745 | * @param bool $bit The bit to set |
||
746 | * @return void |
||
747 | * @see doSetBitBin |
||
748 | * @see doSetBitInt |
||
749 | * @see doUnsetBin |
||
750 | * @see doUnsetInt |
||
751 | */ |
||
752 | public function setBit($ordinal, $bit) |
||
764 | |||
765 | /** |
||
766 | * Set a bit at the given ordinal number. |
||
767 | * |
||
768 | * This is the binary bitset implementation. |
||
769 | * |
||
770 | * @param int $ordinal Ordnal number of bit to set |
||
771 | * @return void |
||
772 | * @see setBit |
||
773 | * @see doSetBitInt |
||
774 | */ |
||
775 | 3 | private function doSetBitBin($ordinal) |
|
780 | |||
781 | /** |
||
782 | * Set a bit at the given ordinal number. |
||
783 | * |
||
784 | * This is the binary bitset implementation. |
||
785 | * |
||
786 | * @param int $ordinal Ordnal number of bit to set |
||
787 | * @return void |
||
788 | * @see setBit |
||
789 | * @see doSetBitBin |
||
790 | */ |
||
791 | 31 | private function doSetBitInt($ordinal) |
|
795 | |||
796 | /** |
||
797 | * Unset a bit at the given ordinal number. |
||
798 | * |
||
799 | * This is the binary bitset implementation. |
||
800 | * |
||
801 | * @param int $ordinal Ordinal number of bit to unset |
||
802 | * @return void |
||
803 | * @see setBit |
||
804 | * @see doUnsetBitInt |
||
805 | */ |
||
806 | 2 | private function doUnsetBitBin($ordinal) |
|
811 | |||
812 | /** |
||
813 | * Unset a bit at the given ordinal number. |
||
814 | * |
||
815 | * This is the integer bitset implementation. |
||
816 | * |
||
817 | * @param int $ordinal Ordinal number of bit to unset |
||
818 | * @return void |
||
819 | * @see setBit |
||
820 | * @see doUnsetBitBin |
||
821 | */ |
||
822 | 4 | private function doUnsetBitInt($ordinal) |
|
826 | } |
||
827 |