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 Currency |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * eg 'US' |
||
| 28 | * |
||
| 29 | * @var string $code |
||
| 30 | */ |
||
| 31 | private $code; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Label $label |
||
| 35 | */ |
||
| 36 | private $label; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * currency sign |
||
| 40 | * |
||
| 41 | * @var string $sign |
||
| 42 | * eg '$' |
||
| 43 | */ |
||
| 44 | private $sign; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Whether the currency sign should come before the number or not |
||
| 48 | * |
||
| 49 | * @var boolean $sign_b4 |
||
| 50 | */ |
||
| 51 | private $sign_b4; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * How many digits should come after the decimal place |
||
| 55 | * |
||
| 56 | * @var int $decimal_places |
||
| 57 | */ |
||
| 58 | private $decimal_places; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Symbol to use for decimal mark |
||
| 62 | * |
||
| 63 | * @var string $decimal_mark |
||
| 64 | * eg '.' |
||
| 65 | */ |
||
| 66 | private $decimal_mark; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Symbol to use for thousands |
||
| 70 | * |
||
| 71 | * @var string $thousands |
||
| 72 | * eg ',' |
||
| 73 | */ |
||
| 74 | private $thousands; |
||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Currency constructor. |
||
| 80 | * |
||
| 81 | * @param string $code |
||
| 82 | * @param Label $label |
||
| 83 | * @param string $sign |
||
| 84 | * @param bool $sign_b4 |
||
| 85 | * @param int $decimal_places |
||
| 86 | * @param string $decimal_mark |
||
| 87 | * @param string $thousands |
||
| 88 | */ |
||
| 89 | private function __construct($code, Label $label, $sign, $sign_b4, $decimal_places, $decimal_mark, $thousands) |
||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * returns a Currency object for the supplied country code |
||
| 104 | * |
||
| 105 | * @param string $CNT_ISO |
||
| 106 | * @return Currency |
||
| 107 | * @throws InvalidArgumentException |
||
| 108 | */ |
||
| 109 | View Code Duplication | public static function createFromCountryCode($CNT_ISO) |
|
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * returns a Currency object for the supplied currency code |
||
| 142 | * PLZ NOTE: variations may exist between how different countries display the same currency, |
||
| 143 | * so it may be necessary to use Currency::createFromCountryCode() to achieve the desired results |
||
| 144 | * |
||
| 145 | * @param string $code |
||
| 146 | * @return Currency |
||
| 147 | * @throws InvalidArgumentException |
||
| 148 | * @throws EE_Error |
||
| 149 | */ |
||
| 150 | View Code Duplication | public static function createFromCode($code) |
|
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * returns true if this currency is the same as the supplied currency |
||
| 183 | * |
||
| 184 | * @param Currency $other |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function equals(Currency $other){ |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function code() |
||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function name() |
||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function plural() |
||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function sign() |
||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function signB4() |
||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function decimalPlaces() |
||
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function decimalMark() |
||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function thousands() |
||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function __toString() |
||
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | } |
||
| 284 | // End of file Currency.php |
||
| 285 | // Location: core/entities/money/Currency.php |
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.