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 | 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 | * 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 | 48 | public function __construct($enumeration) |
|
94 | |||
95 | /** |
||
96 | * Get the classname of the enumeration |
||
97 | * @return string |
||
98 | */ |
||
99 | 1 | public function getEnumeration() |
|
103 | |||
104 | /** |
||
105 | * Attach a new enumerator or overwrite an existing one |
||
106 | * @param Enum|null|boolean|int|float|string $enumerator |
||
107 | * @return void |
||
108 | * @throws InvalidArgumentException On an invalid given enumerator |
||
109 | */ |
||
110 | 32 | public function attach($enumerator) |
|
115 | |||
116 | /** |
||
117 | * Detach the given enumerator |
||
118 | * @param Enum|null|boolean|int|float|string $enumerator |
||
119 | * @return void |
||
120 | * @throws InvalidArgumentException On an invalid given enumerator |
||
121 | */ |
||
122 | 6 | public function detach($enumerator) |
|
127 | |||
128 | /** |
||
129 | * Test if the given enumerator was attached |
||
130 | * @param Enum|null|boolean|int|float|string $enumerator |
||
131 | * @return boolean |
||
132 | */ |
||
133 | 9 | public function contains($enumerator) |
|
138 | |||
139 | /* Iterator */ |
||
140 | |||
141 | /** |
||
142 | * Get the current enumerator |
||
143 | * @return Enum|null Returns the current enumerator or NULL on an invalid iterator position |
||
144 | */ |
||
145 | 9 | public function current() |
|
154 | |||
155 | /** |
||
156 | * Get the ordinal number of the current iterator position |
||
157 | * @return int |
||
158 | */ |
||
159 | 6 | public function key() |
|
163 | |||
164 | /** |
||
165 | * Go to the next valid iterator position. |
||
166 | * If no valid iterator position is found the iterator position will be the last possible + 1. |
||
167 | * @return void |
||
168 | */ |
||
169 | 15 | public function next() |
|
178 | |||
179 | /** |
||
180 | * Go to the first valid iterator position. |
||
181 | * If no valid iterator position was found the iterator position will be 0. |
||
182 | * @return void |
||
183 | * @see doRewindBin |
||
184 | * @see doRewindInt |
||
185 | */ |
||
186 | 9 | public function rewind() |
|
190 | |||
191 | /** |
||
192 | * Go to the first valid iterator position. |
||
193 | * If no valid iterator position was found the iterator position will be 0. |
||
194 | * |
||
195 | * This is the binary bitset implementation. |
||
196 | * |
||
197 | * @return void |
||
198 | * @see rewind |
||
199 | * @see doRewindInt |
||
200 | */ |
||
201 | 3 | View Code Duplication | private function doRewindBin() |
210 | |||
211 | /** |
||
212 | * Go to the first valid iterator position. |
||
213 | * If no valid iterator position was found the iterator position will be 0. |
||
214 | * |
||
215 | * This is the binary bitset implementation. |
||
216 | * |
||
217 | * @return void |
||
218 | * @see rewind |
||
219 | * @see doRewindBin |
||
220 | */ |
||
221 | 6 | private function doRewindInt() |
|
230 | |||
231 | /** |
||
232 | * Test if the iterator in a valid state |
||
233 | * @return boolean |
||
234 | */ |
||
235 | 10 | public function valid() |
|
239 | |||
240 | /* Countable */ |
||
241 | |||
242 | /** |
||
243 | * Count the number of elements |
||
244 | * |
||
245 | * @return int |
||
246 | * @see doCountBin |
||
247 | * @see doCountInt |
||
248 | */ |
||
249 | 9 | public function count() |
|
253 | |||
254 | /** |
||
255 | * Count the number of elements. |
||
256 | * |
||
257 | * This is the binary bitset implementation. |
||
258 | * |
||
259 | * @return int |
||
260 | * @see count |
||
261 | * @see doCountInt |
||
262 | */ |
||
263 | 4 | View Code Duplication | private function doCountBin() |
280 | |||
281 | /** |
||
282 | * Count the number of elements. |
||
283 | * |
||
284 | * This is the integer bitset implementation. |
||
285 | * |
||
286 | * @return int |
||
287 | * @see count |
||
288 | * @see doCountBin |
||
289 | */ |
||
290 | 5 | private function doCountInt() |
|
301 | |||
302 | /** |
||
303 | * Check if this EnumSet is the same as other |
||
304 | * @param EnumSet $other |
||
305 | * @return bool |
||
306 | */ |
||
307 | 3 | public function isEqual(EnumSet $other) |
|
312 | |||
313 | /** |
||
314 | * Check if this EnumSet is a subset of other |
||
315 | * @param EnumSet $other |
||
316 | * @return bool |
||
317 | */ |
||
318 | 4 | View Code Duplication | public function isSubset(EnumSet $other) |
326 | |||
327 | /** |
||
328 | * Check if this EnumSet is a superset of other |
||
329 | * @param EnumSet $other |
||
330 | * @return bool |
||
331 | */ |
||
332 | 4 | View Code Duplication | public function isSuperset(EnumSet $other) |
340 | |||
341 | /** |
||
342 | * Produce a new set with enumerators from both this and other (this | other) |
||
343 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
344 | * @return EnumSet |
||
345 | */ |
||
346 | 2 | View Code Duplication | public function union(EnumSet $other) |
365 | |||
366 | /** |
||
367 | * Produce a new set with enumerators common to both this and other (this & other) |
||
368 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
369 | * @return EnumSet |
||
370 | */ |
||
371 | 2 | View Code Duplication | public function intersect(EnumSet $other) |
390 | |||
391 | /** |
||
392 | * Produce a new set with enumerators in this but not in other (this - other) |
||
393 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
394 | * @return EnumSet |
||
395 | */ |
||
396 | 1 | View Code Duplication | public function diff(EnumSet $other) |
415 | |||
416 | /** |
||
417 | * Produce a new set with enumerators in either this and other but not in both (this ^ (other | other)) |
||
418 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
419 | * @return EnumSet |
||
420 | */ |
||
421 | 1 | View Code Duplication | public function symDiff(EnumSet $other) |
440 | |||
441 | /** |
||
442 | * Get ordinal numbers of the defined enumerators as array |
||
443 | * @return int[] |
||
444 | */ |
||
445 | 10 | public function getOrdinals() |
|
449 | |||
450 | /** |
||
451 | * Get ordinal numbers of the defined enumerators as array. |
||
452 | * |
||
453 | * This is the binary bitset implementation. |
||
454 | * |
||
455 | * @return int[] |
||
456 | * @see getOrdinals |
||
457 | * @see goGetOrdinalsInt |
||
458 | */ |
||
459 | View Code Duplication | private function doGetOrdinalsBin() |
|
476 | |||
477 | /** |
||
478 | * Get ordinal numbers of the defined enumerators as array. |
||
479 | * |
||
480 | * This is the integer bitset implementation. |
||
481 | * |
||
482 | * @return int[] |
||
483 | * @see getOrdinals |
||
484 | * @see doGetOrdinalsBin |
||
485 | */ |
||
486 | 10 | private function doGetOrdinalsInt() |
|
496 | |||
497 | /** |
||
498 | * Get values of the defined enumerators as array |
||
499 | * @return null[]|bool[]|int[]|float[]|string[] |
||
500 | */ |
||
501 | 4 | View Code Duplication | public function getValues() |
510 | |||
511 | /** |
||
512 | * Get names of the defined enumerators as array |
||
513 | * @return string[] |
||
514 | */ |
||
515 | 2 | View Code Duplication | public function getNames() |
524 | |||
525 | /** |
||
526 | * Get the defined enumerators as array |
||
527 | * @return Enum[] |
||
528 | */ |
||
529 | 2 | View Code Duplication | public function getEnumerators() |
538 | |||
539 | /** |
||
540 | * Get binary bitset in little-endian order |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | 4 | public function getBinaryBitsetLe() |
|
548 | |||
549 | /** |
||
550 | * Get binary bitset in little-endian order. |
||
551 | * |
||
552 | * This is the binary bitset implementation. |
||
553 | * |
||
554 | * @return string |
||
555 | */ |
||
556 | 3 | private function doGetBinaryBitsetLeBin() |
|
560 | |||
561 | /** |
||
562 | * Get binary bitset in little-endian order. |
||
563 | * |
||
564 | * This is the integer bitset implementation. |
||
565 | * |
||
566 | * @return string |
||
567 | */ |
||
568 | 1 | private function doGetBinaryBitsetLeInt() |
|
573 | |||
574 | /** |
||
575 | * Set binary bitset in little-endian order |
||
576 | * |
||
577 | * NOTE: It resets the current position of the iterator |
||
578 | * |
||
579 | * @param string $bitset |
||
580 | * @return void |
||
581 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
582 | */ |
||
583 | 8 | public function setBinaryBitsetLe($bitset) |
|
594 | |||
595 | /** |
||
596 | * Set binary bitset in little-endian order |
||
597 | * |
||
598 | * NOTE: It resets the current position of the iterator |
||
599 | * |
||
600 | * @param string $bitset |
||
601 | * @return void |
||
602 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
603 | */ |
||
604 | 4 | private function doSetBinaryBitsetLeBin($bitset) |
|
633 | |||
634 | /** |
||
635 | * Set binary bitset in little-endian order |
||
636 | * |
||
637 | * NOTE: It resets the current position of the iterator |
||
638 | * |
||
639 | * @param string $bitset |
||
640 | * @return void |
||
641 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
642 | */ |
||
643 | 3 | private function doSetBinaryBitsetLeInt($bitset) |
|
663 | |||
664 | /** |
||
665 | * Get binary bitset in big-endian order |
||
666 | * |
||
667 | * @return string |
||
668 | */ |
||
669 | 1 | public function getBinaryBitsetBe() |
|
673 | |||
674 | /** |
||
675 | * Set binary bitset in big-endian order |
||
676 | * |
||
677 | * NOTE: It resets the current position of the iterator |
||
678 | * |
||
679 | * @param string $bitset |
||
680 | * @return void |
||
681 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
682 | */ |
||
683 | 2 | public function setBinaryBitsetBe($bitset) |
|
690 | |||
691 | /** |
||
692 | * Get a bit at the given ordinal number |
||
693 | * |
||
694 | * @param int $ordinal Ordinal number of bit to get |
||
695 | * @return boolean |
||
696 | */ |
||
697 | public function getBit($ordinal) |
||
705 | |||
706 | /** |
||
707 | * Get a bit at the given ordinal number. |
||
708 | * |
||
709 | * This is the binary bitset implementation. |
||
710 | * |
||
711 | * @param int $ordinal Ordinal number of bit to get |
||
712 | * @return boolean |
||
713 | * @see getBit |
||
714 | * @see doGetBitInt |
||
715 | */ |
||
716 | 6 | private function doGetBitBin($ordinal) |
|
720 | |||
721 | /** |
||
722 | * Get a bit at the given ordinal number. |
||
723 | * |
||
724 | * This is the integer bitset implementation. |
||
725 | * |
||
726 | * @param int $ordinal Ordinal number of bit to get |
||
727 | * @return boolean |
||
728 | * @see getBit |
||
729 | * @see doGetBitBin |
||
730 | */ |
||
731 | 13 | private function doGetBitInt($ordinal) |
|
735 | |||
736 | /** |
||
737 | * Set a bit at the given ordinal number |
||
738 | * |
||
739 | * @param int $ordinal Ordnal number of bit to set |
||
740 | * @param bool $bit The bit to set |
||
741 | * @return void |
||
742 | * @see doSetBitBin |
||
743 | * @see doSetBitInt |
||
744 | * @see doUnsetBin |
||
745 | * @see doUnsetInt |
||
746 | */ |
||
747 | public function setBit($ordinal, $bit) |
||
759 | |||
760 | /** |
||
761 | * Set a bit at the given ordinal number. |
||
762 | * |
||
763 | * This is the binary bitset implementation. |
||
764 | * |
||
765 | * @param int $ordinal Ordnal number of bit to set |
||
766 | * @return void |
||
767 | * @see setBit |
||
768 | * @see doSetBitInt |
||
769 | */ |
||
770 | 3 | private function doSetBitBin($ordinal) |
|
775 | |||
776 | /** |
||
777 | * Set a bit at the given ordinal number. |
||
778 | * |
||
779 | * This is the binary bitset implementation. |
||
780 | * |
||
781 | * @param int $ordinal Ordnal number of bit to set |
||
782 | * @return void |
||
783 | * @see setBit |
||
784 | * @see doSetBitBin |
||
785 | */ |
||
786 | 29 | private function doSetBitInt($ordinal) |
|
790 | |||
791 | /** |
||
792 | * Unset a bit at the given ordinal number. |
||
793 | * |
||
794 | * This is the binary bitset implementation. |
||
795 | * |
||
796 | * @param int $ordinal Ordinal number of bit to unset |
||
797 | * @return void |
||
798 | * @see setBit |
||
799 | * @see doUnsetBitInt |
||
800 | */ |
||
801 | 2 | private function doUnsetBitBin($ordinal) |
|
806 | |||
807 | /** |
||
808 | * Unset a bit at the given ordinal number. |
||
809 | * |
||
810 | * This is the integer bitset implementation. |
||
811 | * |
||
812 | * @param int $ordinal Ordinal number of bit to unset |
||
813 | * @return void |
||
814 | * @see setBit |
||
815 | * @see doUnsetBitBin |
||
816 | */ |
||
817 | 4 | private function doUnsetBitInt($ordinal) |
|
821 | } |
||
822 |