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 |
||
22 | class OrangePaymentSlipDataTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | /** |
||
25 | * The object under test |
||
26 | * |
||
27 | * @var OrangePaymentSlipData |
||
28 | */ |
||
29 | protected $slipData; |
||
30 | |||
31 | /** |
||
32 | * Setup the object under test |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | protected function setUp() |
||
40 | |||
41 | /** |
||
42 | * Tests the getWithReferenceNumber and setWithReferenceNumber methods |
||
43 | * |
||
44 | * @return void |
||
45 | * @covers ::setWithReferenceNumber |
||
46 | * @covers ::getWithReferenceNumber |
||
47 | * @covers ::isBool |
||
48 | * @covers ::setReferenceNumber |
||
49 | * @covers ::getReferenceNumber |
||
50 | */ |
||
51 | View Code Duplication | public function testSetWithReferenceNumber() |
|
72 | |||
73 | /** |
||
74 | * Tests the setReferenceNumber method when disabled |
||
75 | * |
||
76 | * @return void |
||
77 | * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException |
||
78 | * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first. |
||
79 | * @covers ::setReferenceNumber |
||
80 | */ |
||
81 | public function testSetReferenceNumberWhenDisabled() |
||
86 | |||
87 | /** |
||
88 | * Tests the getReferenceNumber method when disabled |
||
89 | * |
||
90 | * @return void |
||
91 | * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException |
||
92 | * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first. |
||
93 | * @covers ::getReferenceNumber |
||
94 | */ |
||
95 | public function testGetReferenceNumberWhenDisabled() |
||
100 | |||
101 | /** |
||
102 | * Tests the setWithReferenceNumber method with an invalid parameter |
||
103 | * |
||
104 | * @return void |
||
105 | * @expectedException \InvalidArgumentException |
||
106 | * @expectedExceptionMessage $withReferenceNumber is not a boolean. |
||
107 | * @covers ::setWithReferenceNumber |
||
108 | * @covers ::isBool |
||
109 | */ |
||
110 | public function testSetWithReferenceNumberInvalidParameter() |
||
114 | |||
115 | /** |
||
116 | * Tests the getWithBankingCustomerId and setWithBankingCustomerId methods |
||
117 | * |
||
118 | * @return void |
||
119 | * @covers ::setWithBankingCustomerId |
||
120 | * @covers ::getWithBankingCustomerId |
||
121 | * @covers ::isBool |
||
122 | * @covers ::setBankingCustomerId |
||
123 | * @covers ::getBankingCustomerId |
||
124 | */ |
||
125 | View Code Duplication | public function testSetWithBankingCustomerId() |
|
146 | |||
147 | /** |
||
148 | * Tests the setBankingCustomerId method when disabled |
||
149 | * |
||
150 | * @return void |
||
151 | * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException |
||
152 | * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first. |
||
153 | * @covers ::setBankingCustomerId |
||
154 | */ |
||
155 | public function testSetBankingCustomerIdNumberWhenDisabled() |
||
160 | |||
161 | /** |
||
162 | * Tests the getBankingCustomerId method when disabled |
||
163 | * |
||
164 | * @return void |
||
165 | * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException |
||
166 | * @expectedExceptionMessage You are accessing the disabled banking customer ID. You need to re-enable it first. |
||
167 | * @covers ::getBankingCustomerId |
||
168 | */ |
||
169 | public function testGetBankingCustomerIdNumberWhenDisabled() |
||
174 | |||
175 | /** |
||
176 | * Tests the setWithBankingCustomerId method with an invalid parameter |
||
177 | * |
||
178 | * @return void |
||
179 | * @expectedException \InvalidArgumentException |
||
180 | * @expectedExceptionMessage $withBankingCustomerId is not a boolean. |
||
181 | * @covers ::setWithBankingCustomerId |
||
182 | * @covers ::isBool |
||
183 | */ |
||
184 | public function testSetWithBankingCustomerIdInvalidParameter() |
||
188 | |||
189 | /** |
||
190 | * Tests the getCompleteReferenceNumber method |
||
191 | * |
||
192 | * @return void |
||
193 | * @covers ::getCompleteReferenceNumber |
||
194 | * @covers ::appendCheckDigit |
||
195 | * @covers ::breakStringIntoBlocks |
||
196 | * @covers ::modulo10 |
||
197 | */ |
||
198 | public function testGetCompleteReferenceNumber() |
||
249 | |||
250 | /** |
||
251 | * Tests the getCompleteReferenceNumber method with the reference number disabled |
||
252 | * |
||
253 | * @return void |
||
254 | * @expectedException \SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException |
||
255 | * @expectedExceptionMessage You are accessing the disabled reference number. You need to re-enable it first. |
||
256 | * @covers ::getCompleteReferenceNumber |
||
257 | * @covers ::getReferenceNumber |
||
258 | */ |
||
259 | public function testGetCompleteReferenceNumberWithReferenceNrDisabled() |
||
264 | |||
265 | /** |
||
266 | * Tests the getCodeLine method |
||
267 | * |
||
268 | * @return void |
||
269 | * @covers ::getCodeLine |
||
270 | * @covers ::modulo10 |
||
271 | * @covers ::getAccountDigits |
||
272 | */ |
||
273 | public function testGetCodeLine() |
||
344 | |||
345 | /** |
||
346 | * Tests the setNotForPayment method |
||
347 | * |
||
348 | * @return void |
||
349 | * @covers ::setNotForPayment |
||
350 | * @covers ::appendCheckDigit |
||
351 | * @covers ::getCompleteReferenceNumber |
||
352 | * @covers ::getCodeLine |
||
353 | * @covers ::getAccountDigits |
||
354 | */ |
||
355 | public function testSetNotForPayment() |
||
367 | |||
368 | /** |
||
369 | * Tests the setNotForPayment method when fields are disabled |
||
370 | * |
||
371 | * @return void |
||
372 | * @covers ::setNotForPayment |
||
373 | */ |
||
374 | public function testSetNotForPaymentDisabledFields() |
||
381 | |||
382 | /** |
||
383 | * Tests the setAmount method with a problematic float as amount parameter |
||
384 | * |
||
385 | * It is common computer science knowledge that in some cases floats can be imprecise. |
||
386 | * The class should handle that by rounding it properly. |
||
387 | * |
||
388 | * @return void |
||
389 | * @covers ::setAmount |
||
390 | */ |
||
391 | public function testSetAmountWithProblematicFloat() |
||
436 | |||
437 | /** |
||
438 | * Tests the setAmount method with a another problematic float as amount parameter |
||
439 | * |
||
440 | * It is common computer science knowledge that in some cases floats can be imprecise. |
||
441 | * The class should handle that by rounding it properly. |
||
442 | * |
||
443 | * @return void |
||
444 | * @covers ::setAmount |
||
445 | */ |
||
446 | public function testSetAmountWithAnotherProblematicFloat() |
||
457 | } |
||
458 |
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.