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:
1 | <?php |
||
20 | class EnumSetTest extends TestCase |
||
21 | { |
||
22 | public function testBasic() |
||
23 | { |
||
24 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
25 | $this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumSet->getEnumeration()); |
||
26 | |||
27 | $enum1 = EnumBasic::ONE(); |
||
28 | $enum2 = EnumBasic::TWO(); |
||
29 | |||
30 | $this->assertFalse($enumSet->contains($enum1)); |
||
31 | $this->assertNull($enumSet->attach($enum1)); |
||
32 | $this->assertTrue($enumSet->contains($enum1)); |
||
33 | |||
34 | $this->assertFalse($enumSet->contains($enum2)); |
||
35 | $this->assertNull($enumSet->attach($enum2)); |
||
36 | $this->assertTrue($enumSet->contains($enum2)); |
||
37 | |||
38 | $this->assertNull($enumSet->detach($enum1)); |
||
39 | $this->assertFalse($enumSet->contains($enum1)); |
||
40 | |||
41 | $this->assertNull($enumSet->detach($enum2)); |
||
42 | $this->assertFalse($enumSet->contains($enum2)); |
||
43 | } |
||
44 | |||
45 | public function testDeprecatedGetEnumClass() |
||
50 | |||
51 | public function testBasicWithConstantValuesAsEnums() |
||
52 | { |
||
53 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
54 | |||
55 | $enum1 = EnumBasic::ONE; |
||
56 | $enum2 = EnumBasic::TWO; |
||
57 | |||
58 | $this->assertFalse($enumSet->contains($enum1)); |
||
59 | $this->assertNull($enumSet->attach($enum1)); |
||
60 | $this->assertTrue($enumSet->contains($enum1)); |
||
61 | |||
62 | $this->assertFalse($enumSet->contains($enum2)); |
||
63 | $this->assertNull($enumSet->attach($enum2)); |
||
64 | $this->assertTrue($enumSet->contains($enum2)); |
||
65 | |||
66 | $this->assertNull($enumSet->detach($enum1)); |
||
67 | $this->assertFalse($enumSet->contains($enum1)); |
||
68 | |||
69 | $this->assertNull($enumSet->detach($enum2)); |
||
70 | $this->assertFalse($enumSet->contains($enum2)); |
||
71 | } |
||
72 | |||
73 | public function testUnique() |
||
74 | { |
||
75 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
76 | |||
77 | $enumSet->attach(EnumBasic::ONE()); |
||
78 | $enumSet->attach(EnumBasic::ONE); |
||
79 | |||
80 | $enumSet->attach(EnumBasic::TWO()); |
||
81 | $enumSet->attach(EnumBasic::TWO); |
||
82 | |||
83 | $this->assertSame(2, $enumSet->count()); |
||
84 | } |
||
85 | |||
86 | public function testIterateOrdered() |
||
87 | { |
||
88 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
89 | |||
90 | // an empty enum set needs to be invalid, starting by 0 |
||
91 | $this->assertSame(0, $enumSet->count()); |
||
92 | $this->assertFalse($enumSet->valid()); |
||
93 | $this->assertNull($enumSet->current()); |
||
94 | |||
95 | // attach |
||
96 | $enum1 = EnumBasic::ONE(); |
||
97 | $enum2 = EnumBasic::TWO(); |
||
98 | $enumSet->attach($enum1); |
||
99 | $enumSet->attach($enum2); |
||
100 | |||
101 | // a not empty enum set should be valid, starting by 0 (if not iterated) |
||
102 | $enumSet->rewind(); |
||
103 | $this->assertSame(2, $enumSet->count()); |
||
104 | $this->assertTrue($enumSet->valid()); |
||
105 | $this->assertSame($enum1->getOrdinal(), $enumSet->key()); |
||
106 | $this->assertSame($enum1, $enumSet->current()); |
||
107 | |||
108 | // go to the next element (last) |
||
109 | $this->assertNull($enumSet->next()); |
||
110 | $this->assertTrue($enumSet->valid()); |
||
111 | $this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
||
112 | $this->assertSame($enum2, $enumSet->current()); |
||
113 | |||
114 | // go to the next element (out of range) |
||
115 | $this->assertNull($enumSet->next()); |
||
116 | $this->assertFalse($enumSet->valid()); |
||
117 | $this->assertNull($enumSet->current()); |
||
118 | |||
119 | // rewind will set the iterator position back to 0 |
||
120 | $enumSet->rewind(); |
||
121 | $this->assertTrue($enumSet->valid()); |
||
122 | $this->assertSame(0, $enumSet->key()); |
||
123 | $this->assertSame($enum1, $enumSet->current()); |
||
124 | } |
||
125 | |||
126 | public function testIterateAndDetach() |
||
127 | { |
||
128 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance'); |
||
129 | |||
130 | $enum1 = EnumInheritance::ONE(); |
||
131 | $enum2 = EnumInheritance::TWO(); |
||
132 | $enum3 = EnumInheritance::INHERITANCE(); |
||
133 | |||
134 | // attach |
||
135 | $enumSet->attach($enum1); |
||
136 | $enumSet->attach($enum2); |
||
137 | $enumSet->attach($enum3); |
||
138 | |||
139 | // go to the next entry |
||
140 | $enumSet->next(); |
||
141 | $this->assertSame($enum2, $enumSet->current()); |
||
142 | |||
143 | // detach current entry |
||
144 | $enumSet->detach($enumSet->current()); |
||
145 | $this->assertFalse($enumSet->valid()); |
||
146 | $this->assertNull($enumSet->current()); |
||
147 | $this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
||
148 | |||
149 | // go to the next entry should be the last entry |
||
150 | $enumSet->next(); |
||
151 | $this->assertSame($enum3, $enumSet->current()); |
||
152 | |||
153 | // detech the last entry |
||
154 | $enumSet->detach($enumSet->current()); |
||
155 | $this->assertFalse($enumSet->valid()); |
||
156 | $this->assertNull($enumSet->current()); |
||
157 | $this->assertSame($enum3->getOrdinal(), $enumSet->key()); |
||
158 | } |
||
159 | |||
160 | public function testConstructThrowsInvalidArgumentExceptionIfEnumClassDoesNotExtendBaseEnum() |
||
161 | { |
||
162 | $this->setExpectedException('InvalidArgumentException'); |
||
163 | new EnumSet('stdClass'); |
||
164 | } |
||
165 | |||
166 | public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum() |
||
167 | { |
||
168 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
||
169 | $this->setExpectedException('InvalidArgumentException'); |
||
170 | $this->assertFalse($enumSet->contains(EnumInheritance::INHERITANCE())); |
||
171 | } |
||
172 | |||
173 | public function testIterateOutOfRangeIfLastOrdinalEnumIsSet() |
||
192 | |||
193 | public function testRewindFirstOnEmptySet() |
||
205 | |||
206 | View Code Duplication | public function test32EnumerationsSet() |
|
224 | |||
225 | View Code Duplication | public function test64EnumerationsSet() |
|
243 | |||
244 | public function test65EnumerationsSet() |
||
252 | |||
253 | View Code Duplication | public function testGetBinaryBitsetLe() |
|
|
|||
254 | { |
||
255 | $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
||
256 | |||
257 | $enum1 = Enum65::ONE; |
||
258 | $enum2 = Enum65::TWO; |
||
259 | $enum3 = Enum65::SIXTYFIVE; |
||
286 | |||
287 | View Code Duplication | public function testGetBinaryBitsetBe() |
|
320 | |||
321 | /** |
||
322 | * @deprecated |
||
323 | */ |
||
324 | View Code Duplication | public function testGetBitset() |
|
357 | |||
358 | View Code Duplication | public function testSetBinaryBitsetLe() |
|
369 | |||
370 | View Code Duplication | public function testSetBinaryBitsetBe() |
|
381 | |||
382 | /** |
||
383 | * @deprecated |
||
384 | */ |
||
385 | View Code Duplication | public function testSetBitset() |
|
396 | |||
397 | public function testSetBinaryBitsetLeShort() |
||
403 | |||
404 | public function testSetBinaryBitsetLeLong() |
||
410 | |||
411 | public function testSetBinaryBitsetLeArgumentExceptionIfNotString() |
||
418 | |||
419 | public function testSetBinaryBitsetBeArgumentExceptionIfNotString() |
||
426 | } |
||
427 |
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.