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 |
||
24 | class EnumTests extends EnumTestCase |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function newDefaultTestedInstance() |
||
33 | |||
34 | /** |
||
35 | * Test value method. |
||
36 | */ |
||
37 | View Code Duplication | public function testValue() |
|
47 | |||
48 | /** |
||
49 | * Test name method. |
||
50 | */ |
||
51 | View Code Duplication | public function testName() |
|
61 | |||
62 | /** |
||
63 | * Test hashCode method. |
||
64 | */ |
||
65 | View Code Duplication | public function testHashCode() |
|
75 | |||
76 | /** |
||
77 | * Test __toString method. |
||
78 | */ |
||
79 | View Code Duplication | public function testToString() |
|
89 | |||
90 | /** |
||
91 | * Test is method. |
||
92 | */ |
||
93 | public function testIs() |
||
104 | |||
105 | /** |
||
106 | * Test isValidName method. |
||
107 | */ |
||
108 | public function testIsValidName() |
||
131 | |||
132 | /** |
||
133 | * Test names method. |
||
134 | */ |
||
135 | public function testNames() |
||
144 | |||
145 | /** |
||
146 | * Test values method. |
||
147 | */ |
||
148 | public function testValues() |
||
157 | |||
158 | /** |
||
159 | * Test __DEFAULT method. |
||
160 | */ |
||
161 | public function testDefault() |
||
162 | { |
||
163 | $this->equalityTest(EnumFixture::__DEFAULT(), EnumFixture::FOO()); |
||
164 | $this->equalityTest(DefaultEnumFixture::__DEFAULT(), DefaultEnumFixture::BAR()); |
||
165 | |||
166 | $this |
||
167 | ->exception(function () { |
||
168 | BadDefaultEnumFixture::__DEFAULT(); |
||
169 | }) |
||
170 | ->isInstanceof(\UnexpectedValueException::class) |
||
171 | ->exception(function () { |
||
172 | BadDefaultEnumFixture::BAZ(); |
||
173 | }) |
||
174 | ->isInstanceof(\BadMethodCallException::class) |
||
175 | ; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Test ensure method. |
||
180 | */ |
||
181 | public function testEnsure() |
||
182 | { |
||
183 | $this->equalityTest(EnumFixture::ensure(EnumFixture::FOO()), EnumFixture::FOO()); |
||
184 | $this->equalityTest(EnumFixture::ensure(EnumFixture::FOO(), EnumFixture::BAR()), EnumFixture::FOO()); |
||
185 | $this->equalityTest(EnumFixture::ensure(), EnumFixture::__DEFAULT()); |
||
186 | $this->equalityTest(EnumFixture::ensure(null, EnumFixture::BAR()), EnumFixture::BAR()); |
||
187 | |||
188 | $this |
||
189 | ->exception(function () { |
||
190 | EnumFixture::ensure(DefaultEnumFixture::FOO()); |
||
191 | }) |
||
192 | ->isInstanceof(\InvalidArgumentException::class) |
||
193 | ; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param Enum $enum1 |
||
198 | * @param Enum $enum2 |
||
199 | */ |
||
200 | protected function equalityTest(Enum $enum1, Enum $enum2) |
||
209 | } |
||
210 |
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.