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 ) { |
||
| 38 | if ( $useBC === null ) { |
||
| 39 | $useBC = function_exists( 'bcscale' ); |
||
| 40 | } |
||
| 41 | |||
| 42 | $this->useBC = $useBC; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int|float|string $number |
||
| 47 | * |
||
| 48 | * @return DecimalValue |
||
| 49 | */ |
||
| 50 | private function makeDecimalValue( $number ) { |
||
| 51 | |||
| 52 | if ( is_string( $number ) && $number !== '' ) { |
||
| 53 | if ( $number[0] !== '-' && $number[0] !== '+' ) { |
||
| 54 | $number = '+' . $number; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | return new DecimalValue( $number ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Whether this is using the bcmath library. |
||
| 63 | * |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | public function getUseBC() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the product of the two values. |
||
| 72 | * |
||
| 73 | * @param DecimalValue $a |
||
| 74 | * @param DecimalValue $b |
||
| 75 | * |
||
| 76 | * @return DecimalValue |
||
| 77 | */ |
||
| 78 | public function product( DecimalValue $a, DecimalValue $b ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the sum of the two values. |
||
| 91 | * |
||
| 92 | * @param DecimalValue $a |
||
| 93 | * @param DecimalValue $b |
||
| 94 | * |
||
| 95 | * @return DecimalValue |
||
| 96 | */ |
||
| 97 | public function sum( DecimalValue $a, DecimalValue $b ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the minimum of the two values |
||
| 110 | * |
||
| 111 | * @param DecimalValue $a |
||
| 112 | * @param DecimalValue $b |
||
| 113 | * |
||
| 114 | * @return DecimalValue |
||
| 115 | */ |
||
| 116 | public function min( DecimalValue $a, DecimalValue $b ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the maximum of the two values |
||
| 132 | * |
||
| 133 | * @param DecimalValue $a |
||
| 134 | * @param DecimalValue $b |
||
| 135 | * |
||
| 136 | * @return DecimalValue |
||
| 137 | */ |
||
| 138 | public function max( DecimalValue $a, DecimalValue $b ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 154 | * |
||
| 155 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 156 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 157 | * |
||
| 158 | * @since 0.1 |
||
| 159 | * |
||
| 160 | * @param DecimalValue $decimal |
||
| 161 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
| 162 | * but not counting the leading sign. |
||
| 163 | * |
||
| 164 | * @throws InvalidArgumentException |
||
| 165 | * @return DecimalValue |
||
| 166 | */ |
||
| 167 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 175 | * |
||
| 176 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 177 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 178 | * |
||
| 179 | * @since 0.1 |
||
| 180 | * |
||
| 181 | * @param DecimalValue $decimal |
||
| 182 | * @param int $significantExponent The exponent of the last significant digit, |
||
| 183 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
| 184 | * "zero the last two digits before the decimal point". |
||
| 185 | * |
||
| 186 | * @throws InvalidArgumentException |
||
| 187 | * @return DecimalValue |
||
| 188 | */ |
||
| 189 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the (zero based) position for the given exponent in |
||
| 198 | * the given decimal string, counting the decimal point and the leading sign. |
||
| 199 | * |
||
| 200 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
| 201 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
| 202 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
| 203 | * |
||
| 204 | * @param int $exponent |
||
| 205 | * @param DecimalValue $decimal |
||
| 206 | * |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 233 | * |
||
| 234 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 235 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 236 | * |
||
| 237 | * @see round() |
||
| 238 | * |
||
| 239 | * @param string $value |
||
| 240 | * @param int $significantDigits |
||
| 241 | * |
||
| 242 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | private function roundDigits( $value, $significantDigits ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Increment the least significant digit by one if it is less than 9, and |
||
| 315 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 316 | * Exception: bump( 0 ) == 1; |
||
| 317 | * |
||
| 318 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
| 319 | * |
||
| 320 | * This is the inverse of @see slump() |
||
| 321 | * |
||
| 322 | * @since 0.1 |
||
| 323 | * |
||
| 324 | * @param DecimalValue $decimal |
||
| 325 | * |
||
| 326 | * @return DecimalValue |
||
| 327 | */ |
||
| 328 | public function bump( DecimalValue $decimal ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Increment the least significant digit by one if it is less than 9, and |
||
| 336 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 337 | * |
||
| 338 | * @see bump() |
||
| 339 | * |
||
| 340 | * @param string $value |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | private function bumpDigits( $value ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 374 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 375 | * Exception: slump( 0 ) == -1; |
||
| 376 | * |
||
| 377 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
| 378 | * |
||
| 379 | * This is the inverse of @see bump() |
||
| 380 | * |
||
| 381 | * @since 0.1 |
||
| 382 | * |
||
| 383 | * @param DecimalValue $decimal |
||
| 384 | * |
||
| 385 | * @return DecimalValue |
||
| 386 | */ |
||
| 387 | public function slump( DecimalValue $decimal ) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 395 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 396 | * |
||
| 397 | * @see slump() |
||
| 398 | * |
||
| 399 | * @param string $value |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | private function slumpDigits( $value ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $digits |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | private function stripLeadingZeros( $digits ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Shift the decimal point according to the given exponent. |
||
| 456 | * |
||
| 457 | * @param DecimalValue $decimal |
||
| 458 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
| 459 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
| 460 | * |
||
| 461 | * @throws InvalidArgumentException |
||
| 462 | * @return DecimalValue |
||
| 463 | */ |
||
| 464 | public function shift( DecimalValue $decimal, $exponent ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $intPart |
||
| 491 | * @param int $exponent must be negative |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | private function shiftLeft( $intPart, $exponent ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $fractPart |
||
| 508 | * @param int $exponent must be positive |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | private function shiftRight( $fractPart, $exponent ) { |
||
| 522 | |||
| 523 | } |
||
| 524 |