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 |
||
21 | class Money |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * number of decimal places to be added to currencies for internal computations, |
||
26 | * but removed before any output or formatting is applied. |
||
27 | * This allows us to avoid rounding errors during calculations. |
||
28 | */ |
||
29 | const EXTRA_PRECISION = 3; |
||
30 | |||
31 | /** |
||
32 | * @var int $amount |
||
33 | */ |
||
34 | private $amount; |
||
35 | |||
36 | /** |
||
37 | * @var Currency $currency |
||
38 | */ |
||
39 | private $currency; |
||
40 | |||
41 | /** |
||
42 | * @var Calculator $calculator |
||
43 | */ |
||
44 | protected $calculator; |
||
45 | |||
46 | |||
47 | |||
48 | /** |
||
49 | * Money constructor. |
||
50 | * |
||
51 | * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
||
52 | * example: $12.5 USD would equate to a value amount of 12.50 |
||
53 | * @param Currency $currency |
||
54 | * @param Calculator $calculator |
||
55 | * @throws InvalidDataTypeException |
||
56 | */ |
||
57 | public function __construct($amount, Currency $currency, Calculator $calculator) |
||
58 | { |
||
59 | $this->currency = $currency; |
||
60 | $this->amount = $this->parseAmount($amount); |
||
61 | $this->calculator = $calculator; |
||
62 | } |
||
63 | |||
64 | |||
65 | |||
66 | /** |
||
67 | * @return Calculator |
||
68 | */ |
||
69 | protected function calculator() |
||
70 | { |
||
71 | return $this->calculator; |
||
72 | } |
||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * Convert's a standard unit amount into the subunits of the currency |
||
78 | * |
||
79 | * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
||
80 | * example: $12.5 USD would equate to a value amount of 12.50 |
||
81 | * @return integer in the currency's subunit |
||
82 | * @throws InvalidDataTypeException |
||
83 | */ |
||
84 | private function parseAmount($amount) |
||
85 | { |
||
86 | if (! in_array(gettype($amount), array('integer', 'double', 'string'), true)) { |
||
87 | throw new InvalidDataTypeException( |
||
88 | '$amount', |
||
89 | $amount, |
||
90 | 'integer (or float or string)' |
||
91 | ); |
||
92 | } |
||
93 | if ($this->currency->decimalMark() !== '.') { |
||
94 | // remove thousands separator and replace decimal place with standard decimal. |
||
95 | $amount = str_replace( |
||
96 | array( |
||
97 | $this->currency->thousands(), |
||
98 | $this->currency->decimalMark(), |
||
99 | ), |
||
100 | array( |
||
101 | '', |
||
102 | '.', |
||
103 | ), |
||
104 | $amount |
||
105 | ); |
||
106 | } |
||
107 | // remove any non numeric values but leave the decimal |
||
108 | $amount = (float) preg_replace('/([^0-9\\.-])/', '', $amount); |
||
109 | // maybe convert the incoming decimal amount to the currencies subunits |
||
110 | // ex: 12.5 for a currency with 100 subunits would become 1250 |
||
111 | if ($this->currency()->subunits()) { |
||
112 | $amount *= $this->currency()->subunits(); |
||
113 | } |
||
114 | // then shift the decimal position by the number of decimal places used internally |
||
115 | // so if our extra internal precision was 3, it would become 1250000 |
||
116 | $amount *= pow(10, $this->precision()); |
||
117 | // then round up the remaining value if there is still a fractional amount left |
||
118 | $amount = round($amount); |
||
119 | return (int)$amount; |
||
120 | } |
||
121 | |||
122 | |||
123 | |||
124 | /** |
||
125 | * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
||
126 | * |
||
127 | * @param bool $adding_precision if true, will move the decimal to the right (increase amount) |
||
128 | * if false, will move the decimal to the left (decrease amount) |
||
129 | * @return integer |
||
130 | */ |
||
131 | private function precision($adding_precision = true) |
||
132 | { |
||
133 | $sign = $adding_precision ? 1 : -1; |
||
134 | return Money::EXTRA_PRECISION * $sign; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns the money amount as an unformatted float |
||
139 | * @return float |
||
140 | */ |
||
141 | public function floatAmount() |
||
142 | { |
||
143 | // shift the decimal position BACK by the number of decimal places used internally |
||
144 | // ex: if our extra internal precision was 3, then 1250000 would become 1250 |
||
145 | $amount = $this->amount * pow(10, $this->precision(false)); |
||
146 | // then maybe adjust for the currencies subunits |
||
147 | // ex: 1250 for a currency with 100 subunits would become 12.50 |
||
148 | if ($this->currency()->subunits()) { |
||
149 | $amount /= $this->currency()->subunits(); |
||
150 | } |
||
151 | return $amount; |
||
152 | } |
||
153 | |||
154 | |||
155 | |||
156 | /** |
||
157 | * Returns the money amount as an unformatted string |
||
158 | * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
||
159 | * In the currency's standard units |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | public function amount() |
||
164 | { |
||
165 | // shave off our extra internal precision using the number of decimal places for the currency |
||
166 | return (string) round($this->floatAmount(), $this->currency()->decimalPlaces()); |
||
167 | } |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * Returns the money SUBUNITS amount as an INTEGER |
||
173 | * |
||
174 | * @return integer |
||
175 | */ |
||
176 | public function amountInSubunits() |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * Returns the Currency object for this money |
||
192 | * |
||
193 | * @return Currency |
||
194 | */ |
||
195 | public function currency() |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * adds the supplied Money amount to this Money amount |
||
204 | * and returns a new Money object |
||
205 | * |
||
206 | * @param Money $other |
||
207 | * @return Money |
||
208 | * @throws InvalidArgumentException |
||
209 | */ |
||
210 | View Code Duplication | public function add(Money $other) |
|
222 | |||
223 | |||
224 | |||
225 | /** |
||
226 | * subtracts the supplied Money amount from this Money amount |
||
227 | * and returns a new Money object |
||
228 | * |
||
229 | * @param Money $other |
||
230 | * @return Money |
||
231 | * @throws InvalidArgumentException |
||
232 | */ |
||
233 | View Code Duplication | public function subtract(Money $other) |
|
245 | |||
246 | |||
247 | /** |
||
248 | * multiplies this Money amount by the supplied $multiplier |
||
249 | * and returns a new Money object |
||
250 | * |
||
251 | * @param float|int|string $multiplier |
||
252 | * @param int $rounding_mode |
||
253 | * @return Money |
||
254 | * @throws InvalidDataTypeException |
||
255 | */ |
||
256 | View Code Duplication | public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
269 | |||
270 | |||
271 | /** |
||
272 | * divides this Money amount by the supplied $divisor |
||
273 | * and returns a new Money object |
||
274 | * |
||
275 | * @param float|int|string $divisor |
||
276 | * @param int $rounding_mode |
||
277 | * @return Money |
||
278 | * @throws InvalidDataTypeException |
||
279 | */ |
||
280 | View Code Duplication | public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
293 | |||
294 | |||
295 | |||
296 | /** |
||
297 | * @param Currency $other_currency |
||
298 | * @throws InvalidArgumentException |
||
299 | */ |
||
300 | public function verifySameCurrency(Currency $other_currency) |
||
311 | |||
312 | |||
313 | |||
314 | /** |
||
315 | * @return string |
||
316 | */ |
||
317 | public function __toString() |
||
321 | } |
||
322 |