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 |
||
11 | class Normalizer implements Preprocessor |
||
12 | { |
||
13 | const NORM_L1 = 1; |
||
14 | const NORM_L2 = 2; |
||
15 | const NORM_STD= 3; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $norm; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $fitted = false; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $std; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $mean; |
||
36 | |||
37 | /** |
||
38 | * @param int $norm |
||
39 | * |
||
40 | * @throws NormalizerException |
||
41 | */ |
||
42 | public function __construct(int $norm = self::NORM_L2) |
||
50 | |||
51 | /** |
||
52 | * @param array $samples |
||
53 | */ |
||
54 | public function fit(array $samples) |
||
71 | |||
72 | /** |
||
73 | * @param array $samples |
||
74 | */ |
||
75 | public function transform(array &$samples) |
||
90 | |||
91 | /** |
||
92 | * @param array $sample |
||
93 | */ |
||
94 | View Code Duplication | private function normalizeL1(array &$sample) |
|
110 | |||
111 | /** |
||
112 | * @param array $sample |
||
113 | */ |
||
114 | View Code Duplication | private function normalizeL2(array &$sample) |
|
130 | |||
131 | /** |
||
132 | * @param array $sample |
||
133 | */ |
||
134 | private function normalizeSTD(array &$sample) |
||
140 | } |
||
141 |
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.