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 EnumSetTest 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 EnumSetTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class EnumSetTest extends TestCase |
||
22 | { |
||
23 | public function testBasic() |
||
24 | { |
||
25 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
26 | $this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumSet->getEnumeration()); |
||
27 | |||
28 | $enum1 = EnumBasic::ONE(); |
||
29 | $enum2 = EnumBasic::TWO(); |
||
30 | |||
31 | $this->assertFalse($enumSet->contains($enum1)); |
||
32 | $this->assertNull($enumSet->attach($enum1)); |
||
33 | $this->assertTrue($enumSet->contains($enum1)); |
||
34 | |||
35 | $this->assertFalse($enumSet->contains($enum2)); |
||
36 | $this->assertNull($enumSet->attach($enum2)); |
||
37 | $this->assertTrue($enumSet->contains($enum2)); |
||
38 | |||
39 | $this->assertNull($enumSet->detach($enum1)); |
||
40 | $this->assertFalse($enumSet->contains($enum1)); |
||
41 | |||
42 | $this->assertNull($enumSet->detach($enum2)); |
||
43 | $this->assertFalse($enumSet->contains($enum2)); |
||
44 | } |
||
45 | |||
46 | public function testDeprecatedGetEnumClass() |
||
51 | |||
52 | public function testBasicWithConstantValuesAsEnums() |
||
53 | { |
||
54 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
55 | |||
56 | $enum1 = EnumBasic::ONE; |
||
57 | $enum2 = EnumBasic::TWO; |
||
58 | |||
59 | $this->assertFalse($enumSet->contains($enum1)); |
||
60 | $this->assertNull($enumSet->attach($enum1)); |
||
61 | $this->assertTrue($enumSet->contains($enum1)); |
||
62 | |||
63 | $this->assertFalse($enumSet->contains($enum2)); |
||
64 | $this->assertNull($enumSet->attach($enum2)); |
||
65 | $this->assertTrue($enumSet->contains($enum2)); |
||
66 | |||
67 | $this->assertNull($enumSet->detach($enum1)); |
||
68 | $this->assertFalse($enumSet->contains($enum1)); |
||
69 | |||
70 | $this->assertNull($enumSet->detach($enum2)); |
||
71 | $this->assertFalse($enumSet->contains($enum2)); |
||
72 | } |
||
73 | |||
74 | public function testUnique() |
||
75 | { |
||
76 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
77 | |||
78 | $enumSet->attach(EnumBasic::ONE()); |
||
79 | $enumSet->attach(EnumBasic::ONE); |
||
80 | |||
81 | $enumSet->attach(EnumBasic::TWO()); |
||
82 | $enumSet->attach(EnumBasic::TWO); |
||
83 | |||
84 | $this->assertSame(2, $enumSet->count()); |
||
85 | } |
||
86 | |||
87 | public function testIterateOrdered() |
||
88 | { |
||
89 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
90 | |||
91 | // an empty enum set needs to be invalid, starting by 0 |
||
92 | $this->assertSame(0, $enumSet->count()); |
||
93 | $this->assertFalse($enumSet->valid()); |
||
94 | $this->assertNull($enumSet->current()); |
||
95 | |||
96 | // attach |
||
97 | $enum1 = EnumBasic::ONE(); |
||
98 | $enum2 = EnumBasic::TWO(); |
||
99 | $enumSet->attach($enum1); |
||
100 | $enumSet->attach($enum2); |
||
101 | |||
102 | // a not empty enum set should be valid, starting by 0 (if not iterated) |
||
103 | $enumSet->rewind(); |
||
104 | $this->assertSame(2, $enumSet->count()); |
||
105 | $this->assertTrue($enumSet->valid()); |
||
106 | $this->assertSame($enum1->getOrdinal(), $enumSet->key()); |
||
107 | $this->assertSame($enum1, $enumSet->current()); |
||
108 | |||
109 | // go to the next element (last) |
||
110 | $this->assertNull($enumSet->next()); |
||
111 | $this->assertTrue($enumSet->valid()); |
||
112 | $this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
||
113 | $this->assertSame($enum2, $enumSet->current()); |
||
114 | |||
115 | // go to the next element (out of range) |
||
116 | $this->assertNull($enumSet->next()); |
||
117 | $this->assertFalse($enumSet->valid()); |
||
118 | $this->assertNull($enumSet->current()); |
||
119 | |||
120 | // rewind will set the iterator position back to 0 |
||
121 | $enumSet->rewind(); |
||
122 | $this->assertTrue($enumSet->valid()); |
||
123 | $this->assertSame(0, $enumSet->key()); |
||
124 | $this->assertSame($enum1, $enumSet->current()); |
||
125 | } |
||
126 | |||
127 | public function testIterateAndDetach() |
||
128 | { |
||
129 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance'); |
||
130 | |||
131 | $enum1 = EnumInheritance::ONE(); |
||
132 | $enum2 = EnumInheritance::TWO(); |
||
133 | $enum3 = EnumInheritance::INHERITANCE(); |
||
134 | |||
135 | // attach |
||
136 | $enumSet->attach($enum1); |
||
137 | $enumSet->attach($enum2); |
||
138 | $enumSet->attach($enum3); |
||
139 | |||
140 | // go to the next entry |
||
141 | $enumSet->next(); |
||
142 | $this->assertSame($enum2, $enumSet->current()); |
||
143 | |||
144 | // detach current entry |
||
145 | $enumSet->detach($enumSet->current()); |
||
146 | $this->assertFalse($enumSet->valid()); |
||
147 | $this->assertNull($enumSet->current()); |
||
148 | $this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
||
149 | |||
150 | // go to the next entry should be the last entry |
||
151 | $enumSet->next(); |
||
152 | $this->assertSame($enum3, $enumSet->current()); |
||
153 | |||
154 | // detech the last entry |
||
155 | $enumSet->detach($enumSet->current()); |
||
156 | $this->assertFalse($enumSet->valid()); |
||
157 | $this->assertNull($enumSet->current()); |
||
158 | $this->assertSame($enum3->getOrdinal(), $enumSet->key()); |
||
159 | } |
||
160 | |||
161 | public function testConstructThrowsInvalidArgumentExceptionIfEnumClassDoesNotExtendBaseEnum() |
||
162 | { |
||
163 | $this->setExpectedException('InvalidArgumentException'); |
||
164 | new EnumSet('stdClass'); |
||
165 | } |
||
166 | |||
167 | public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum() |
||
168 | { |
||
169 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
170 | $this->setExpectedException('InvalidArgumentException'); |
||
171 | $this->assertFalse($enumSet->contains(EnumInheritance::INHERITANCE())); |
||
172 | } |
||
173 | |||
174 | public function testIterateOutOfRangeIfLastOrdinalEnumIsSet() |
||
193 | |||
194 | public function testRewindFirstOnEmptySet() |
||
206 | |||
207 | View Code Duplication | public function test32EnumerationsSet() |
|
225 | |||
226 | View Code Duplication | public function test64EnumerationsSet() |
|
244 | |||
245 | public function test65EnumerationsSet() |
||
253 | |||
254 | View Code Duplication | public function testGetBinaryBitsetLe() |
|
|
|||
255 | { |
||
256 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
||
257 | |||
258 | $enum1 = Enum65::ONE; |
||
259 | $enum2 = Enum65::TWO; |
||
260 | $enum3 = Enum65::SIXTYFIVE; |
||
287 | |||
288 | View Code Duplication | public function testGetBinaryBitsetBe() |
|
321 | |||
322 | /** |
||
323 | * @deprecated |
||
324 | */ |
||
325 | View Code Duplication | public function testGetBitset() |
|
358 | |||
359 | View Code Duplication | public function testSetBinaryBitsetLe() |
|
370 | |||
371 | View Code Duplication | public function testSetBinaryBitsetBe() |
|
382 | |||
383 | /** |
||
384 | * @deprecated |
||
385 | */ |
||
386 | View Code Duplication | public function testSetBitset() |
|
397 | |||
398 | public function testSetBinaryBitsetLeShort() |
||
404 | |||
405 | public function testSetBinaryBitsetLeLong() |
||
411 | |||
412 | public function testSetBinaryBitsetLeArgumentExceptionIfNotString() |
||
419 | |||
420 | public function testSetBinaryBitsetBeArgumentExceptionIfNotString() |
||
427 | |||
428 | public function testCountingEmptyEnumEmptySet() |
||
433 | |||
434 | View Code Duplication | public function testIsEqual() |
|
448 | |||
449 | View Code Duplication | public function testIsEqualWrongInstance() |
|
463 | |||
464 | /** |
||
465 | * if $A->isEqual($B) is true then $A->isSubsetOf($B) is also true |
||
466 | */ |
||
467 | public function testIsSubsetEqual() |
||
479 | |||
480 | View Code Duplication | public function testIsSubsetFull() |
|
490 | |||
491 | View Code Duplication | public function testIsSubsetFalse() |
|
501 | |||
502 | View Code Duplication | public function testIsSubsetWrongInstance() |
|
516 | |||
517 | /** |
||
518 | * if $A->isEqual($B) is true then $A->isSuperset($B) is also true |
||
519 | */ |
||
520 | public function testIsSsetEqual() |
||
532 | |||
533 | View Code Duplication | public function testIsSupersetFull() |
|
543 | |||
544 | View Code Duplication | public function testIsSupersetFalse() |
|
554 | |||
555 | View Code Duplication | public function testIsSupersetWrongInstance() |
|
569 | } |
||
570 |
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.