Complex classes like Spl 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 Spl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Spl implements MathLibraryInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $roundingStrategy; |
||
18 | |||
19 | /** |
||
20 | * @param int $roundingStrategy a PHP_ROUND_HALF_* integer |
||
21 | */ |
||
22 | 106 | public function __construct(int $roundingStrategy) |
|
23 | { |
||
24 | 106 | $this->roundingStrategy = $roundingStrategy; |
|
25 | 106 | } |
|
26 | |||
27 | /** |
||
28 | * Add two arbitrary precision numbers. |
||
29 | * |
||
30 | * @param string $leftOperand |
||
31 | * @param string $rightOperand |
||
32 | * @param int $precision |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | 1 | public function add(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
37 | { |
||
38 | 1 | return (string) ($this->isIntOperation($precision) ? (intval($leftOperand) + intval($rightOperand)) : |
|
39 | 1 | round(floatval($leftOperand) + floatval($rightOperand), $precision, $this->roundingStrategy)); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Subtract two arbitrary precision numbers. |
||
44 | * |
||
45 | * @param string $leftOperand |
||
46 | * @param string $rightOperand |
||
47 | * @param int $precision |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 1 | public function subtract(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
52 | { |
||
53 | 1 | return (string) ($this->isIntOperation($precision) ? (intval($leftOperand) - intval($rightOperand)) : |
|
54 | 1 | round($leftOperand - $rightOperand, $precision, $this->roundingStrategy)); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Multiply two arbitrary precision numbers. |
||
59 | * |
||
60 | * @param string $leftOperand |
||
61 | * @param string $rightOperand |
||
62 | * @param int $precision |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 1 | public function multiply(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
67 | { |
||
68 | 1 | return (string) ($this->isIntOperation($precision) ? (intval($leftOperand) * intval($rightOperand)) : |
|
69 | 1 | round($leftOperand * $rightOperand, ($precision ?? 0), $this->roundingStrategy)); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Divide two arbitrary precision numbers. |
||
74 | * |
||
75 | * @param string $leftOperand |
||
76 | * @param string $rightOperand |
||
77 | * @param int $precision |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 1 | public function divide(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
82 | { |
||
83 | 1 | return (string) ($this->isIntOperation($precision) ? (intval($leftOperand) / intval($rightOperand)) : |
|
84 | 1 | round($leftOperand / $rightOperand, $precision, $this->roundingStrategy)); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Compare two arbitrary precision numbers. |
||
89 | * |
||
90 | * @param string $leftOperand |
||
91 | * @param string $rightOperand |
||
92 | * @param int $precision |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 2 | public function compare(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
97 | { |
||
98 | 2 | return strval($leftOperand <=> $rightOperand); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get modulus of an arbitrary precision number. |
||
103 | * |
||
104 | * @param string $operand |
||
105 | * @param string $modulus |
||
106 | * @param int $precision |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 3 | public function modulus(string $operand, string $modulus, int $precision = 0): string |
|
111 | { |
||
112 | 3 | return (string) round( |
|
113 | 3 | fmod( |
|
114 | 3 | floatval($operand), |
|
115 | 3 | floatval($modulus) |
|
116 | ), |
||
117 | 3 | ($precision ?? 0), |
|
118 | 3 | $this->roundingStrategy |
|
119 | ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Raise an arbitrary precision number to another. |
||
124 | * |
||
125 | * @param string $leftOperand |
||
126 | * @param string $rightOperand |
||
127 | * @param int $precision |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 1 | public function power(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
132 | { |
||
133 | 1 | return (string) round( |
|
134 | 1 | pow( |
|
135 | 1 | floatval($leftOperand), |
|
136 | 1 | floatval($rightOperand) |
|
137 | ), |
||
138 | 1 | ($precision ?? 0), |
|
139 | 1 | $this->roundingStrategy |
|
140 | ); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the square root of an arbitrary precision number. |
||
145 | * |
||
146 | * @param string $operand |
||
147 | * @param int $precision |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 3 | public function squareRoot(string $operand, int $precision = 0): string |
|
152 | { |
||
153 | 3 | return (string) round(sqrt(floatval($operand)), ($precision ?? 0), $this->roundingStrategy); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Returns absolute value of operand. |
||
158 | * |
||
159 | * @param string $operand |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 3 | public function absolute(string $operand): string |
|
164 | { |
||
165 | 3 | return (string) abs($operand); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Negates a number. Opposite of absolute/abs. |
||
170 | * |
||
171 | * @param string $operand |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | 3 | public function negate(string $operand): string |
|
176 | { |
||
177 | 3 | return strval($operand * -1); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns the factorial of operand. |
||
182 | * |
||
183 | * @param string $operand |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | public function factorial(string $operand): string |
|
188 | { |
||
189 | 1 | if (StringType::create($operand)->contains('.')) { |
|
190 | 1 | ++$operand; |
|
191 | |||
192 | 1 | return $this->gamma((string) $operand); |
|
193 | } |
||
194 | |||
195 | $factorial = function (string $num) use (&$factorial) { |
||
196 | 1 | if ($num < 2) { |
|
197 | 1 | return 1; |
|
198 | } |
||
199 | |||
200 | 1 | return $factorial(strval($num - 1)) * $num; |
|
201 | 1 | }; |
|
202 | |||
203 | 1 | return (string) $factorial($operand); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Greatest common divisor. |
||
208 | * |
||
209 | * @param string $leftOperand |
||
210 | * @param string $rightOperand |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function gcd(string $leftOperand, string $rightOperand): string |
||
229 | |||
230 | /** |
||
231 | * Calculates to the nth root. |
||
232 | * |
||
233 | * @param string $operand |
||
234 | * @param int $nth |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 1 | public function root(string $operand, int $nth): string |
|
243 | |||
244 | /** |
||
245 | * Gets the next prime after operand. |
||
246 | * |
||
247 | * @param string $operand |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 2 | public function nextPrime(string $operand): string |
|
262 | |||
263 | /** |
||
264 | * @param string $operand |
||
265 | * @param int $reps |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | 3 | public function isPrime(string $operand, int $reps = 10): bool |
|
280 | |||
281 | /** |
||
282 | * Checks if operand is perfect square. |
||
283 | * |
||
284 | * @param string $operand |
||
285 | * @param int $precision |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 2 | public function isPerfectSquare(string $operand, int $precision = 0): bool |
|
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | 9 | public function isEnabled(): bool |
|
303 | |||
304 | /** |
||
305 | * @param string $type |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 9 | public function supportsOperationType(string $type): bool |
|
314 | |||
315 | /** |
||
316 | * @param string $operand |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | 5 | public function gamma(string $operand): string |
|
321 | { |
||
322 | 5 | if ($operand <= 0.0) { |
|
323 | 1 | throw new InvalidNumberException('Operand must be a positive number.'); |
|
324 | } |
||
325 | |||
326 | // Euler's gamma constant |
||
395 | |||
396 | /** |
||
397 | * @param string $operand |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | 3 | public function logGamma(string $operand): string |
|
435 | |||
436 | /** |
||
437 | * Figures out the smallest number of decimal places between the two numbers and returns that count. |
||
438 | * Eg. (1.005, 2.4) => 1, (1.005, 2.5399) => 3. |
||
439 | * |
||
440 | * @param string $leftOperand |
||
441 | * @param string $rightOperand |
||
442 | * |
||
443 | * @return int |
||
444 | */ |
||
445 | 1 | private function getSmallestDecimalPlaceCount(string $leftOperand, string $rightOperand): int |
|
452 | |||
453 | /** |
||
454 | * Ensures that an operation is meant to be an integer operation, float operation otherwise. |
||
455 | * |
||
456 | * @param int $precision |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | 4 | private function isIntOperation(int $precision = 0): bool |
|
464 | } |
||
465 |