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 EnumTest 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 EnumTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class EnumTest extends TestCase |
||
23 | { |
||
24 | public function testGetNameReturnsConstantNameOfCurrentValue() |
||
29 | |||
30 | public function testToStringMagicMethodReturnsName() |
||
35 | |||
36 | public function testEnumInheritance() |
||
37 | { |
||
38 | $this->assertSame([ |
||
39 | 'ONE' => 1, |
||
40 | 'TWO' => 2, |
||
41 | 'THREE' => 3, |
||
42 | 'FOUR' => 4, |
||
43 | 'FIVE' => 5, |
||
44 | 'SIX' => 6, |
||
45 | 'SEVEN' => 7, |
||
46 | 'EIGHT' => 8, |
||
47 | 'NINE' => 9, |
||
48 | 'ZERO' => 0, |
||
49 | 'FLOAT' => 0.123, |
||
50 | 'STR' => 'str', |
||
51 | 'STR_EMPTY' => '', |
||
52 | 'NIL' => null, |
||
53 | 'BOOLEAN_TRUE' => true, |
||
54 | 'BOOLEAN_FALSE' => false, |
||
55 | 'INHERITANCE' => 'Inheritance', |
||
56 | ], EnumInheritance::getConstants()); |
||
57 | |||
58 | $enum = EnumInheritance::get(EnumInheritance::ONE); |
||
59 | $this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
||
60 | $this->assertSame(0, $enum->getOrdinal()); |
||
61 | |||
62 | $enum = EnumInheritance::get(EnumInheritance::INHERITANCE); |
||
63 | $this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue()); |
||
64 | $this->assertSame(16, $enum->getOrdinal()); |
||
65 | } |
||
66 | |||
67 | public function testGetWithStrictValue() |
||
73 | |||
74 | public function testGetWithNonStrictValueThrowsInvalidArgumentException() |
||
79 | |||
80 | public function testGetWithInvalidValueThrowsInvalidArgumentException() |
||
85 | |||
86 | public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException() |
||
91 | |||
92 | public function testGetByInstance() |
||
98 | |||
99 | public function testGetByExtendedInstanceOfKnownValue() |
||
106 | |||
107 | public function testGetEnumerators() |
||
108 | { |
||
109 | $constants = EnumInheritance::getConstants(); |
||
110 | $enumerators = EnumInheritance::getEnumerators(); |
||
111 | $count = count($enumerators); |
||
112 | |||
113 | $this->assertSame(count($constants), $count); |
||
114 | for ($i = 0; $i < $count; ++$i) { |
||
115 | $this->assertArrayHasKey($i, $enumerators); |
||
116 | $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enumerators[ $i ]); |
||
117 | |||
118 | $enumerator = $enumerators[ $i ]; |
||
119 | $this->assertArrayHasKey($enumerator->getName(), $constants); |
||
120 | $this->assertSame($constants[ $enumerator->getName() ], $enumerator->getValue()); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | View Code Duplication | public function testGetValues() |
|
|
|||
125 | { |
||
126 | $expectedValues = array_values(EnumInheritance::getConstants()); |
||
127 | $values = EnumInheritance::getValues(); |
||
128 | $count = count($values); |
||
129 | |||
130 | $this->assertSame(count($expectedValues), $count); |
||
131 | for ($i = 0; $i < $count; ++$i) { |
||
132 | $this->assertArrayHasKey($i, $values); |
||
133 | $this->assertSame($expectedValues[ $i ], $values[ $i ]); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | View Code Duplication | public function testGetNames() |
|
149 | |||
150 | View Code Duplication | public function testGetOrdinals() |
|
162 | |||
163 | public function testGetAllValues() |
||
172 | |||
173 | View Code Duplication | public function testIsBasic() |
|
186 | |||
187 | public function testCallingGetOrdinalTwoTimesWillResultTheSameValue() |
||
193 | |||
194 | public function testInstantiateUsingOrdinalNumber() |
||
200 | |||
201 | public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException() |
||
206 | |||
207 | public function testInstantiateByName() |
||
213 | |||
214 | public function testInstantiateByUnknownNameThrowsInvalidArgumentException() |
||
219 | |||
220 | public function testInstantiateUsingMagicMethod() |
||
226 | |||
227 | public function testAmbiguousConstantsThrowsLogicException() |
||
232 | |||
233 | public function testExtendedAmbiguousCanstantsThrowsLogicException() |
||
238 | |||
239 | public function testSingleton() |
||
245 | |||
246 | public function testClear() |
||
256 | |||
257 | public function testClone() |
||
258 | { |
||
259 | $enum = EnumBasic::ONE(); |
||
260 | |||
261 | $reflectionClass = new ReflectionClass($enum); |
||
262 | $reflectionMethod = $reflectionClass->getMethod('__clone'); |
||
263 | $this->assertTrue($reflectionMethod->isPublic(), 'The method __clone must be private'); |
||
264 | $this->assertTrue($reflectionMethod->isFinal(), 'The method __clone must be final'); |
||
265 | |||
266 | $reflectionMethod->setAccessible(true); |
||
267 | $enumClone = $reflectionMethod->invoke($enum); |
||
268 | $this->assertSame($enum, $enumClone); |
||
269 | } |
||
270 | |||
271 | public function testNotSerializable() |
||
278 | |||
279 | public function testNotUnserializable() |
||
284 | |||
285 | View Code Duplication | public function testHas() |
|
286 | { |
||
287 | $enum = EnumBasic::ONE(); |
||
288 | |||
289 | $this->assertFalse($enum->has('invalid')); |
||
290 | $this->assertFalse($enum->has(EnumInheritance::ONE())); |
||
291 | $this->assertTrue($enum->has(EnumBasic::ONE())); |
||
292 | $this->assertTrue($enum->has(EnumBasic::ONE)); |
||
293 | } |
||
294 | |||
295 | public function testConstVisibility() |
||
296 | { |
||
297 | if (PHP_VERSION_ID < 70100) { |
||
298 | $this->markTestSkipped('This test is for PHP-7.1 and upper only'); |
||
299 | } |
||
300 | |||
301 | $constants = ConstVisibilityEnum::getConstants(); |
||
302 | $this->assertSame([ |
||
303 | 'IPUB' => ConstVisibilityEnum::IPUB, |
||
304 | 'PUB' => ConstVisibilityEnum::PUB, |
||
305 | ], $constants); |
||
306 | } |
||
307 | |||
308 | public function testConstVisibilityExtended() |
||
309 | { |
||
310 | if (PHP_VERSION_ID < 70100) { |
||
311 | $this->markTestSkipped('This test is for PHP-7.1 and upper only'); |
||
312 | } |
||
313 | |||
314 | $constants = ConstVisibilityEnumExtended::getConstants(); |
||
315 | $this->assertSame([ |
||
316 | 'IPUB' => ConstVisibilityEnumExtended::IPUB, |
||
317 | 'PUB' => ConstVisibilityEnumExtended::PUB, |
||
318 | 'IPUB2' => ConstVisibilityEnumExtended::IPUB2, |
||
319 | 'PUB2' => ConstVisibilityEnumExtended::PUB2, |
||
320 | ], $constants); |
||
321 | } |
||
322 | |||
323 | public function testIsSerializableIssue() |
||
335 | } |
||
336 |
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.