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 | 224 | 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 | * @param DecimalValue $a |
||
| 56 | * @param DecimalValue $b |
||
| 57 | * |
||
| 58 | * @return DecimalValue |
||
| 59 | */ |
||
| 60 | 28 | public function product( DecimalValue $a, DecimalValue $b ) { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param DecimalValue $a |
||
| 69 | * @param DecimalValue $b |
||
| 70 | * |
||
| 71 | * @return DecimalValue |
||
| 72 | */ |
||
| 73 | 16 | private function productWithBC( DecimalValue $a, DecimalValue $b ) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param DecimalValue $a |
||
| 88 | * @param DecimalValue $b |
||
| 89 | * |
||
| 90 | * @return DecimalValue |
||
| 91 | */ |
||
| 92 | 12 | private function productWithoutBC( DecimalValue $a, DecimalValue $b ) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param DecimalValue $a |
||
| 109 | * @param DecimalValue $b |
||
| 110 | * |
||
| 111 | * @return DecimalValue |
||
| 112 | */ |
||
| 113 | 9 | public function sum( DecimalValue $a, DecimalValue $b ) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param DecimalValue $a |
||
| 126 | * @param DecimalValue $b |
||
| 127 | * |
||
| 128 | * @return DecimalValue |
||
| 129 | */ |
||
| 130 | 5 | public function min( DecimalValue $a, DecimalValue $b ) { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param DecimalValue $a |
||
| 145 | * @param DecimalValue $b |
||
| 146 | * |
||
| 147 | * @return DecimalValue |
||
| 148 | */ |
||
| 149 | 5 | public function max( DecimalValue $a, DecimalValue $b ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 164 | * |
||
| 165 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 166 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 167 | * |
||
| 168 | * @since 0.1 |
||
| 169 | * |
||
| 170 | * @param DecimalValue $decimal |
||
| 171 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
| 172 | * but not counting the leading sign. |
||
| 173 | * |
||
| 174 | * @throws InvalidArgumentException |
||
| 175 | * @return DecimalValue |
||
| 176 | */ |
||
| 177 | 105 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 185 | * |
||
| 186 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 187 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 188 | * |
||
| 189 | * @since 0.1 |
||
| 190 | * |
||
| 191 | * @param DecimalValue $decimal |
||
| 192 | * @param int $significantExponent The exponent of the last significant digit, |
||
| 193 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
| 194 | * "zero the last two digits before the decimal point". |
||
| 195 | * |
||
| 196 | * @throws InvalidArgumentException |
||
| 197 | * @return DecimalValue |
||
| 198 | */ |
||
| 199 | 47 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the (zero based) position for the given exponent in |
||
| 208 | * the given decimal string, counting the decimal point and the leading sign. |
||
| 209 | * |
||
| 210 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
| 211 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
| 212 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
| 213 | * |
||
| 214 | * @param int $exponent |
||
| 215 | * @param DecimalValue $decimal |
||
| 216 | * |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | 53 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
| 243 | * |
||
| 244 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
| 245 | * rounded to +1 and -0.5 is rounded to -1). |
||
| 246 | * |
||
| 247 | * @see round() |
||
| 248 | * |
||
| 249 | * @param string $value |
||
| 250 | * @param int $significantDigits |
||
| 251 | * |
||
| 252 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 121 | private function roundDigits( $value, $significantDigits ) { |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Increment the least significant digit by one if it is less than 9, and |
||
| 321 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 322 | * Exception: bump( 0 ) == 1; |
||
| 323 | * |
||
| 324 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
| 325 | * |
||
| 326 | * This is the inverse of @see slump() |
||
| 327 | * |
||
| 328 | * @since 0.1 |
||
| 329 | * |
||
| 330 | * @param DecimalValue $decimal |
||
| 331 | * |
||
| 332 | * @return DecimalValue |
||
| 333 | */ |
||
| 334 | 16 | public function bump( DecimalValue $decimal ) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Increment the least significant digit by one if it is less than 9, and |
||
| 342 | * set it to zero and continue to the next more significant digit if it is 9. |
||
| 343 | * |
||
| 344 | * @see bump() |
||
| 345 | * |
||
| 346 | * @param string $value |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 43 | private function bumpDigits( $value ) { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 380 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 381 | * Exception: slump( 0 ) == -1; |
||
| 382 | * |
||
| 383 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
| 384 | * |
||
| 385 | * This is the inverse of @see bump() |
||
| 386 | * |
||
| 387 | * @since 0.1 |
||
| 388 | * |
||
| 389 | * @param DecimalValue $decimal |
||
| 390 | * |
||
| 391 | * @return DecimalValue |
||
| 392 | */ |
||
| 393 | 24 | public function slump( DecimalValue $decimal ) { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Decrement the least significant digit by one if it is more than 0, and |
||
| 401 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
| 402 | * |
||
| 403 | * @see slump() |
||
| 404 | * |
||
| 405 | * @param string $value |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 24 | private function slumpDigits( $value ) { |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $digits |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 40 | private function stripLeadingZeros( $digits ) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Shift the decimal point according to the given exponent. |
||
| 461 | * |
||
| 462 | * @param DecimalValue $decimal |
||
| 463 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
| 464 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
| 465 | * |
||
| 466 | * @throws InvalidArgumentException |
||
| 467 | * @return DecimalValue |
||
| 468 | */ |
||
| 469 | 26 | public function shift( DecimalValue $decimal, $exponent ) { |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $intPart |
||
| 496 | * @param int $exponent must be negative |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | 14 | private function shiftLeft( $intPart, $exponent ) { |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $fractPart |
||
| 513 | * @param int $exponent must be positive |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | 8 | private function shiftRight( $fractPart, $exponent ) { |
|
| 527 | |||
| 528 | } |
||
| 529 |