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 DecisionStump extends WeightedClassifier |
||
| 13 | { |
||
| 14 | use Trainable, Predictable; |
||
| 15 | |||
| 16 | const AUTO_SELECT = -1; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $givenColumnIndex; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * Sample weights : If used the optimization on the decision value |
||
| 26 | * will take these weights into account. If not given, all samples |
||
| 27 | * will be weighed with the same value of 1 |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $weights = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Lowest error rate obtained while training/optimizing the model |
||
| 35 | * |
||
| 36 | * @var float |
||
| 37 | */ |
||
| 38 | protected $trainingErrorRate; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $column; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var mixed |
||
| 47 | */ |
||
| 48 | protected $value; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $operator; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $columnTypes; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var float |
||
| 62 | */ |
||
| 63 | protected $numSplitCount = 10.0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * A DecisionStump classifier is a one-level deep DecisionTree. It is generally |
||
| 67 | * used with ensemble algorithms as in the weak classifier role. <br> |
||
| 68 | * |
||
| 69 | * If columnIndex is given, then the stump tries to produce a decision node |
||
| 70 | * on this column, otherwise in cases given the value of -1, the stump itself |
||
| 71 | * decides which column to take for the decision (Default DecisionTree behaviour) |
||
| 72 | * |
||
| 73 | * @param int $columnIndex |
||
| 74 | */ |
||
| 75 | public function __construct(int $columnIndex = self::AUTO_SELECT) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param array $samples |
||
| 82 | * @param array $targets |
||
| 83 | */ |
||
| 84 | public function train(array $samples, array $targets) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * While finding best split point for a numerical valued column, |
||
| 145 | * DecisionStump looks for equally distanced values between minimum and maximum |
||
| 146 | * values in the column. Given <i>$count</i> value determines how many split |
||
| 147 | * points to be probed. The more split counts, the better performance but |
||
| 148 | * worse processing time (Default value is 10.0) |
||
| 149 | * |
||
| 150 | * @param float $count |
||
| 151 | */ |
||
| 152 | public function setNumericalSplitCount(float $count) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Determines best split point for the given column |
||
| 159 | * |
||
| 160 | * @param int $col |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function getBestNumericalSplit(int $col) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * |
||
| 199 | * @param int $col |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | protected function getBestNominalSplit(int $col) |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | * @param type $leftValue |
||
| 229 | * @param type $operator |
||
| 230 | * @param type $rightValue |
||
| 231 | * |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | protected function evaluate($leftValue, $operator, $rightValue) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Calculates the ratio of wrong predictions based on the new threshold |
||
| 251 | * value given as the parameter |
||
| 252 | * |
||
| 253 | * @param float $threshold |
||
| 254 | * @param string $operator |
||
| 255 | * @param array $values |
||
| 256 | */ |
||
| 257 | protected function calculateErrorRate(float $threshold, string $operator, array $values) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $sample |
||
| 280 | * @return mixed |
||
| 281 | */ |
||
| 282 | protected function predictSample(array $sample) |
||
| 289 | |||
| 290 | public function __toString() |
||
| 294 | } |
||
| 295 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: