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:
Complex classes like DecimalFormatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DecimalFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class DecimalFormatter |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string The format string given by the user |
||
| 42 | */ |
||
| 43 | protected $originalFormatString = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string The format string which will be given to sprintf |
||
| 47 | */ |
||
| 48 | protected $formatString = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string The format string which will be given to sprintf if the |
||
| 52 | * number is negative |
||
| 53 | */ |
||
| 54 | protected $negativeFormatString = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int The minimum number of integrals displayed (will be padded |
||
| 58 | * with 0 on the left) |
||
| 59 | */ |
||
| 60 | protected $minShowedIntegrals = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var int The minimum number of fractionals displayed (will be |
||
| 64 | * padded with 0 on the right) |
||
| 65 | */ |
||
| 66 | protected $minShowedFractionals = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int The maximum number of fractionals displayed |
||
| 70 | * (-1 means all get displayed) |
||
| 71 | */ |
||
| 72 | protected $maxShowedFractionals = 0; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool Whether the format string has the location of the minus |
||
| 76 | * defined |
||
| 77 | */ |
||
| 78 | protected $hasMinus = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool Whether the format string has the location of the |
||
| 82 | * currency sign defined |
||
| 83 | */ |
||
| 84 | protected $hasCurrency = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int The type of the currency symbol. |
||
| 88 | */ |
||
| 89 | protected $currencyType = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array An array containing the distances for the grouping |
||
| 93 | * operators which will be applied to the number |
||
| 94 | */ |
||
| 95 | protected $groupingDistances = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string The grouping(thousands) separator |
||
| 99 | */ |
||
| 100 | protected $groupingSeparator = ','; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string The decimal separator |
||
| 104 | */ |
||
| 105 | protected $decimalSeparator = '.'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var int The rounding mode |
||
| 109 | */ |
||
| 110 | protected $roundingMode = DecimalFormatter::ROUND_SCIENTIFIC; |
||
| 111 | |||
| 112 | const CURRENCY_SYMBOL = 1; |
||
| 113 | const CURRENCY_CODE = 2; |
||
| 114 | const CURRENCY_NAME = 3; |
||
| 115 | |||
| 116 | const ROUND_NONE = 0; |
||
| 117 | const ROUND_SCIENTIFIC = 1; |
||
| 118 | const ROUND_FINANCIAL = 2; |
||
| 119 | const ROUND_FLOOR = 3; |
||
| 120 | const ROUND_CEIL = 4; |
||
| 121 | |||
| 122 | const IN_PREFIX = 1; |
||
| 123 | const IN_NUMBER = 2; |
||
| 124 | const IN_POSTFIX = 3; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Constructs a new Decimalformatter with the optional format. |
||
| 128 | * |
||
| 129 | * @param string $format The format (if any). |
||
| 130 | * |
||
| 131 | * @author Dominik del Bondio <[email protected]> |
||
| 132 | * @since 0.11.0 |
||
| 133 | */ |
||
| 134 | public function __construct($format = null) |
||
| 135 | { |
||
| 136 | if ($format !== null) { |
||
| 137 | $this->setFormat($format); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the format which is currently used to format numbers. |
||
| 143 | * |
||
| 144 | * @return string The current format. |
||
| 145 | * |
||
| 146 | * @author Dominik del Bondio <[email protected]> |
||
| 147 | * @since 0.11.0 |
||
| 148 | */ |
||
| 149 | public function getFormat() |
||
| 150 | { |
||
| 151 | return $this->originalFormatString; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the format to be used for formatting numbers. |
||
| 156 | * |
||
| 157 | * @return string The current format. |
||
| 158 | * |
||
| 159 | * @author Dominik del Bondio <[email protected]> |
||
| 160 | * @since 0.11.0 |
||
| 161 | */ |
||
| 162 | public function setFormat($format) |
||
| 163 | { |
||
| 164 | if ($this->originalFormatString == $format) { |
||
| 165 | // the given and the currently set format string are equal so we have nothing to do |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->originalFormatString = $format; |
||
| 170 | |||
| 171 | if (($pos = strpos($format, ';')) !== false) { |
||
| 172 | $fullFormat = $format; |
||
| 173 | $format = substr($fullFormat, 0, $pos); |
||
| 174 | $negativeFormat = substr($fullFormat, $pos + 1); |
||
| 175 | } else { |
||
| 176 | $fullFormat = $format; |
||
|
|
|||
| 177 | $negativeFormat = '-#'; |
||
| 178 | } |
||
| 179 | |||
| 180 | $numberChars = array('0', '#', '.', ','); |
||
| 181 | |||
| 182 | $formatStr = ''; |
||
| 183 | |||
| 184 | // an array containing the distances between the grouping operators (and up to the decimals) from left to right |
||
| 185 | $groupingDistances = array(); |
||
| 186 | $currentGroupingDistance = 0; |
||
| 187 | |||
| 188 | $hasMinus = false; |
||
| 189 | $hasCurrency = false; |
||
| 190 | $currencyType = 0; |
||
| 191 | $minShowedIntegrals = 0; |
||
| 192 | $minShowedFractionals = 0; |
||
| 193 | $maxShowedFractionals = 0; |
||
| 194 | $skippedFractionals = 0; |
||
| 195 | $numberState = 'inInteger'; |
||
| 196 | |||
| 197 | $inQuote = false; |
||
| 198 | $quoteStr = ''; |
||
| 199 | $state = self::IN_PREFIX; |
||
| 200 | $len = strlen($format); |
||
| 201 | |||
| 202 | for ($i = 0; $i < $len; ++$i) { |
||
| 203 | $c = $format[$i]; |
||
| 204 | $cNext = (($i + 1) < $len) ? $format[$i + 1] : 0; |
||
| 205 | |||
| 206 | switch ($state) { |
||
| 207 | case self::IN_POSTFIX: |
||
| 208 | case self::IN_PREFIX: { |
||
| 209 | if ($state == self::IN_PREFIX && in_array($c, $numberChars)) { |
||
| 210 | --$i; |
||
| 211 | $state = self::IN_NUMBER; |
||
| 212 | } elseif ($inQuote) { |
||
| 213 | // quote closed |
||
| 214 | if ($c == '\'') { |
||
| 215 | // when the quoted string was empty we need to output a ' |
||
| 216 | if (strlen($quoteStr) == 0) { |
||
| 217 | $quoteStr = '\''; |
||
| 218 | } |
||
| 219 | $formatStr .= $quoteStr; |
||
| 220 | $inQuote = false; |
||
| 221 | } else { |
||
| 222 | // quote % for sprintf usage |
||
| 223 | if ($c == '%') { |
||
| 224 | $c = '%' . $c; |
||
| 225 | } |
||
| 226 | $quoteStr .= $c; |
||
| 227 | } |
||
| 228 | } else { |
||
| 229 | if ($c == '\'') { |
||
| 230 | $quoteStr = ''; |
||
| 231 | $inQuote = true; |
||
| 232 | // } elseif($c == '-') { |
||
| 233 | // $hasMinus = true; |
||
| 234 | // $formatStr .= '%2$s'; |
||
| 235 | } elseif (/*$c == '¤'*/ !$hasCurrency && ord($c) == 194 && ord($cNext) == 164) { |
||
| 236 | ++$i; |
||
| 237 | $hasCurrency = true; |
||
| 238 | $currencyType = self::CURRENCY_SYMBOL; |
||
| 239 | $formatStr .= '%3$s'; |
||
| 240 | |||
| 241 | for (; $i + 2 < $len && ord($format[$i + 1]) == 194 && ord($format[$i + 2]) == 164 && $currencyType < self::CURRENCY_NAME; $i += 2) { |
||
| 242 | ++$currencyType; |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | // quote % for sprintf usage |
||
| 246 | if ($c == '%') { |
||
| 247 | $c = '%' . $c; |
||
| 248 | } |
||
| 249 | $formatStr .= $c; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | case self::IN_NUMBER: { |
||
| 255 | if (!in_array($c, $numberChars)) { |
||
| 256 | if ($numberState == 'inInteger') { |
||
| 257 | $groupingDistances[] = $currentGroupingDistance; |
||
| 258 | } |
||
| 259 | $formatStr .= '%1$s'; |
||
| 260 | --$i; |
||
| 261 | $state = self::IN_POSTFIX; |
||
| 262 | } else { |
||
| 263 | if ($numberState == 'inInteger') { |
||
| 264 | if ($c == ',') { |
||
| 265 | $groupingDistances[] = $currentGroupingDistance; |
||
| 266 | $currentGroupingDistance = 0; |
||
| 267 | } elseif ($c == '.') { |
||
| 268 | $groupingDistances[] = $currentGroupingDistance; |
||
| 269 | // if we have a dot we default to show the entire fractional part |
||
| 270 | $maxShowedFractionals = -1; |
||
| 271 | $numberState = 'inFraction'; |
||
| 272 | } else { |
||
| 273 | // when the user has a pattern like 0##0 the 2 ## are mandatory too |
||
| 274 | // (basically everything after the first 0 is mandatory, so take care here) |
||
| 275 | if ($minShowedIntegrals > 0) { |
||
| 276 | ++$minShowedIntegrals; |
||
| 277 | } elseif ($c == '0') { |
||
| 278 | ++$minShowedIntegrals; |
||
| 279 | } |
||
| 280 | ++$currentGroupingDistance; |
||
| 281 | } |
||
| 282 | } elseif ($numberState == 'inFraction') { |
||
| 283 | if ($c == ',' || $c == '.') { |
||
| 284 | throw new Exception($c. ' is not allowed in the fraction part of the number'); |
||
| 285 | } else { |
||
| 286 | if ($c == '#') { |
||
| 287 | ++$skippedFractionals; |
||
| 288 | } elseif ($c == '0') { |
||
| 289 | ++$minShowedFractionals; |
||
| 290 | $minShowedFractionals += $skippedFractionals; |
||
| 291 | $maxShowedFractionals = $minShowedFractionals; |
||
| 292 | $skippedFractionals = 0; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | break; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | if ($state == self::IN_NUMBER) { |
||
| 304 | if ($numberState == 'inInteger') { |
||
| 305 | $groupingDistances[] = $currentGroupingDistance; |
||
| 306 | } |
||
| 307 | $formatStr .= '%1$s'; |
||
| 308 | } |
||
| 309 | |||
| 310 | // when the user had 0.00# as format (the fractional part ended with an #) |
||
| 311 | // the max numbers of the fractional part is unlimited |
||
| 312 | if ($skippedFractionals) { |
||
| 313 | $maxShowedFractionals = -1; |
||
| 314 | } |
||
| 315 | |||
| 316 | // we chop of the first element of the grouping distance which is |
||
| 317 | // either the the number of chars until the first ',' or the only element |
||
| 318 | // in case there was no grouping separator specified (which means that |
||
| 319 | // there won't be grouping at all) |
||
| 320 | array_shift($groupingDistances); |
||
| 321 | |||
| 322 | // now we reverse the array so we can process it in natural order later |
||
| 323 | $groupingDistances = array_reverse($groupingDistances); |
||
| 324 | |||
| 325 | if (($pos = strpos($negativeFormat, '-')) !== false) { |
||
| 326 | str_replace('-', '%2$s', $negativeFormat); |
||
| 327 | } |
||
| 328 | $hasMinus = true; |
||
| 329 | $negativeFormat = preg_replace('/[' . preg_quote(implode('', $numberChars), '/') . ']+/', $formatStr, $negativeFormat); |
||
| 330 | // replace the currency specifier from the old string if it was specified extra in the negative one |
||
| 331 | if (($pos = strpos($negativeFormat, /*'¤'*/ chr(194) . chr(164))) !== false) { |
||
| 332 | $negativeFormat = str_replace('%3$s', '', $negativeFormat); |
||
| 333 | $negativeFormat = str_replace(chr(194) . chr(164), '%3$s', $negativeFormat); |
||
| 334 | } |
||
| 335 | |||
| 336 | // store all info |
||
| 337 | |||
| 338 | $this->formatString = $formatStr; |
||
| 339 | $this->negativeFormatString = $negativeFormat; |
||
| 340 | |||
| 341 | $this->minShowedIntegrals = $minShowedIntegrals; |
||
| 342 | $this->minShowedFractionals = $minShowedFractionals; |
||
| 343 | $this->maxShowedFractionals = $maxShowedFractionals; |
||
| 344 | |||
| 345 | $this->hasMinus = $hasMinus; |
||
| 346 | $this->hasCurrency = $hasCurrency; |
||
| 347 | $this->currencyType = $currencyType; |
||
| 348 | |||
| 349 | $this->groupingDistances = $groupingDistances; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Formats the given number with the information in this instance. |
||
| 354 | * |
||
| 355 | * @param int|float A number to format. |
||
| 356 | * @param string A currency symbol to be used. |
||
| 357 | * |
||
| 358 | * @return array The number and some information in the desired format. |
||
| 359 | * |
||
| 360 | * @author Dominik del Bondio <[email protected]> |
||
| 361 | * @since 0.11.0 |
||
| 362 | */ |
||
| 363 | protected function prepareNumber($number, $currencySymbol) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Formats the given number and returns the formatted result. |
||
| 533 | * |
||
| 534 | * @param int|float The number to be formatted. |
||
| 535 | * |
||
| 536 | * @return string The number formatted in the desired format. |
||
| 537 | * |
||
| 538 | * @author Dominik del Bondio <[email protected]> |
||
| 539 | * @since 0.11.0 |
||
| 540 | */ |
||
| 541 | public function formatNumber($number) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Formats the given currency and returns the formatted result. |
||
| 548 | * |
||
| 549 | * @param int|float The number to be formatted. |
||
| 550 | * @param string The currency symbol to be used when formatting. |
||
| 551 | * |
||
| 552 | * @return string The currency formatted in the desired format. |
||
| 553 | * |
||
| 554 | * @author Dominik del Bondio <[email protected]> |
||
| 555 | * @since 0.11.0 |
||
| 556 | */ |
||
| 557 | public function formatCurrency($number, $currencySymbol) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Returns the rounding mode. |
||
| 564 | * |
||
| 565 | * @return int The rounding mode. |
||
| 566 | * |
||
| 567 | * @author Dominik del Bondio <[email protected]> |
||
| 568 | * @since 0.11.0 |
||
| 569 | */ |
||
| 570 | public function getRoundingMode() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Sets the rounding mode. |
||
| 577 | * |
||
| 578 | * @return string The rounding mode. |
||
| 579 | * |
||
| 580 | * @author Dominik del Bondio <[email protected]> |
||
| 581 | * @since 0.11.0 |
||
| 582 | */ |
||
| 583 | public function setRoundingMode($mode) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Maps a string rounding mode definition to the rounding mode constants. |
||
| 590 | * |
||
| 591 | * @param string The mode string. |
||
| 592 | * |
||
| 593 | * @return string The rounding mode constant. |
||
| 594 | * |
||
| 595 | * @author Dominik del Bondio <[email protected]> |
||
| 596 | * @since 0.11.0 |
||
| 597 | */ |
||
| 598 | public function getRoundingModeFromString($mode) |
||
| 614 | |||
| 615 | protected static function getDecimalParseRegex(Locale $locale = null) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Parses a string into float or int. |
||
| 695 | * |
||
| 696 | * @param string The input number string. |
||
| 697 | * @param Locale An optional locale to get the separators from. |
||
| 698 | * @param bool An out value indicating whether there were additional |
||
| 699 | * characters after the matched number. |
||
| 700 | * |
||
| 701 | * @return mixed The result if parsing was successful or false when the |
||
| 702 | * input was no number. |
||
| 703 | * |
||
| 704 | * @author Dominik del Bondio <[email protected]> |
||
| 705 | * @since 0.11.0 |
||
| 706 | */ |
||
| 707 | public static function parse($string, $locale = null, &$hasExtraChars = false) |
||
| 763 | } |
||
| 764 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.