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 |
||
21 | class EnumTest extends TestCase |
||
22 | { |
||
23 | public function setUp() |
||
24 | { |
||
25 | EnumBasic::clear(); |
||
26 | EnumInheritance::clear(); |
||
27 | } |
||
28 | |||
29 | public function testGetNameReturnsConstantNameOfCurrentValue() |
||
30 | { |
||
31 | $enum = EnumBasic::get(EnumBasic::ONE); |
||
32 | $this->assertSame('ONE', $enum->getName()); |
||
33 | } |
||
34 | |||
35 | public function testToStringMagicMethodReturnsName() |
||
36 | { |
||
37 | $enum = EnumBasic::get(EnumBasic::ONE); |
||
38 | $this->assertSame('ONE', $enum->__toString()); |
||
39 | } |
||
40 | |||
41 | public function testEnumInheritance() |
||
42 | { |
||
43 | $this->assertSame(array( |
||
44 | 'ONE' => 1, |
||
45 | 'TWO' => 2, |
||
46 | 'THREE' => 3, |
||
47 | 'FOUR' => 4, |
||
48 | 'FIVE' => 5, |
||
49 | 'SIX' => 6, |
||
50 | 'SEVEN' => 7, |
||
51 | 'EIGHT' => 8, |
||
52 | 'NINE' => 9, |
||
53 | 'ZERO' => 0, |
||
54 | 'FLOAT' => 0.123, |
||
55 | 'STR' => 'str', |
||
56 | 'STR_EMPTY' => '', |
||
57 | 'NIL' => null, |
||
58 | 'BOOLEAN_TRUE' => true, |
||
59 | 'BOOLEAN_FALSE' => false, |
||
60 | 'INHERITANCE' => 'Inheritance', |
||
61 | ), EnumInheritance::getConstants()); |
||
62 | |||
63 | $enum = EnumInheritance::get(EnumInheritance::ONE); |
||
64 | $this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
||
65 | $this->assertSame(0, $enum->getOrdinal()); |
||
66 | |||
67 | $enum = EnumInheritance::get(EnumInheritance::INHERITANCE); |
||
68 | $this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue()); |
||
69 | $this->assertSame(16, $enum->getOrdinal()); |
||
70 | } |
||
71 | |||
72 | public function testGetWithStrictValue() |
||
73 | { |
||
74 | $enum = EnumBasic::get(EnumBasic::ONE); |
||
75 | $this->assertSame(1, $enum->getValue()); |
||
76 | $this->assertSame(0, $enum->getOrdinal()); |
||
77 | } |
||
78 | |||
79 | public function testGetWithNonStrictValueThrowsInvalidArgumentException() |
||
80 | { |
||
81 | $this->setExpectedException('InvalidArgumentException'); |
||
82 | EnumBasic::get((string)EnumBasic::TWO); |
||
83 | } |
||
84 | |||
85 | public function testGetWithInvalidValueThrowsInvalidArgumentException() |
||
86 | { |
||
87 | $this->setExpectedException('InvalidArgumentException'); |
||
88 | EnumBasic::get('unknown'); |
||
89 | } |
||
90 | |||
91 | public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException() |
||
92 | { |
||
93 | $this->setExpectedException('InvalidArgumentException'); |
||
94 | EnumBasic::get(array()); |
||
95 | } |
||
96 | |||
97 | public function testGetByInstance() |
||
98 | { |
||
99 | $enum1 = EnumBasic::get(EnumBasic::ONE); |
||
100 | $enum2 = EnumBasic::get($enum1); |
||
101 | $this->assertSame($enum1, $enum2); |
||
102 | } |
||
103 | |||
104 | public function testGetByExtendedInstanceOfKnownValue() |
||
111 | |||
112 | public function testGetEnumerators() |
||
113 | { |
||
114 | $constants = EnumInheritance::getConstants(); |
||
115 | $enumerators = EnumInheritance::getEnumerators(); |
||
116 | $count = count($enumerators); |
||
117 | |||
118 | $this->assertSame(count($constants), $count); |
||
119 | for ($i = 0; $i < $count; ++$i) { |
||
128 | |||
129 | View Code Duplication | public function testGetValues() |
|
141 | |||
142 | View Code Duplication | public function testGetNames() |
|
154 | |||
155 | View Code Duplication | public function testGetOrdinals() |
|
167 | |||
168 | public function testGetAllValues() |
||
177 | |||
178 | View Code Duplication | public function testIsBasic() |
|
191 | |||
192 | public function testCallingGetOrdinalTwoTimesWillResultTheSameValue() |
||
198 | |||
199 | public function testInstantiateUsingOrdinalNumber() |
||
205 | |||
206 | public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException() |
||
211 | |||
212 | public function testInstantiateByName() |
||
218 | |||
219 | public function testInstantiateByUnknownNameThrowsInvalidArgumentException() |
||
224 | |||
225 | public function testInstantiateUsingMagicMethod() |
||
231 | |||
232 | public function testAmbiguousConstantsThrowsLogicException() |
||
237 | |||
238 | public function testExtendedAmbiguousCanstantsThrowsLogicException() |
||
243 | |||
244 | public function testSingleton() |
||
250 | |||
251 | public function testClear() |
||
261 | |||
262 | View Code Duplication | public function testCloneNotCallableAndThrowsLogicException() |
|
275 | |||
276 | public function testNotSerializable() |
||
283 | |||
284 | public function testNotUnserializable() |
||
289 | |||
290 | View Code Duplication | public function testHas() |
|
299 | |||
300 | public function testConstVisibility() |
||
312 | |||
313 | public function testConstVisibilityExtended() |
||
327 | } |
||
328 |
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.