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 ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the maximum of the two values |
||
| 115 | * |
||
| 116 | * @param DecimalValue $a |
||
| 117 | * @param DecimalValue $b |
||
| 118 | * |
||
| 119 | * @return DecimalValue |
||
| 120 | */ |
||
| 121 | public function max( DecimalValue $a, DecimalValue $b ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 136 | * |
||
| 137 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 138 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 139 | * |
||
| 140 | * @since 0.1 |
||
| 141 | * |
||
| 142 | * @param DecimalValue $decimal |
||
| 143 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
| 144 | * but not counting the leading sign. |
||
| 145 | * |
||
| 146 | * @throws InvalidArgumentException |
||
| 147 | * @return DecimalValue |
||
| 148 | */ |
||
| 149 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 157 | * |
||
| 158 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 159 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 160 | * |
||
| 161 | * @since 0.1 |
||
| 162 | * |
||
| 163 | * @param DecimalValue $decimal |
||
| 164 | * @param int $significantExponent The exponent of the last significant digit, |
||
| 165 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
| 166 | * "zero the last two digits before the decimal point". |
||
| 167 | * |
||
| 168 | * @throws InvalidArgumentException |
||
| 169 | * @return DecimalValue |
||
| 170 | */ |
||
| 171 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the (zero based) position for the given exponent in |
||
| 180 | * the given decimal string, counting the decimal point and the leading sign. |
||
| 181 | * |
||
| 182 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
| 183 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
| 184 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
| 185 | * |
||
| 186 | * @param int $exponent |
||
| 187 | * @param DecimalValue $decimal |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 215 | * |
||
| 216 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 217 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 218 | * |
||
| 219 | * @see round() |
||
| 220 | * |
||
| 221 | * @param string $value |
||
| 222 | * @param int $significantDigits |
||
| 223 | * |
||
| 224 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | private function roundDigits( $value, $significantDigits ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Increment the least significant digit by one if it is less than 9, and |
||
| 293 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 294 | * Exception: bump( 0 ) == 1; |
||
| 295 | * |
||
| 296 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
| 297 | * |
||
| 298 | * This is the inverse of @see slump() |
||
| 299 | * |
||
| 300 | * @since 0.1 |
||
| 301 | * |
||
| 302 | * @param DecimalValue $decimal |
||
| 303 | * |
||
| 304 | * @return DecimalValue |
||
| 305 | */ |
||
| 306 | public function bump( DecimalValue $decimal ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Increment the least significant digit by one if it is less than 9, and |
||
| 314 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 315 | * |
||
| 316 | * @see bump() |
||
| 317 | * |
||
| 318 | * @param string $value |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | private function bumpDigits( $value ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 352 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 353 | * Exception: slump( 0 ) == -1; |
||
| 354 | * |
||
| 355 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
| 356 | * |
||
| 357 | * This is the inverse of @see bump() |
||
| 358 | * |
||
| 359 | * @since 0.1 |
||
| 360 | * |
||
| 361 | * @param DecimalValue $decimal |
||
| 362 | * |
||
| 363 | * @return DecimalValue |
||
| 364 | */ |
||
| 365 | public function slump( DecimalValue $decimal ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 373 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 374 | * |
||
| 375 | * @see slump() |
||
| 376 | * |
||
| 377 | * @param string $value |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function slumpDigits( $value ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $digits |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | private function stripLeadingZeros( $digits ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Shift the decimal point according to the given exponent. |
||
| 433 | * |
||
| 434 | * @param DecimalValue $decimal |
||
| 435 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
| 436 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
| 437 | * |
||
| 438 | * @throws InvalidArgumentException |
||
| 439 | * @return DecimalValue |
||
| 440 | */ |
||
| 441 | public function shift( DecimalValue $decimal, $exponent ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $intPart |
||
| 468 | * @param int $exponent must be negative |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | private function shiftLeft( $intPart, $exponent ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $fractPart |
||
| 485 | * @param int $exponent must be positive |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | private function shiftRight( $fractPart, $exponent ) { |
||
| 499 | |||
| 500 | } |
||
| 501 |