Complex classes like DecimalMath 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 DecimalMath, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DecimalMath { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Whether to use the bcmath library. |
||
| 28 | * |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private $useBC; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param bool|null $useBC Whether to use the bcmath library. If null, |
||
| 35 | * bcmath will automatically be used if available. |
||
| 36 | */ |
||
| 37 | public function __construct( $useBC = null ) { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Whether this is using the bcmath library. |
||
| 47 | * |
||
| 48 | * @return bool |
||
| 49 | */ |
||
| 50 | public function getUseBC() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the product of the two values. |
||
| 56 | * |
||
| 57 | * @param DecimalValue $a |
||
| 58 | * @param DecimalValue $b |
||
| 59 | * |
||
| 60 | * @return DecimalValue |
||
| 61 | */ |
||
| 62 | public function product( DecimalValue $a, DecimalValue $b ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the sum of the two values. |
||
| 75 | * |
||
| 76 | * @param DecimalValue $a |
||
| 77 | * @param DecimalValue $b |
||
| 78 | * |
||
| 79 | * @return DecimalValue |
||
| 80 | */ |
||
| 81 | public function sum( DecimalValue $a, DecimalValue $b ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the minimum of the two values |
||
| 94 | * |
||
| 95 | * @param DecimalValue $a |
||
| 96 | * @param DecimalValue $b |
||
| 97 | * |
||
| 98 | * @return DecimalValue |
||
| 99 | */ |
||
| 100 | public function min( DecimalValue $a, DecimalValue $b ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the maximum of the two values |
||
| 116 | * |
||
| 117 | * @param DecimalValue $a |
||
| 118 | * @param DecimalValue $b |
||
| 119 | * |
||
| 120 | * @return DecimalValue |
||
| 121 | */ |
||
| 122 | public function max( DecimalValue $a, DecimalValue $b ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 138 | * |
||
| 139 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 140 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 141 | * |
||
| 142 | * @since 0.1 |
||
| 143 | * |
||
| 144 | * @param DecimalValue $decimal |
||
| 145 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
| 146 | * but not counting the leading sign. |
||
| 147 | * |
||
| 148 | * @throws InvalidArgumentException |
||
| 149 | * @return DecimalValue |
||
| 150 | */ |
||
| 151 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 159 | * |
||
| 160 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 161 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 162 | * |
||
| 163 | * @since 0.1 |
||
| 164 | * |
||
| 165 | * @param DecimalValue $decimal |
||
| 166 | * @param int $significantExponent The exponent of the last significant digit, |
||
| 167 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
| 168 | * "zero the last two digits before the decimal point". |
||
| 169 | * |
||
| 170 | * @throws InvalidArgumentException |
||
| 171 | * @return DecimalValue |
||
| 172 | */ |
||
| 173 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the (zero based) position for the given exponent in |
||
| 182 | * the given decimal string, counting the decimal point and the leading sign. |
||
| 183 | * |
||
| 184 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
| 185 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
| 186 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
| 187 | * |
||
| 188 | * @param int $exponent |
||
| 189 | * @param DecimalValue $decimal |
||
| 190 | * |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 217 | * |
||
| 218 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 219 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 220 | * |
||
| 221 | * @see round() |
||
| 222 | * |
||
| 223 | * @param string $value |
||
| 224 | * @param int $significantDigits |
||
| 225 | * |
||
| 226 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | private function roundDigits( $value, $significantDigits ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Extracts the next character to add to the result of a rounding run: |
||
| 275 | * $value[$] will be examined and processed in order to determine the next |
||
| 276 | * character to prepend to the result (returned in the $nextCharacter field). |
||
| 277 | * |
||
| 278 | * Updated values for the parameters are returned as well as the next |
||
| 279 | * character. |
||
| 280 | * |
||
| 281 | * @param string $value |
||
| 282 | * @param int $i |
||
| 283 | * @param bool $inIntPart |
||
| 284 | * |
||
| 285 | * @return array ( $value, $i, $inIntPart, $nextCharacter ) |
||
| 286 | */ |
||
| 287 | private function roundNextDigit( $value, $i, $inIntPart ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Bumps the last digit of a value that is being processed for rounding while taking |
||
| 315 | * care of edge cases and updating the state of the rounding process. |
||
| 316 | * |
||
| 317 | * - $value is truncated to $i digits, so we can safely increment (bump) the last digit. |
||
| 318 | * - if the last character of $value is '.', it's trimmed (and $inIntPart is set to true) |
||
| 319 | * to handle the transition from the fractional to the integer part of $value. |
||
| 320 | * - the last digit of $value is bumped using bumpDigits() - this is where the magic happens. |
||
| 321 | * - $i is set to strln( $value ) to make the index consistent in case a trailing decimal |
||
| 322 | * point got removed. |
||
| 323 | * |
||
| 324 | * Updated values for the parameters are returned. |
||
| 325 | * Note: when returning, $i is always one greater than the greatest valid index in $value. |
||
| 326 | * |
||
| 327 | * @param string $value |
||
| 328 | * @param int $i |
||
| 329 | * @param bool $inIntPart |
||
| 330 | * |
||
| 331 | * @return array ( $value, $i, $inIntPart, $next ) |
||
| 332 | */ |
||
| 333 | private function bumpDigitsForRounding( $value, $i, $inIntPart ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Increment the least significant digit by one if it is less than 9, and |
||
| 352 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 353 | * Exception: bump( 0 ) == 1; |
||
| 354 | * |
||
| 355 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
| 356 | * |
||
| 357 | * This is the inverse of @see slump() |
||
| 358 | * |
||
| 359 | * @since 0.1 |
||
| 360 | * |
||
| 361 | * @param DecimalValue $decimal |
||
| 362 | * |
||
| 363 | * @return DecimalValue |
||
| 364 | */ |
||
| 365 | public function bump( DecimalValue $decimal ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Increment the least significant digit by one if it is less than 9, and |
||
| 373 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 374 | * |
||
| 375 | * @see bump() |
||
| 376 | * |
||
| 377 | * @param string $value |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function bumpDigits( $value ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 411 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 412 | * Exception: slump( 0 ) == -1; |
||
| 413 | * |
||
| 414 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
| 415 | * |
||
| 416 | * This is the inverse of @see bump() |
||
| 417 | * |
||
| 418 | * @since 0.1 |
||
| 419 | * |
||
| 420 | * @param DecimalValue $decimal |
||
| 421 | * |
||
| 422 | * @return DecimalValue |
||
| 423 | */ |
||
| 424 | public function slump( DecimalValue $decimal ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 432 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 433 | * |
||
| 434 | * @see slump() |
||
| 435 | * |
||
| 436 | * @param string $value |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | private function slumpDigits( $value ) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $digits |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | private function stripLeadingZeros( $digits ) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Shift the decimal point according to the given exponent. |
||
| 493 | * |
||
| 494 | * @param DecimalValue $decimal |
||
| 495 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
| 496 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
| 497 | * |
||
| 498 | * @throws InvalidArgumentException |
||
| 499 | * @return DecimalValue |
||
| 500 | */ |
||
| 501 | public function shift( DecimalValue $decimal, $exponent ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param string $intPart |
||
| 528 | * @param int $exponent must be negative |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | private function shiftLeft( $intPart, $exponent ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $fractPart |
||
| 545 | * @param int $exponent must be positive |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | private function shiftRight( $fractPart, $exponent ) { |
||
| 559 | |||
| 560 | } |
||
| 561 |