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 | const DEFAULT_SCALE = 100; |
||
16 | |||
17 | const MAX_BASE = 256; |
||
18 | |||
19 | const BIT_OPERATOR_AND = 'and'; |
||
20 | const BIT_OPERATOR_OR = 'or'; |
||
21 | const BIT_OPERATOR_XOR = 'xor'; |
||
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 | 359 | public static function convertScientificNotationToString($number) |
|
66 | |||
67 | /** |
||
68 | * @param int|string|float $number |
||
69 | * @return int |
||
70 | */ |
||
71 | 91 | public static function getDecimalsLengthFromNumber($number) |
|
80 | |||
81 | /** |
||
82 | * @param string $leftOperand |
||
83 | * @param string $rightOperand |
||
84 | * @param null|int $scale |
||
85 | * @return string |
||
86 | */ |
||
87 | 141 | public static function pow($leftOperand, $rightOperand, $scale = null) |
|
106 | |||
107 | /** |
||
108 | * @param int|string $number |
||
109 | * @return bool |
||
110 | */ |
||
111 | 257 | private static function checkIsFloat($number) |
|
115 | |||
116 | /** |
||
117 | * @param string $leftOperand |
||
118 | * @param string $rightOperand |
||
119 | * @param null|int $scale |
||
120 | * @return string |
||
121 | */ |
||
122 | 7 | private static function powFractional($leftOperand, $rightOperand, $scale = null) |
|
143 | |||
144 | /** |
||
145 | * @return int |
||
146 | */ |
||
147 | 35 | public static function getScale() |
|
153 | |||
154 | /** |
||
155 | * @param string $operand |
||
156 | * @param null|int $scale |
||
157 | * @return string |
||
158 | */ |
||
159 | 40 | public static function sqrt($operand, $scale = null) |
|
169 | |||
170 | /** |
||
171 | * @param int|string $number |
||
172 | * @return int|string |
||
173 | */ |
||
174 | 359 | private static function checkNumber($number) |
|
183 | |||
184 | /** |
||
185 | * @param string $leftOperand |
||
186 | * @param string $rightOperand |
||
187 | * @param null|int $scale |
||
188 | * @return string |
||
189 | */ |
||
190 | 161 | View Code Duplication | public static function mul($leftOperand, $rightOperand, $scale = null) |
201 | |||
202 | /** |
||
203 | * @param string $arg |
||
204 | * @return string |
||
205 | */ |
||
206 | 15 | public static function exp($arg) |
|
216 | |||
217 | /** |
||
218 | * @param string $leftOperand |
||
219 | * @param string $rightOperand |
||
220 | * @param null|int $scale |
||
221 | * @return string |
||
222 | */ |
||
223 | 155 | View Code Duplication | public static function add($leftOperand, $rightOperand, $scale = null) |
234 | |||
235 | /** |
||
236 | * @param string $leftOperand |
||
237 | * @param string $rightOperand |
||
238 | * @param null|int $scale |
||
239 | * @return string |
||
240 | */ |
||
241 | 125 | View Code Duplication | public static function div($leftOperand, $rightOperand, $scale = null) |
252 | |||
253 | /** |
||
254 | * @param string $arg |
||
255 | * @return string |
||
256 | */ |
||
257 | 12 | public static function log($arg) |
|
285 | |||
286 | /** |
||
287 | * @param string $leftOperand |
||
288 | * @param string $rightOperand |
||
289 | * @param null|int $scale |
||
290 | * @return int |
||
291 | */ |
||
292 | 59 | public static function comp($leftOperand, $rightOperand, $scale = null) |
|
307 | |||
308 | /** |
||
309 | * @param string $leftOperand |
||
310 | * @param string $rightOperand |
||
311 | * @param null|int $scale |
||
312 | * @return string |
||
313 | */ |
||
314 | 73 | View Code Duplication | public static function sub($leftOperand, $rightOperand, $scale = null) |
325 | |||
326 | /** |
||
327 | * @param $number |
||
328 | * @return bool |
||
329 | */ |
||
330 | 148 | private static function isNegative($number) |
|
334 | |||
335 | /** |
||
336 | * @param int|string $min |
||
337 | * @param int|string $max |
||
338 | * @return string |
||
339 | */ |
||
340 | 2 | public static function rand($min, $max) |
|
350 | |||
351 | /** |
||
352 | * @param array|int|string,... |
||
353 | * @return null|string |
||
354 | */ |
||
355 | 1 | View Code Duplication | public static function max() |
373 | |||
374 | /** |
||
375 | * @param array|int|string,... |
||
376 | * @return null|string |
||
377 | */ |
||
378 | 1 | View Code Duplication | public static function min() |
396 | |||
397 | /** |
||
398 | * @param int|string $number |
||
399 | * @param int $precision |
||
400 | * @return string |
||
401 | */ |
||
402 | 17 | View Code Duplication | public static function roundDown($number, $precision = 0) |
419 | |||
420 | /** |
||
421 | * @param int|string $number |
||
422 | * @return string |
||
423 | */ |
||
424 | 93 | View Code Duplication | public static function floor($number) |
437 | |||
438 | /** |
||
439 | * @param int|string $number |
||
440 | * @return bool |
||
441 | */ |
||
442 | 68 | private static function checkIsFloatCleanZeros(&$number) |
|
446 | |||
447 | /** |
||
448 | * @param int|string $number |
||
449 | * @param int $precision |
||
450 | * @return string |
||
451 | */ |
||
452 | 17 | View Code Duplication | public static function roundUp($number, $precision = 0) |
469 | |||
470 | /** |
||
471 | * @param int|string $number |
||
472 | * @return string |
||
473 | */ |
||
474 | 41 | View Code Duplication | public static function ceil($number) |
487 | |||
488 | /** |
||
489 | * @param string $leftOperand |
||
490 | * @param string $rightOperand |
||
491 | * @param string $modulus |
||
492 | * @param null|int $scale |
||
493 | * @return string |
||
494 | */ |
||
495 | 10 | public static function powMod($leftOperand, $rightOperand, $modulus, $scale = null) |
|
512 | |||
513 | /** |
||
514 | * @param string $leftOperand |
||
515 | * @param string $modulus |
||
516 | * @param null $scale |
||
517 | * @return string |
||
518 | */ |
||
519 | 51 | public static function mod($leftOperand, $modulus, $scale = null) |
|
535 | |||
536 | /** |
||
537 | * @param string $arg |
||
538 | * @return string |
||
539 | * @throws \InvalidArgumentException |
||
540 | */ |
||
541 | 8 | public static function fact($arg) |
|
559 | |||
560 | /** |
||
561 | * @param string $hex |
||
562 | * @return string |
||
563 | */ |
||
564 | 5 | public static function hexdec($hex) |
|
575 | |||
576 | /** |
||
577 | * @param int $decimal |
||
578 | * @return string |
||
579 | */ |
||
580 | 6 | public static function dechex($decimal) |
|
591 | |||
592 | /** |
||
593 | * @param string $leftOperand |
||
594 | * @param string $rightOperand |
||
595 | * @return string |
||
596 | */ |
||
597 | 12 | public static function bitAnd($leftOperand, $rightOperand) |
|
601 | |||
602 | /** |
||
603 | * @param string $leftOperand |
||
604 | * @param string $rightOperand |
||
605 | * @param string $operator |
||
606 | * @return string |
||
607 | */ |
||
608 | 36 | private static function bitOperatorHelper($leftOperand, $rightOperand, $operator) |
|
659 | |||
660 | /** |
||
661 | * @param string $number |
||
662 | * @param int $base |
||
663 | * @return string |
||
664 | */ |
||
665 | public static function dec2bin($number, $base = self::MAX_BASE) |
||
682 | |||
683 | /** |
||
684 | * @param int $base |
||
685 | * @param \Closure $closure |
||
686 | * @return string |
||
687 | */ |
||
688 | 30 | private static function decBaseHelper($base, \Closure $closure) |
|
702 | |||
703 | /** |
||
704 | * @param null|int $scale |
||
705 | */ |
||
706 | 359 | public static function setScale($scale) |
|
710 | |||
711 | /** |
||
712 | * @param int|string $number |
||
713 | * @return string |
||
714 | */ |
||
715 | 45 | public static function abs($number) |
|
725 | |||
726 | /** |
||
727 | * @param string $string |
||
728 | * @param int $length |
||
729 | * @return string |
||
730 | */ |
||
731 | 30 | private static function alignBinLength($string, $length) |
|
735 | |||
736 | /** |
||
737 | * @param string $number |
||
738 | * @return string |
||
739 | */ |
||
740 | 11 | private static function recalculateNegative($number) |
|
755 | |||
756 | /** |
||
757 | * @param string $value |
||
758 | * @param int $base |
||
759 | * @return string |
||
760 | */ |
||
761 | public static function bin2dec($value, $base = self::MAX_BASE) |
||
775 | |||
776 | /** |
||
777 | * @param string $leftOperand |
||
778 | * @param string $rightOperand |
||
779 | * @return string |
||
780 | */ |
||
781 | 14 | public static function bitOr($leftOperand, $rightOperand) |
|
785 | |||
786 | /** |
||
787 | * @param string $leftOperand |
||
788 | * @param string $rightOperand |
||
789 | * @return string |
||
790 | */ |
||
791 | 10 | public static function bitXor($leftOperand, $rightOperand) |
|
795 | } |
||
796 |
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.