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 |
||
17 | class Money |
||
18 | { |
||
19 | /** |
||
20 | * The amount of the monetary unit. |
||
21 | * @var int |
||
22 | */ |
||
23 | private $amount; |
||
24 | |||
25 | /** |
||
26 | * The currency of the monetary unit. |
||
27 | * @var \MoneyMan\Currency |
||
28 | */ |
||
29 | private $currency; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * Sets the immutable amount and currency. |
||
35 | * |
||
36 | * @param int $amount |
||
37 | * @param \MoneyMan\Currency $currency |
||
38 | */ |
||
39 | 32 | public function __construct($amount, Currency $currency) |
|
50 | |||
51 | /** |
||
52 | * Get the currency of the monetary unit. |
||
53 | * |
||
54 | * @return \MoneyMan\Currency |
||
55 | */ |
||
56 | 30 | public function getCurrency() |
|
60 | |||
61 | /** |
||
62 | * Get the amount of the monetary unit. |
||
63 | * |
||
64 | * @return int |
||
65 | */ |
||
66 | 26 | public function getAmount() |
|
70 | |||
71 | /** |
||
72 | * Get the amount and currency code in a human |
||
73 | * readable format. |
||
74 | * |
||
75 | * e.g. (new \MoneyMan\Money(800, new \MoneyMan\Currency('USD')))->getFormatted(); // => "$8.00" |
||
76 | * |
||
77 | * @param string $locale The locale to format the output to. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 8 | public function getFormatted($locale = 'en_US') |
|
94 | |||
95 | /** |
||
96 | * Adds two \MoneyMan\Money objects together by combining the amounts and |
||
97 | * returning a new \MoneyMan\Money object. |
||
98 | * |
||
99 | * This method can only add two money objects of the same \MoneyMan\Currency |
||
100 | * type. |
||
101 | * |
||
102 | * @param \MoneyMan\Money $money |
||
103 | * |
||
104 | * @return \MoneyMan\Money |
||
105 | * |
||
106 | * @throws \MoneyMan\Exception\CannotAddDifferentCurrenciesException |
||
107 | */ |
||
108 | 7 | View Code Duplication | public function add(Money $money) |
126 | |||
127 | /** |
||
128 | * Subtracts the incoming \MoneyMan\Money object's amount from this object. |
||
129 | * |
||
130 | * This method can only add two money objects of the same \MoneyMan\Currency |
||
131 | * type. |
||
132 | * |
||
133 | * @param \MoneyMan\Money $money |
||
134 | * |
||
135 | * @return \MoneyMan\Money |
||
136 | * |
||
137 | * @throws \MoneyMan\Exception\CannotSubtractDifferentCurrenciesException |
||
138 | */ |
||
139 | 7 | View Code Duplication | public function subtract(Money $money) |
157 | |||
158 | /** |
||
159 | * Determine if this \MoneyMan\Money object has the same currency as another |
||
160 | * \MoneyMan\Money object. |
||
161 | * |
||
162 | * @param \MoneyMan\Money $money The money object to compare against. |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | 16 | public function hasSameCurrencyAs(Money $money) |
|
170 | } |
||
171 |
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.