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 |
||
12 | class AdaBoost implements Classifier |
||
13 | { |
||
14 | use Predictable, Trainable; |
||
15 | |||
16 | /** |
||
17 | * Actual labels given in the targets array |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $labels = []; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $sampleCount; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $featureCount; |
||
31 | |||
32 | /** |
||
33 | * Number of maximum iterations to be done |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $maxIterations; |
||
38 | |||
39 | /** |
||
40 | * Sample weights |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $weights = []; |
||
45 | |||
46 | /** |
||
47 | * Base classifiers |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $classifiers = []; |
||
52 | |||
53 | /** |
||
54 | * Base classifier weights |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $alpha = []; |
||
59 | |||
60 | /** |
||
61 | * ADAptive BOOSTing (AdaBoost) is an ensemble algorithm to |
||
62 | * improve classification performance of 'weak' classifiers such as |
||
63 | * DecisionStump (default base classifier of AdaBoost). |
||
64 | * |
||
65 | */ |
||
66 | public function __construct(int $maxIterations = 30) |
||
70 | |||
71 | /** |
||
72 | * @param array $samples |
||
73 | * @param array $targets |
||
74 | */ |
||
75 | public function train(array $samples, array $targets) |
||
111 | |||
112 | /** |
||
113 | * Returns the classifier with the lowest error rate with the |
||
114 | * consideration of current sample weights |
||
115 | * |
||
116 | * @return Classifier |
||
117 | */ |
||
118 | protected function getBestClassifier() |
||
139 | |||
140 | /** |
||
141 | * Calculates alpha of a classifier |
||
142 | * |
||
143 | * @param float $errorRate |
||
144 | * @return float |
||
145 | */ |
||
146 | protected function calculateAlpha(float $errorRate) |
||
153 | |||
154 | /** |
||
155 | * Updates the sample weights |
||
156 | * |
||
157 | * @param DecisionStump $classifier |
||
158 | * @param float $alpha |
||
159 | */ |
||
160 | protected function updateWeights(DecisionStump $classifier, float $alpha) |
||
175 | |||
176 | /** |
||
177 | * @param array $sample |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function predictSample(array $sample) |
||
190 | } |
||
191 |
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.