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 | 2 | public static function setScale($scale) |
|
22 | |||
23 | /** |
||
24 | * @param int|string $number |
||
25 | * @return string |
||
26 | */ |
||
27 | 2 | View Code Duplication | public static function ceil($number) |
41 | |||
42 | /** |
||
43 | * @param int|string $number |
||
44 | * @return bool |
||
45 | */ |
||
46 | 6 | private static function checkIsFloat($number) |
|
50 | |||
51 | /** |
||
52 | * @param int|string $number |
||
53 | * @return bool |
||
54 | */ |
||
55 | 5 | private static function checkIsFloatCleanZeros(&$number) |
|
59 | |||
60 | /** |
||
61 | * @param $number |
||
62 | * @return bool |
||
63 | */ |
||
64 | 6 | private static function isNegative($number) |
|
68 | |||
69 | /** |
||
70 | * @param int|string $number |
||
71 | * @return int|string |
||
72 | */ |
||
73 | 7 | private static function checkNumber($number) |
|
81 | |||
82 | /** |
||
83 | * @param int|string $number |
||
84 | * @param int $precision |
||
85 | * @return string |
||
86 | */ |
||
87 | 1 | public static function round($number, $precision = 0) |
|
101 | |||
102 | /** |
||
103 | * @param int|string $number |
||
104 | * @return string |
||
105 | */ |
||
106 | 3 | View Code Duplication | public static function floor($number) |
120 | |||
121 | /** |
||
122 | * @param int|string $number |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | public static function abs($number) |
|
135 | |||
136 | /** |
||
137 | * @param int|string $min |
||
138 | * @param int|string $max |
||
139 | * @return string |
||
140 | */ |
||
141 | 1 | public static function rand($min, $max) |
|
151 | |||
152 | /** |
||
153 | * @param array|int|string,... |
||
154 | * @return null|int|string |
||
155 | */ |
||
156 | 1 | View Code Duplication | public static function max() |
175 | |||
176 | /** |
||
177 | * @param array|int|string,... |
||
178 | * @return null|int|string |
||
179 | */ |
||
180 | 1 | View Code Duplication | public static function min() |
199 | |||
200 | /** |
||
201 | * @param int|string $number |
||
202 | * @param int $precision |
||
203 | * @return string |
||
204 | */ |
||
205 | 1 | View Code Duplication | public static function roundDown($number, $precision = 0) |
212 | |||
213 | /** |
||
214 | * @param int|string $number |
||
215 | * @param int $precision |
||
216 | * @return string |
||
217 | */ |
||
218 | 1 | View Code Duplication | public static function roundUp($number, $precision = 0) |
225 | |||
226 | /** |
||
227 | * @return int |
||
228 | */ |
||
229 | 1 | public static function getScale() |
|
234 | |||
235 | /** |
||
236 | * @param string $leftOperand |
||
237 | * @param string $rightOperand |
||
238 | * @param int $scale |
||
239 | * @return string |
||
240 | */ |
||
241 | 7 | public static function add($leftOperand, $rightOperand, $scale = null) |
|
245 | |||
246 | /** |
||
247 | * @param string $leftOperand |
||
248 | * @param string $rightOperand |
||
249 | * @param int $scale |
||
250 | * @return int |
||
251 | */ |
||
252 | 3 | public static function comp($leftOperand, $rightOperand, $scale = null) |
|
256 | |||
257 | /** |
||
258 | * @param string $leftOperand |
||
259 | * @param string $rightOperand |
||
260 | * @param int $scale |
||
261 | * @return string |
||
262 | */ |
||
263 | 5 | public static function div($leftOperand, $rightOperand, $scale = null) |
|
270 | |||
271 | /** |
||
272 | * @param string $leftOperand |
||
273 | * @param string $modulus |
||
274 | * @return string |
||
275 | */ |
||
276 | 1 | public static function mod($leftOperand, $modulus) |
|
280 | |||
281 | /** |
||
282 | * @param string $leftOperand |
||
283 | * @param string $modulus |
||
284 | * @param int $scale |
||
285 | * @return string |
||
286 | */ |
||
287 | 1 | public static function fmod($leftOperand, $modulus, $scale = null) |
|
305 | |||
306 | /** |
||
307 | * @param string $leftOperand |
||
308 | * @param string $rightOperand |
||
309 | * @param int $scale |
||
310 | * @return string |
||
311 | */ |
||
312 | 5 | public static function mul($leftOperand, $rightOperand, $scale = null) |
|
319 | |||
320 | /** |
||
321 | * @param string $leftOperand |
||
322 | * @param string $rightOperand |
||
323 | * @param int $scale |
||
324 | * @return string |
||
325 | */ |
||
326 | 3 | public static function pow($leftOperand, $rightOperand, $scale = null) |
|
330 | |||
331 | /** |
||
332 | * @param string $leftOperand |
||
333 | * @param string $rightOperand |
||
334 | * @param string $modulus |
||
335 | * @param int $scale |
||
336 | * @return string |
||
337 | */ |
||
338 | 1 | public static function powMod($leftOperand, $rightOperand, $modulus, $scale = null) |
|
342 | |||
343 | /** |
||
344 | * @param string $operand |
||
345 | * @param int $scale |
||
346 | * @return string |
||
347 | */ |
||
348 | 1 | public static function sqrt($operand, $scale = null) |
|
352 | |||
353 | /** |
||
354 | * @param string $leftOperand |
||
355 | * @param string $rightOperand |
||
356 | * @param int $scale |
||
357 | * @return string |
||
358 | */ |
||
359 | 4 | public static function sub($leftOperand, $rightOperand, $scale = null) |
|
363 | } |
||
364 |
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.