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 Maths 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 Maths, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Maths |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var boolean $blnBcmath Efficiency: Is the BC Math extension loaded? |
||
10 | */ |
||
11 | protected static $blnBcmath = null; |
||
12 | |||
13 | /** |
||
14 | * Normalises score according to min & max allowed. If score larger |
||
15 | * than max, max is returned. If score less than min, min is returned. |
||
16 | * Also rounds result to specified precision. |
||
17 | * Thanks to github.com/lvil. |
||
18 | * @param int|float $score Initial score |
||
19 | * @param int $min Minimum score allowed |
||
20 | * @param int $max Maximum score allowed |
||
21 | * @return int|float |
||
22 | */ |
||
23 | 1 | public static function normaliseScore($score, $min, $max, $dps = 1) |
|
34 | |||
35 | /** |
||
36 | * Do simple reliable floating point calculations without the risk of wrong results |
||
37 | * @see http://floating-point-gui.de/ |
||
38 | * @see the big red warning on http://php.net/language.types.float.php |
||
39 | * |
||
40 | * @source https://gist.github.com/jrfnl/8449978 |
||
41 | * |
||
42 | * In the rare case that the bcmath extension would not be loaded, it will return the normal calculation results |
||
43 | * |
||
44 | * @param mixed $number1 Scalar (string/int/float/bool) |
||
45 | * @param string $action Calculation action to execute. Valid input: |
||
46 | * '+' or 'add' or 'addition', |
||
47 | * '-' or 'sub' or 'subtract', |
||
48 | * '*' or 'mul' or 'multiply', |
||
49 | * '/' or 'div' or 'divide', |
||
50 | * '%' or 'mod' or 'modulus' |
||
51 | * '=' or 'comp' or 'compare' |
||
52 | * @param mixed $number2 Scalar (string/int/float/bool) |
||
53 | * @param bool $round Whether or not to round the result. Defaults to false. |
||
54 | * Will be disregarded for a compare operation |
||
55 | * @param int $decimals Decimals for rounding operation. Defaults to 0. |
||
56 | * @param int $precision Calculation precision. Defaults to 10. |
||
57 | * @return mixed Calculation result or false if either or the numbers isn't scalar or |
||
58 | * an invalid operation was passed |
||
59 | * - for compare the result will always be an integer |
||
60 | * - for all other operations, the result will either be an integer |
||
61 | * (preferred) or a float |
||
62 | */ |
||
63 | 23 | public static function bcCalc($number1, $action, $number2, $round = false, $decimals = 0, $precision = 10) |
|
86 | |||
87 | /** |
||
88 | * Normalise operators for bcMath function. |
||
89 | * @param string $operator Operators such as "+", "add" |
||
90 | * @return string |
||
91 | */ |
||
92 | 23 | public static function normaliseOperator($operator) |
|
122 | |||
123 | /** |
||
124 | * Function which performs calculation. |
||
125 | * @param string|integer|float|boolean $number1 See bcCalc description |
||
126 | * @param string $action See bcCalc description |
||
127 | * @param string|integer|float|boolean $number2 See bcCalc description |
||
128 | * @param boolean $round See bcCalc description |
||
129 | * @param integer $decimals See bcCalc description |
||
130 | * @param integer $precision See bcCalc description |
||
131 | * @return integer|float|boolean |
||
132 | */ |
||
133 | 23 | private static function performCalc($number1, $action, $number2, $round, $decimals, $precision) |
|
200 | } |
||
201 |
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.