Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BC 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 BC, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class BC |
||
10 | { |
||
11 | const COMPARE_EQUAL = 0; |
||
12 | const COMPARE_LEFT_GRATER = 1; |
||
13 | const COMPARE_RIGHT_GRATER = -1; |
||
14 | |||
15 | /** |
||
16 | * @param int $scale |
||
17 | */ |
||
18 | 13 | public static function setScale($scale) |
|
22 | |||
23 | /** |
||
24 | * @param int|string $number |
||
25 | * @param int $precision |
||
26 | * @return string |
||
27 | */ |
||
28 | 47 | public static function round($number, $precision = 0) |
|
41 | |||
42 | /** |
||
43 | * @param int|string|float $number |
||
44 | * @return string |
||
45 | */ |
||
46 | 190 | public static function convertScientificNotationToString($number) |
|
66 | |||
67 | /** |
||
68 | * @param int|string|float $number |
||
69 | * @return int |
||
70 | */ |
||
71 | 38 | private static function getDecimalsLengthFromNumber($number) |
|
80 | |||
81 | |||
82 | /** |
||
83 | * @param string $leftOperand |
||
84 | * @param string $rightOperand |
||
85 | * @param int $scale |
||
86 | * @return string |
||
87 | */ |
||
88 | 39 | View Code Duplication | public static function mul($leftOperand, $rightOperand, $scale = null) |
99 | |||
100 | /** |
||
101 | * @param string $leftOperand |
||
102 | * @param string $rightOperand |
||
103 | * @param int $scale |
||
104 | * @return string |
||
105 | */ |
||
106 | 43 | View Code Duplication | public static function pow($leftOperand, $rightOperand, $scale = null) |
117 | |||
118 | /** |
||
119 | * @param string $leftOperand |
||
120 | * @param string $rightOperand |
||
121 | * @param int $scale |
||
122 | * @return string |
||
123 | */ |
||
124 | 26 | View Code Duplication | public static function div($leftOperand, $rightOperand, $scale = null) |
135 | |||
136 | /** |
||
137 | * @param int|string $number |
||
138 | * @return int|string |
||
139 | */ |
||
140 | 190 | private static function checkNumber($number) |
|
149 | |||
150 | /** |
||
151 | * @param int|string $number |
||
152 | * @return bool |
||
153 | */ |
||
154 | 101 | private static function checkIsFloat($number) |
|
158 | |||
159 | /** |
||
160 | * @param $number |
||
161 | * @return bool |
||
162 | */ |
||
163 | 77 | private static function isNegative($number) |
|
167 | |||
168 | /** |
||
169 | * @param string $leftOperand |
||
170 | * @param string $rightOperand |
||
171 | * @param int $scale |
||
172 | * @return string |
||
173 | */ |
||
174 | 11 | View Code Duplication | public static function sub($leftOperand, $rightOperand, $scale = null) |
185 | |||
186 | /** |
||
187 | * @param string $leftOperand |
||
188 | * @param string $rightOperand |
||
189 | * @param int $scale |
||
190 | * @return string |
||
191 | */ |
||
192 | 65 | View Code Duplication | public static function add($leftOperand, $rightOperand, $scale = null) |
203 | |||
204 | /** |
||
205 | * @param int|string $number |
||
206 | * @return string |
||
207 | */ |
||
208 | 15 | public static function abs($number) |
|
218 | |||
219 | /** |
||
220 | * @param int|string $min |
||
221 | * @param int|string $max |
||
222 | * @return string |
||
223 | */ |
||
224 | 2 | public static function rand($min, $max) |
|
234 | |||
235 | /** |
||
236 | * @param array|int|string,... |
||
237 | * @return null|string |
||
238 | */ |
||
239 | 1 | View Code Duplication | public static function max() |
257 | |||
258 | /** |
||
259 | * @param string $leftOperand |
||
260 | * @param string $rightOperand |
||
261 | * @param int $scale |
||
262 | * @return int |
||
263 | */ |
||
264 | 14 | public static function comp($leftOperand, $rightOperand, $scale = null) |
|
279 | |||
280 | /** |
||
281 | * @param array|int|string,... |
||
282 | * @return null|string |
||
283 | */ |
||
284 | 1 | View Code Duplication | public static function min() |
302 | |||
303 | /** |
||
304 | * @param int|string $number |
||
305 | * @param int $precision |
||
306 | * @return string |
||
307 | */ |
||
308 | 1 | View Code Duplication | public static function roundDown($number, $precision = 0) |
323 | |||
324 | /** |
||
325 | * @param int|string $number |
||
326 | * @return string |
||
327 | */ |
||
328 | 29 | View Code Duplication | public static function floor($number) |
341 | |||
342 | /** |
||
343 | * @param int|string $number |
||
344 | * @return bool |
||
345 | */ |
||
346 | 28 | private static function checkIsFloatCleanZeros(&$number) |
|
350 | |||
351 | /** |
||
352 | * @param int|string $number |
||
353 | * @param int $precision |
||
354 | * @return string |
||
355 | */ |
||
356 | 1 | View Code Duplication | public static function roundUp($number, $precision = 0) |
371 | |||
372 | /** |
||
373 | * @param int|string $number |
||
374 | * @return string |
||
375 | */ |
||
376 | 25 | View Code Duplication | public static function ceil($number) |
389 | |||
390 | /** |
||
391 | * @return int |
||
392 | */ |
||
393 | 3 | public static function getScale() |
|
399 | |||
400 | /** |
||
401 | * @param string $operand |
||
402 | * @param int $scale |
||
403 | * @return string |
||
404 | */ |
||
405 | 5 | public static function sqrt($operand, $scale = null) |
|
415 | |||
416 | /** |
||
417 | * @param string $leftOperand |
||
418 | * @param string $modulus |
||
419 | * @param int $scale |
||
420 | * @return string |
||
421 | */ |
||
422 | 3 | public static function fmod($leftOperand, $modulus, $scale = null) |
|
442 | |||
443 | /** |
||
444 | * @param string $leftOperand |
||
445 | * @param string $modulus |
||
446 | * @return string |
||
447 | */ |
||
448 | 1 | public static function mod($leftOperand, $modulus) |
|
455 | |||
456 | /** |
||
457 | * @param string $leftOperand |
||
458 | * @param string $rightOperand |
||
459 | * @param string $modulus |
||
460 | * @param int $scale |
||
461 | * @return string |
||
462 | */ |
||
463 | 1 | View Code Duplication | public static function powMod($leftOperand, $rightOperand, $modulus, $scale = null) |
474 | } |
||
475 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.