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 | * @param DecimalValue $a |
||
56 | * @param DecimalValue $b |
||
57 | * |
||
58 | * @return DecimalValue |
||
59 | */ |
||
60 | public function product( DecimalValue $a, DecimalValue $b ) { |
||
70 | |||
71 | /** |
||
72 | * @param DecimalValue $a |
||
73 | * @param DecimalValue $b |
||
74 | * |
||
75 | * @return DecimalValue |
||
76 | */ |
||
77 | public function sum( DecimalValue $a, DecimalValue $b ) { |
||
87 | |||
88 | /** |
||
89 | * @param DecimalValue $a |
||
90 | * @param DecimalValue $b |
||
91 | * |
||
92 | * @return DecimalValue |
||
93 | */ |
||
94 | public function min( DecimalValue $a, DecimalValue $b ) { |
||
106 | |||
107 | /** |
||
108 | * @param DecimalValue $a |
||
109 | * @param DecimalValue $b |
||
110 | * |
||
111 | * @return DecimalValue |
||
112 | */ |
||
113 | public function max( DecimalValue $a, DecimalValue $b ) { |
||
125 | |||
126 | /** |
||
127 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
128 | * |
||
129 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
130 | * rounded to +1 and -0.5 is rounded to -1). |
||
131 | * |
||
132 | * @since 0.1 |
||
133 | * |
||
134 | * @param DecimalValue $decimal |
||
135 | * @param int $significantDigits The number of digits to retain, counting the decimal point, |
||
136 | * but not counting the leading sign. |
||
137 | * |
||
138 | * @throws InvalidArgumentException |
||
139 | * @return DecimalValue |
||
140 | */ |
||
141 | public function roundToDigit( DecimalValue $decimal, $significantDigits ) { |
||
146 | |||
147 | /** |
||
148 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
149 | * |
||
150 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
151 | * rounded to +1 and -0.5 is rounded to -1). |
||
152 | * |
||
153 | * @since 0.1 |
||
154 | * |
||
155 | * @param DecimalValue $decimal |
||
156 | * @param int $significantExponent The exponent of the last significant digit, |
||
157 | * e.g. -1 for "keep the first digit after the decimal point", or 2 for |
||
158 | * "zero the last two digits before the decimal point". |
||
159 | * |
||
160 | * @throws InvalidArgumentException |
||
161 | * @return DecimalValue |
||
162 | */ |
||
163 | public function roundToExponent( DecimalValue $decimal, $significantExponent ) { |
||
169 | |||
170 | /** |
||
171 | * Returns the (zero based) position for the given exponent in |
||
172 | * the given decimal string, counting the decimal point and the leading sign. |
||
173 | * |
||
174 | * @example: the position of exponent 0 in "+10.03" is 2. |
||
175 | * @example: the position of exponent 1 in "+210.03" is 2. |
||
176 | * @example: the position of exponent -2 in "+1.037" is 4. |
||
177 | * |
||
178 | * @param int $exponent |
||
179 | * @param DecimalValue $decimal |
||
180 | * |
||
181 | * @return int |
||
182 | */ |
||
183 | public function getPositionForExponent( $exponent, DecimalValue $decimal ) { |
||
204 | |||
205 | /** |
||
206 | * Returns the given value, with any insignificant digits removed or zeroed. |
||
207 | * |
||
208 | * Rounding is applied using the "round half away from zero" rule (that is, +0.5 is |
||
209 | * rounded to +1 and -0.5 is rounded to -1). |
||
210 | * |
||
211 | * @see round() |
||
212 | * |
||
213 | * @param string $value |
||
214 | * @param int $significantDigits |
||
215 | * |
||
216 | * @throws InvalidArgumentException if $significantDigits is smaller than 0 |
||
217 | * @return string |
||
218 | */ |
||
219 | private function roundDigits( $value, $significantDigits ) { |
||
282 | |||
283 | /** |
||
284 | * Increment the least significant digit by one if it is less than 9, and |
||
285 | * set it to zero and continue to the next more significant digit if it is 9. |
||
286 | * Exception: bump( 0 ) == 1; |
||
287 | * |
||
288 | * E.g.: bump( 0.2 ) == 0.3, bump( -0.09 ) == -0.10, bump( 9.99 ) == 10.00 |
||
289 | * |
||
290 | * This is the inverse of @see slump() |
||
291 | * |
||
292 | * @since 0.1 |
||
293 | * |
||
294 | * @param DecimalValue $decimal |
||
295 | * |
||
296 | * @return DecimalValue |
||
297 | */ |
||
298 | public function bump( DecimalValue $decimal ) { |
||
303 | |||
304 | /** |
||
305 | * Increment the least significant digit by one if it is less than 9, and |
||
306 | * set it to zero and continue to the next more significant digit if it is 9. |
||
307 | * |
||
308 | * @see bump() |
||
309 | * |
||
310 | * @param string $value |
||
311 | * @return string |
||
312 | */ |
||
313 | private function bumpDigits( $value ) { |
||
341 | |||
342 | /** |
||
343 | * Decrement the least significant digit by one if it is more than 0, and |
||
344 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
345 | * Exception: slump( 0 ) == -1; |
||
346 | * |
||
347 | * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 |
||
348 | * |
||
349 | * This is the inverse of @see bump() |
||
350 | * |
||
351 | * @since 0.1 |
||
352 | * |
||
353 | * @param DecimalValue $decimal |
||
354 | * |
||
355 | * @return DecimalValue |
||
356 | */ |
||
357 | public function slump( DecimalValue $decimal ) { |
||
362 | |||
363 | /** |
||
364 | * Decrement the least significant digit by one if it is more than 0, and |
||
365 | * set it to 9 and continue to the next more significant digit if it is 0. |
||
366 | * |
||
367 | * @see slump() |
||
368 | * |
||
369 | * @param string $value |
||
370 | * @return string |
||
371 | */ |
||
372 | private function slumpDigits( $value ) { |
||
412 | |||
413 | /** |
||
414 | * @param string $digits |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | private function stripLeadingZeros( $digits ) { |
||
422 | |||
423 | /** |
||
424 | * Shift the decimal point according to the given exponent. |
||
425 | * |
||
426 | * @param DecimalValue $decimal |
||
427 | * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent |
||
428 | * shifts the decimal point to the right, a negative exponent shifts to the left. |
||
429 | * |
||
430 | * @throws InvalidArgumentException |
||
431 | * @return DecimalValue |
||
432 | */ |
||
433 | public function shift( DecimalValue $decimal, $exponent ) { |
||
457 | |||
458 | /** |
||
459 | * @param string $intPart |
||
460 | * @param int $exponent must be negative |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | private function shiftLeft( $intPart, $exponent ) { |
||
474 | |||
475 | /** |
||
476 | * @param string $fractPart |
||
477 | * @param int $exponent must be positive |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | private function shiftRight( $fractPart, $exponent ) { |
||
491 | |||
492 | } |
||
493 |