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:
1 | <?php |
||
13 | class Math |
||
14 | { |
||
15 | |||
16 | const ERROR_ARRAY_LENGTH = 'The length of the arrays does not match'; |
||
17 | |||
18 | /** |
||
19 | * The quadratic mean square error |
||
20 | * |
||
21 | * @param array $rightData |
||
22 | * @param array $hypothesisData |
||
23 | * @return float |
||
24 | * @throws IllegalArgumentException |
||
25 | */ |
||
26 | public static function mse(array $rightData, array $hypothesisData): float |
||
40 | |||
41 | /** |
||
42 | * Total Sum of Squares |
||
43 | * |
||
44 | * @param array $rightData |
||
45 | * @return float |
||
46 | */ |
||
47 | public static function tss(array $rightData): float |
||
57 | |||
58 | /** |
||
59 | * @param array $rightData |
||
60 | * @param array $hypothesisData |
||
61 | * @return float |
||
62 | * @throws IllegalArgumentException |
||
63 | */ |
||
64 | public static function r2(array $rightData, array $hypothesisData):float |
||
68 | |||
69 | /** |
||
70 | * Mean absolute percentage error |
||
71 | * |
||
72 | * @param array $rightData |
||
73 | * @param array $hypothesisData |
||
74 | * @return float |
||
75 | * @throws IllegalArgumentException |
||
76 | */ |
||
77 | View Code Duplication | public static function mape(array $rightData, array $hypothesisData): float |
|
90 | |||
91 | /** |
||
92 | * Mean absolute error |
||
93 | * |
||
94 | * @param array $rightData |
||
95 | * @param array $hypothesisData |
||
96 | * @return float |
||
97 | * @throws IllegalArgumentException |
||
98 | */ |
||
99 | View Code Duplication | public static function mae(array $rightData, array $hypothesisData): float |
|
112 | |||
113 | /** |
||
114 | * 1- mae |
||
115 | * |
||
116 | * @param array $rightData |
||
117 | * @param array $hypothesisData |
||
118 | * @return float |
||
119 | * @throws IllegalArgumentException |
||
120 | */ |
||
121 | public static function mpmae(array $rightData, array $hypothesisData): float |
||
125 | |||
126 | /** |
||
127 | * The accuracy of the forecast |
||
128 | * |
||
129 | * @param array $rightData |
||
130 | * @param array $hypothesisData |
||
131 | * @return float |
||
132 | * @throws IllegalArgumentException |
||
133 | */ |
||
134 | public static function mpe(array $rightData, array $hypothesisData): float |
||
138 | |||
139 | /** |
||
140 | * Moving Average |
||
141 | * |
||
142 | * @param array $vector |
||
143 | * @return array |
||
144 | */ |
||
145 | public static function tripleMovingAverage(array $vector): array |
||
158 | } |
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.