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 |
||
| 15 | final class CashPaymentTest extends TestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @test |
||
| 19 | * @dataProvider getAmounts |
||
| 20 | */ |
||
| 21 | public function changedAmount($shouldPay, $payed) |
||
| 22 | { |
||
| 23 | $shouldReceiveChange = ($payed - $shouldPay); |
||
| 24 | |||
| 25 | $payment = new CashPayment(Uuid::uuid1(), Money::EUR($shouldPay), Money::EUR($payed)); |
||
| 26 | |||
| 27 | self::assertSame($shouldReceiveChange, (int) $payment->getChangeAmount()->getAmount()); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function getAmounts(): array |
||
| 31 | { |
||
| 32 | return [ |
||
| 33 | 'positive 1' => [200, 500], |
||
| 34 | 'positive 2' => [1000, 250], |
||
| 35 | 'negative 1' => [-100, 0], |
||
| 36 | 'negative 2' => [-1000, -1000], |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | } |
||
| 40 |