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 |
||
23 | class OrangePaymentSlipTest extends PaymentSlipTestCase |
||
24 | { |
||
25 | /** |
||
26 | * The object under test |
||
27 | * |
||
28 | * @var OrangePaymentSlip |
||
29 | */ |
||
30 | protected $paymentSlip; |
||
31 | |||
32 | /** |
||
33 | * Setup a slip to test and some default and set attributes to test |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | protected function setUp() |
||
70 | |||
71 | /** |
||
72 | * Tests the getPaymentSlipData method |
||
73 | * |
||
74 | * @return void |
||
75 | * @covers ::getPaymentSlipData |
||
76 | */ |
||
77 | public function testGetPaymentSlipDataIsInstanceOf() |
||
84 | |||
85 | /** |
||
86 | * Tests the default background |
||
87 | * |
||
88 | * @return void |
||
89 | * @covers ::setDefaults |
||
90 | */ |
||
91 | public function testSlipBackgroundDefaultValues() |
||
95 | |||
96 | /** |
||
97 | * Tests the default attributes of the left reference number element |
||
98 | * |
||
99 | * @return void |
||
100 | * @covers ::setDefaults |
||
101 | */ |
||
102 | View Code Duplication | public function testReferenceNumberLeftAttrDefaultValues() |
|
116 | |||
117 | /** |
||
118 | * Tests the default attributes of the right reference number element |
||
119 | * |
||
120 | * @return void |
||
121 | * @covers ::setDefaults |
||
122 | */ |
||
123 | View Code Duplication | public function testReferenceNumberRightAttrDefaultValues() |
|
137 | |||
138 | /** |
||
139 | * Tests the default attributes of the code line element |
||
140 | * |
||
141 | * @return void |
||
142 | * @covers ::setDefaults |
||
143 | */ |
||
144 | View Code Duplication | public function testCodeLineAttrDefaultValues() |
|
159 | |||
160 | /** |
||
161 | * Tests the setReferenceNumberLeftAttr method |
||
162 | * |
||
163 | * @return void |
||
164 | * @covers ::setReferenceNumberLeftAttr |
||
165 | * @covers ::setAttributes |
||
166 | * @covers ::getReferenceNumberLeftAttr |
||
167 | */ |
||
168 | public function testSetReferenceNumberLeftAttr() |
||
174 | |||
175 | /** |
||
176 | * Tests the setReferenceNumberRightAttr method |
||
177 | * |
||
178 | * @return void |
||
179 | * @covers ::setReferenceNumberRightAttr |
||
180 | * @covers ::setAttributes |
||
181 | * @covers ::getReferenceNumberRightAttr |
||
182 | */ |
||
183 | public function testSetReferenceNumberRightAttr() |
||
189 | |||
190 | /** |
||
191 | * Tests the setCodeLineAttr method |
||
192 | * |
||
193 | * @return void |
||
194 | * @covers ::setCodeLineAttr |
||
195 | * @covers ::setAttributes |
||
196 | * @covers ::getCodeLineAttr |
||
197 | */ |
||
198 | public function testSetCodeLineAttr() |
||
204 | |||
205 | /** |
||
206 | * Tests the setDisplayReferenceNr method |
||
207 | * |
||
208 | * @return void |
||
209 | * @covers ::setDisplayReferenceNr |
||
210 | * @covers ::getDisplayReferenceNr |
||
211 | * @covers ::isBool |
||
212 | */ |
||
213 | View Code Duplication | public function testSetDisplayReferenceNr() |
|
231 | |||
232 | /** |
||
233 | * Tests the setDisplayReferenceNr method with an invalid parameter |
||
234 | * |
||
235 | * @return void |
||
236 | * @expectedException \InvalidArgumentException |
||
237 | * @expectedExceptionMessage $displayReferenceNr is not a boolean. |
||
238 | * @covers ::setDisplayReferenceNr |
||
239 | * @covers ::isBool |
||
240 | */ |
||
241 | public function testSetDisplayReferenceNrInvalidParameter() |
||
245 | |||
246 | /** |
||
247 | * Tests the setDisplayCodeLine method |
||
248 | * |
||
249 | * @return void |
||
250 | * @covers ::setDisplayCodeLine |
||
251 | * @covers ::getDisplayCodeLine |
||
252 | * @covers ::isBool |
||
253 | */ |
||
254 | public function testSetDisplayCodeLine() |
||
278 | |||
279 | /** |
||
280 | * Tests the setDisplayCodeLine method with an invalid parameter |
||
281 | * |
||
282 | * @return void |
||
283 | * @expectedException \InvalidArgumentException |
||
284 | * @expectedExceptionMessage $displayCodeLine is not a boolean. |
||
285 | * @covers ::setDisplayCodeLine |
||
286 | * @covers ::isBool |
||
287 | */ |
||
288 | public function testSetDisplayCodeLineInvalidParameter() |
||
292 | |||
293 | /** |
||
294 | * Tests the setReferenceNrFormatted method |
||
295 | * |
||
296 | * @return void |
||
297 | * @covers ::setReferenceNrFormatted |
||
298 | * @covers ::getReferenceNrFormatted |
||
299 | * @covers ::isBool |
||
300 | */ |
||
301 | View Code Duplication | public function testSetReferenceNrFormatted() |
|
315 | |||
316 | /** |
||
317 | * Tests the setReferenceNrFormatted method with an invalid parameter |
||
318 | * |
||
319 | * @return void |
||
320 | * @expectedException \InvalidArgumentException |
||
321 | * @expectedExceptionMessage $referenceNrFormatted is not a boolean. |
||
322 | * @covers ::setReferenceNrFormatted |
||
323 | * @covers ::isBool |
||
324 | */ |
||
325 | public function testSetReferenceNrFormattedInvalidParameter() |
||
329 | |||
330 | /** |
||
331 | * Tests the setReferenceNrFillZeros method |
||
332 | * |
||
333 | * @return void |
||
334 | * @covers ::setReferenceNrFillZeros |
||
335 | * @covers ::getReferenceNrFillZeros |
||
336 | * @covers ::isBool |
||
337 | */ |
||
338 | View Code Duplication | public function testSetReferenceNrFillZeros() |
|
352 | |||
353 | /** |
||
354 | * Tests the setReferenceNrFillZeros method with an invalid parameter |
||
355 | * |
||
356 | * @return void |
||
357 | * @expectedException \InvalidArgumentException |
||
358 | * @expectedExceptionMessage $referenceNrFillZeros is not a boolean. |
||
359 | * @covers ::setReferenceNrFillZeros |
||
360 | * @covers ::isBool |
||
361 | */ |
||
362 | public function testSetReferenceNrFillZerosInvalidParameter() |
||
366 | |||
367 | /** |
||
368 | * Tests the getAllElements method |
||
369 | * |
||
370 | * @return void |
||
371 | * @covers ::getAllElements |
||
372 | */ |
||
373 | View Code Duplication | public function testGetAllElements() |
|
397 | |||
398 | /** |
||
399 | * Tests the getAllElements method when no elements are shown |
||
400 | * |
||
401 | * @return void |
||
402 | * @covers ::getAllElements |
||
403 | */ |
||
404 | View Code Duplication | public function testGetAllElementsNoElementsShown() |
|
420 | |||
421 | /** |
||
422 | * Tests the getAllElements method when all data is disabled |
||
423 | * |
||
424 | * @return void |
||
425 | * @covers ::getAllElements |
||
426 | */ |
||
427 | View Code Duplication | public function testGetAllElementsDisabledData() |
|
444 | } |
||
445 |
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.