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 |
||
| 26 | class Money |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * number of decimal places to be added to currencies for internal computations, |
||
| 31 | * but removed before any output or formatting is applied. |
||
| 32 | * This allows us to avoid rounding errors during calculations. |
||
| 33 | */ |
||
| 34 | const EXTRA_PRECISION = 3; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int $amount |
||
| 38 | */ |
||
| 39 | private $amount; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Currency $currency |
||
| 43 | */ |
||
| 44 | private $currency; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Calculator $calculator |
||
| 48 | */ |
||
| 49 | protected $calculator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var MoneyFormatter[] $formatters |
||
| 53 | */ |
||
| 54 | protected $formatters; |
||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Money constructor. |
||
| 60 | * |
||
| 61 | * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
||
| 62 | * example: $12.5 USD would equate to a value amount of 12.50 |
||
| 63 | * @param Currency $currency |
||
| 64 | * @param Calculator $calculator |
||
| 65 | * @param MoneyFormatter[] $formatters |
||
| 66 | * @throws InvalidDataTypeException |
||
| 67 | */ |
||
| 68 | public function __construct($amount, Currency $currency, Calculator $calculator, array $formatters) |
||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * @return Calculator |
||
| 80 | */ |
||
| 81 | protected function calculator() |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @return MoneyFormatter[] |
||
| 90 | */ |
||
| 91 | protected function formatters() |
||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
||
| 100 | * example: $12.5 USD would equate to a value amount of 12.50 |
||
| 101 | * @return float|int|number|string |
||
| 102 | * @throws InvalidDataTypeException |
||
| 103 | */ |
||
| 104 | private function parseAmount($amount) |
||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant |
||
| 148 | * |
||
| 149 | * @param bool $positive |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | private function precision($positive = true) |
||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the money amount as an unformatted string |
||
| 162 | * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format() |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function amount() |
||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the money SUBUNITS amount as an INTEGER |
||
| 180 | * |
||
| 181 | * @return integer |
||
| 182 | */ |
||
| 183 | public function amountInSubunits() |
||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * applies formatting based on the specified formatting level |
||
| 199 | * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter |
||
| 200 | * |
||
| 201 | * @param int $formatting_level |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS) |
||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns the Currency object for this money |
||
| 225 | * |
||
| 226 | * @return Currency |
||
| 227 | */ |
||
| 228 | public function currency() |
||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * adds the supplied Money amount to this Money amount |
||
| 237 | * and returns a new Money object |
||
| 238 | * |
||
| 239 | * @param Money $other |
||
| 240 | * @return Money |
||
| 241 | * @throws InvalidArgumentException |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function add(Money $other) |
|
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * subtracts the supplied Money amount from this Money amount |
||
| 261 | * and returns a new Money object |
||
| 262 | * |
||
| 263 | * @param Money $other |
||
| 264 | * @return Money |
||
| 265 | * @throws InvalidArgumentException |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function subtract(Money $other) |
|
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * multiplies this Money amount by the supplied $multiplier |
||
| 285 | * and returns a new Money object |
||
| 286 | * |
||
| 287 | * @param float|int|string $multiplier |
||
| 288 | * @param int $rounding_mode |
||
| 289 | * @return Money |
||
| 290 | * @throws InvalidDataTypeException |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * divides this Money amount by the supplied $divisor |
||
| 311 | * and returns a new Money object |
||
| 312 | * |
||
| 313 | * @param float|int|string $divisor |
||
| 314 | * @param int $rounding_mode |
||
| 315 | * @return Money |
||
| 316 | * @throws InvalidDataTypeException |
||
| 317 | */ |
||
| 318 | View Code Duplication | public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP) |
|
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @param Currency $other_currency |
||
| 337 | * @throws InvalidArgumentException |
||
| 338 | */ |
||
| 339 | public function verifySameCurrency(Currency $other_currency) |
||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function __toString() |
||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * factory method that returns a Money object for the currency specified as if it were a class method |
||
| 365 | * example: Money::USD(12.5); |
||
| 366 | * money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
||
| 367 | * example: $12.5 USD would equate to a value amount of 12.50 |
||
| 368 | * |
||
| 369 | * @param string $currency_code |
||
| 370 | * @param array $arguments |
||
| 371 | * @return Money |
||
| 372 | * @throws InvalidArgumentException |
||
| 373 | * @throws InvalidDataTypeException |
||
| 374 | * @throws EE_Error |
||
| 375 | * @throws InvalidInterfaceException |
||
| 376 | */ |
||
| 377 | public static function __callStatic($currency_code, $arguments) |
||
| 386 | |||
| 387 | |||
| 388 | } |
||
| 389 | // End of file Money.php |
||
| 391 |