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 |
||
| 23 | class Money { |
||
| 24 | /** |
||
| 25 | * Amount value. |
||
| 26 | * |
||
| 27 | * @var float |
||
| 28 | */ |
||
| 29 | private $value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Currency. |
||
| 33 | * |
||
| 34 | * @var Currency |
||
| 35 | */ |
||
| 36 | private $currency; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Calculator. |
||
| 40 | * |
||
| 41 | * @var Calculator|null |
||
| 42 | */ |
||
| 43 | private static $calculator; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Calculators. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $calculators = [ |
||
| 51 | BcMathCalculator::class, |
||
| 52 | PhpCalculator::class, |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Construct and initialize money object. |
||
| 57 | * |
||
| 58 | * @param string|int|float $value Amount value. |
||
| 59 | * @param Currency|string $currency Currency. |
||
| 60 | */ |
||
| 61 | 74 | public function __construct( $value = 0, $currency = 'EUR' ) { |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Get default format. |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | 17 | public static function get_default_format() { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Format i18n. |
||
| 82 | * |
||
| 83 | * @param string|null $format Format. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 14 | public function format_i18n( $format = null ) { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Format i18n without trailing zeros. |
||
| 121 | * |
||
| 122 | * @param string|null $format Format. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 7 | public function format_i18n_non_trailing_zeros( $format = null ) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Format. |
||
| 138 | * |
||
| 139 | * @param string|null $format Format. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 7 | public function format( $format = null ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get value. |
||
| 164 | * |
||
| 165 | * @return float Amount value. |
||
| 166 | */ |
||
| 167 | 74 | public function get_value() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Get amount. |
||
| 173 | * |
||
| 174 | * @deprecated 1.2.0 |
||
| 175 | * @return float Amount value. |
||
| 176 | */ |
||
| 177 | public function get_amount() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get cents. |
||
| 185 | * |
||
| 186 | * @return float |
||
| 187 | * |
||
| 188 | * @deprecated 1.2.2 Use `Money::get_minor_units()` instead. |
||
| 189 | */ |
||
| 190 | 1 | public function get_cents() { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get amount in minor units. |
||
| 196 | * |
||
| 197 | * Examples for value 10: |
||
| 198 | * JPY 0 decimals: 10 |
||
| 199 | * EUR 2 decimals: 1000 |
||
| 200 | * BHD 3 decimals: 10000 |
||
| 201 | * NLG 4 decimals: 100000 |
||
| 202 | * |
||
| 203 | * @since 1.2.1 |
||
| 204 | * |
||
| 205 | * @return int |
||
| 206 | */ |
||
| 207 | 21 | public function get_minor_units() { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set value. |
||
| 217 | * |
||
| 218 | * @param mixed $value Amount value. |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | 74 | public function set_value( $value ) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set amount. |
||
| 227 | * |
||
| 228 | * @deprecated 1.2.0 |
||
| 229 | * @param mixed $value Amount value. |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function set_amount( $value ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get currency. |
||
| 240 | * |
||
| 241 | * @return Currency |
||
| 242 | */ |
||
| 243 | 8 | public function get_currency() { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set currency. |
||
| 249 | * |
||
| 250 | * @param string|Currency $currency Currency. |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | 74 | public function set_currency( $currency ) { |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Create a string representation of this money object. |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function __toString() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns a new Money object that represents |
||
| 274 | * the sum of this and an other Money object. |
||
| 275 | * |
||
| 276 | * @param Money $addend Addend. |
||
| 277 | * |
||
| 278 | * @return Money |
||
| 279 | */ |
||
| 280 | 1 | View Code Duplication | public function add( Money $addend ) { |
| 289 | |||
| 290 | /** |
||
| 291 | * Returns a new Money object that represents |
||
| 292 | * the difference of this and an other Money object. |
||
| 293 | * |
||
| 294 | * @link https://github.com/moneyphp/money/blob/v3.2.1/src/Money.php#L235-L255 |
||
| 295 | * |
||
| 296 | * @param Money $subtrahend Subtrahend. |
||
| 297 | * |
||
| 298 | * @return Money |
||
| 299 | */ |
||
| 300 | View Code Duplication | public function subtract( Money $subtrahend ) { |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Initialize calculator. |
||
| 312 | * |
||
| 313 | * @return Calculator |
||
| 314 | * |
||
| 315 | * @throws \RuntimeException If cannot find calculator for money calculations. |
||
| 316 | */ |
||
| 317 | 1 | private static function initialize_calculator() { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get calculator. |
||
| 335 | * |
||
| 336 | * @return Calculator |
||
| 337 | */ |
||
| 338 | 22 | protected function get_calculator() { |
|
| 345 | } |
||
| 346 |
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.