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 | 212 | 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 | 16 | public function product( DecimalValue $a, DecimalValue $b ) { |
|
76 | |||
77 | /** |
||
78 | * @param DecimalValue $a |
||
79 | * @param DecimalValue $b |
||
80 | * |
||
81 | * @return DecimalValue |
||
82 | */ |
||
83 | 9 | public function sum( DecimalValue $a, DecimalValue $b ) { |
|
93 | |||
94 | /** |
||
95 | * @param DecimalValue $a |
||
96 | * @param DecimalValue $b |
||
97 | * |
||
98 | * @return DecimalValue |
||
99 | */ |
||
100 | 5 | public function min( DecimalValue $a, DecimalValue $b ) { |
|
112 | |||
113 | /** |
||
114 | * @param DecimalValue $a |
||
115 | * @param DecimalValue $b |
||
116 | * |
||
117 | * @return DecimalValue |
||
118 | */ |
||
119 | 5 | public function max( DecimalValue $a, DecimalValue $b ) { |
|
131 | |||
132 | /** |
||
133 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
134 | * |
||
135 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
136 | * rounded to +1 and -0.5 is rounded to -1). |
||
137 | * |
||
138 | * @since 0.1 |
||
139 | * |
||
140 | * @param DecimalValue $decimal |
||
141 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
142 | * but not counting the leading sign. |
||
143 | * |
||
144 | * @throws InvalidArgumentException |
||
145 | * @return DecimalValue |
||
146 | */ |
||
147 | 105 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
|
152 | |||
153 | /** |
||
154 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
155 | * |
||
156 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
157 | * rounded to +1 and -0.5 is rounded to -1). |
||
158 | * |
||
159 | * @since 0.1 |
||
160 | * |
||
161 | * @param DecimalValue $decimal |
||
162 | * @param int $significantExponent The exponent of the last significant digit, |
||
163 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
164 | * "zero the last two digits before the decimal point". |
||
165 | * |
||
166 | * @throws InvalidArgumentException |
||
167 | * @return DecimalValue |
||
168 | */ |
||
169 | 47 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
|
175 | |||
176 | /** |
||
177 | * Returns the (zero based) position for the given exponent in |
||
178 | * the given decimal string, counting the decimal point and the leading sign. |
||
179 | * |
||
180 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
181 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
182 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
183 | * |
||
184 | * @param int $exponent |
||
185 | * @param DecimalValue $decimal |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | 53 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
|
210 | |||
211 | /** |
||
212 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
213 | * |
||
214 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
215 | * rounded to +1 and -0.5 is rounded to -1). |
||
216 | * |
||
217 | * @see round() |
||
218 | * |
||
219 | * @param string $value |
||
220 | * @param int $significantDigits |
||
221 | * |
||
222 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
223 | * @return string |
||
224 | */ |
||
225 | 121 | private function roundDigits( $value, $significantDigits ) { |
|
288 | |||
289 | /** |
||
290 | * Increment the least significant digit by one if it is less than 9, and |
||
291 | * set it to zero and continue to the next more significant digit if it is 9. |
||
292 | * Exception: bump( 0 ) == 1; |
||
293 | * |
||
294 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
295 | * |
||
296 | * This is the inverse of @see slump() |
||
297 | * |
||
298 | * @since 0.1 |
||
299 | * |
||
300 | * @param DecimalValue $decimal |
||
301 | * |
||
302 | * @return DecimalValue |
||
303 | */ |
||
304 | 16 | public function bump( DecimalValue $decimal ) { |
|
309 | |||
310 | /** |
||
311 | * Increment the least significant digit by one if it is less than 9, and |
||
312 | * set it to zero and continue to the next more significant digit if it is 9. |
||
313 | * |
||
314 | * @see bump() |
||
315 | * |
||
316 | * @param string $value |
||
317 | * @return string |
||
318 | */ |
||
319 | 43 | private function bumpDigits( $value ) { |
|
347 | |||
348 | /** |
||
349 | * Decrement the least significant digit by one if it is more than 0, and |
||
350 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
351 | * Exception: slump( 0 ) == -1; |
||
352 | * |
||
353 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
354 | * |
||
355 | * This is the inverse of @see bump() |
||
356 | * |
||
357 | * @since 0.1 |
||
358 | * |
||
359 | * @param DecimalValue $decimal |
||
360 | * |
||
361 | * @return DecimalValue |
||
362 | */ |
||
363 | 24 | public function slump( DecimalValue $decimal ) { |
|
368 | |||
369 | /** |
||
370 | * Decrement the least significant digit by one if it is more than 0, and |
||
371 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
372 | * |
||
373 | * @see slump() |
||
374 | * |
||
375 | * @param string $value |
||
376 | * @return string |
||
377 | */ |
||
378 | 24 | private function slumpDigits( $value ) { |
|
418 | |||
419 | /** |
||
420 | * @param string $digits |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 40 | private function stripLeadingZeros( $digits ) { |
|
428 | |||
429 | /** |
||
430 | * Shift the decimal point according to the given exponent. |
||
431 | * |
||
432 | * @param DecimalValue $decimal |
||
433 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
434 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
435 | * |
||
436 | * @throws InvalidArgumentException |
||
437 | * @return DecimalValue |
||
438 | */ |
||
439 | 26 | public function shift( DecimalValue $decimal, $exponent ) { |
|
463 | |||
464 | /** |
||
465 | * @param string $intPart |
||
466 | * @param int $exponent must be negative |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | 14 | private function shiftLeft( $intPart, $exponent ) { |
|
480 | |||
481 | /** |
||
482 | * @param string $fractPart |
||
483 | * @param int $exponent must be positive |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | 8 | private function shiftRight( $fractPart, $exponent ) { |
|
497 | |||
498 | } |
||
499 |