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 |
||
| 9 | class Adaline extends Perceptron |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Batch training is the default Adaline training algorithm |
||
| 14 | */ |
||
| 15 | const BATCH_TRAINING = 1; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Online training: Stochastic gradient descent learning |
||
| 19 | */ |
||
| 20 | const ONLINE_TRAINING = 2; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Training type may be either 'Batch' or 'Online' learning |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $trainingType; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Initalize an Adaline (ADAptive LInear NEuron) classifier with given learning rate and maximum |
||
| 31 | * number of iterations used while training the classifier <br> |
||
| 32 | * |
||
| 33 | * Learning rate should be a float value between 0.0(exclusive) and 1.0 (inclusive) <br> |
||
| 34 | * Maximum number of iterations can be an integer value greater than 0 <br> |
||
| 35 | * If normalizeInputs is set to true, then every input given to the algorithm will be standardized |
||
| 36 | * by use of standard deviation and mean calculation |
||
| 37 | * |
||
| 38 | * @param int $learningRate |
||
| 39 | * @param int $maxIterations |
||
| 40 | */ |
||
| 41 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Adapts the weights with respect to given samples and targets |
||
| 55 | * by use of gradient descent learning rule |
||
| 56 | */ |
||
| 57 | protected function runTraining() |
||
| 74 | } |
||
| 75 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.