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 | const DEFAULT_SCALE = 100; |
||
15 | |||
16 | /** |
||
17 | * @param null|int $scale |
||
18 | */ |
||
19 | 323 | public static function setScale($scale) |
|
23 | |||
24 | /** |
||
25 | * @param int|string $number |
||
26 | * @param int $precision |
||
27 | * @return string |
||
28 | */ |
||
29 | 47 | public static function round($number, $precision = 0) |
|
42 | |||
43 | /** |
||
44 | * @param int|string|float $number |
||
45 | * @return string |
||
46 | */ |
||
47 | 323 | public static function convertScientificNotationToString($number) |
|
67 | |||
68 | /** |
||
69 | * @param int|string|float $number |
||
70 | * @return int |
||
71 | */ |
||
72 | 91 | public static function getDecimalsLengthFromNumber($number) |
|
81 | |||
82 | /** |
||
83 | * @param string $leftOperand |
||
84 | * @param string $rightOperand |
||
85 | * @param null|int $scale |
||
86 | * @return string |
||
87 | */ |
||
88 | 111 | public static function pow($leftOperand, $rightOperand, $scale = null) |
|
107 | |||
108 | /** |
||
109 | * @param int|string $number |
||
110 | * @return bool |
||
111 | */ |
||
112 | 221 | private static function checkIsFloat($number) |
|
116 | |||
117 | /** |
||
118 | * @param string $leftOperand |
||
119 | * @param string $rightOperand |
||
120 | * @param null|int $scale |
||
121 | * @return string |
||
122 | */ |
||
123 | 7 | private static function powFractional($leftOperand, $rightOperand, $scale = null) |
|
144 | |||
145 | /** |
||
146 | * @param int|string $number |
||
147 | * @return int|string |
||
148 | */ |
||
149 | 323 | private static function checkNumber($number) |
|
158 | |||
159 | /** |
||
160 | * @param string $leftOperand |
||
161 | * @param string $rightOperand |
||
162 | * @param null|int $scale |
||
163 | * @return string |
||
164 | */ |
||
165 | 131 | View Code Duplication | public static function mul($leftOperand, $rightOperand, $scale = null) |
176 | |||
177 | /** |
||
178 | * @param string $arg |
||
179 | * @return string |
||
180 | */ |
||
181 | 15 | public static function exp($arg) |
|
191 | |||
192 | /** |
||
193 | * @param string $leftOperand |
||
194 | * @param string $rightOperand |
||
195 | * @param null|int $scale |
||
196 | * @return string |
||
197 | */ |
||
198 | 125 | View Code Duplication | public static function add($leftOperand, $rightOperand, $scale = null) |
209 | |||
210 | /** |
||
211 | * @param string $leftOperand |
||
212 | * @param string $rightOperand |
||
213 | * @param null|int $scale |
||
214 | * @return string |
||
215 | */ |
||
216 | 97 | View Code Duplication | public static function div($leftOperand, $rightOperand, $scale = null) |
227 | |||
228 | /** |
||
229 | * @param string $arg |
||
230 | * @return string |
||
231 | */ |
||
232 | 12 | public static function log($arg) |
|
260 | |||
261 | /** |
||
262 | * @param string $leftOperand |
||
263 | * @param string $rightOperand |
||
264 | * @param null|int $scale |
||
265 | * @return int |
||
266 | */ |
||
267 | 31 | public static function comp($leftOperand, $rightOperand, $scale = null) |
|
282 | |||
283 | /** |
||
284 | * @param string $leftOperand |
||
285 | * @param string $rightOperand |
||
286 | * @param null|int $scale |
||
287 | * @return string |
||
288 | */ |
||
289 | 45 | View Code Duplication | public static function sub($leftOperand, $rightOperand, $scale = null) |
300 | |||
301 | /** |
||
302 | * @param $number |
||
303 | * @return bool |
||
304 | */ |
||
305 | 118 | private static function isNegative($number) |
|
309 | |||
310 | /** |
||
311 | * @param int|string $number |
||
312 | * @return string |
||
313 | */ |
||
314 | 15 | public static function abs($number) |
|
324 | |||
325 | /** |
||
326 | * @param int|string $min |
||
327 | * @param int|string $max |
||
328 | * @return string |
||
329 | */ |
||
330 | 2 | public static function rand($min, $max) |
|
340 | |||
341 | /** |
||
342 | * @param array|int|string,... |
||
343 | * @return null|string |
||
344 | */ |
||
345 | 1 | View Code Duplication | public static function max() |
363 | |||
364 | /** |
||
365 | * @param array|int|string,... |
||
366 | * @return null|string |
||
367 | */ |
||
368 | 1 | View Code Duplication | public static function min() |
386 | |||
387 | /** |
||
388 | * @param int|string $number |
||
389 | * @param int $precision |
||
390 | * @return string |
||
391 | */ |
||
392 | 17 | View Code Duplication | public static function roundDown($number, $precision = 0) |
407 | |||
408 | /** |
||
409 | * @param int|string $number |
||
410 | * @return string |
||
411 | */ |
||
412 | 65 | View Code Duplication | public static function floor($number) |
425 | |||
426 | /** |
||
427 | * @param int|string $number |
||
428 | * @return bool |
||
429 | */ |
||
430 | 68 | private static function checkIsFloatCleanZeros(&$number) |
|
434 | |||
435 | /** |
||
436 | * @param int|string $number |
||
437 | * @param int $precision |
||
438 | * @return string |
||
439 | */ |
||
440 | 17 | View Code Duplication | public static function roundUp($number, $precision = 0) |
455 | |||
456 | /** |
||
457 | * @param int|string $number |
||
458 | * @return string |
||
459 | */ |
||
460 | 41 | View Code Duplication | public static function ceil($number) |
473 | |||
474 | /** |
||
475 | * @return int |
||
476 | */ |
||
477 | 5 | public static function getScale() |
|
483 | |||
484 | /** |
||
485 | * @param string $operand |
||
486 | * @param null|int $scale |
||
487 | * @return string |
||
488 | */ |
||
489 | 10 | public static function sqrt($operand, $scale = null) |
|
499 | |||
500 | /** |
||
501 | * @param string $leftOperand |
||
502 | * @param string $modulus |
||
503 | * @param null $scale |
||
504 | * @return string |
||
505 | */ |
||
506 | 23 | public static function mod($leftOperand, $modulus, $scale = null) |
|
522 | |||
523 | /** |
||
524 | * @param string $leftOperand |
||
525 | * @param string $rightOperand |
||
526 | * @param string $modulus |
||
527 | * @param null|int $scale |
||
528 | * @return string |
||
529 | */ |
||
530 | 10 | public static function powMod($leftOperand, $rightOperand, $modulus, $scale = null) |
|
547 | |||
548 | /** |
||
549 | * @param string $arg |
||
550 | * @return string |
||
551 | * @throws \InvalidArgumentException |
||
552 | */ |
||
553 | 8 | public static function fact($arg) |
|
571 | |||
572 | /** |
||
573 | * @param string $hex |
||
574 | * @return string |
||
575 | */ |
||
576 | 5 | public static function hexdec($hex) { |
|
586 | |||
587 | /** |
||
588 | * @param int $decimal |
||
589 | * @return string |
||
590 | */ |
||
591 | 6 | public static function dechex($decimal) { |
|
601 | } |
||
602 |
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.