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 Number extends NamedElement |
||
12 | { |
||
13 | protected $type = 'number'; |
||
14 | |||
15 | /** |
||
16 | * the maximum allowed value |
||
17 | * |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $max; |
||
21 | |||
22 | /** |
||
23 | * the minimum allowed value |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $min; |
||
28 | |||
29 | /** |
||
30 | * incremental step |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $step = 1; |
||
35 | |||
36 | /** |
||
37 | * @return string|int |
||
38 | */ |
||
39 | public function getMax() |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | protected function getDefaultMax() |
||
55 | |||
56 | /** |
||
57 | * @param int $value |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setMax(int $value) |
||
68 | |||
69 | /** |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getMin() |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | protected function getDefaultMin() |
||
88 | |||
89 | /** |
||
90 | * @param int $value |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setMin(int $value) |
||
101 | |||
102 | /** |
||
103 | * @return int |
||
104 | */ |
||
105 | public function getStep() |
||
109 | |||
110 | /** |
||
111 | * @param int $value |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setStep(int $value) |
||
120 | |||
121 | protected function getDefaultValidationRules() |
||
127 | |||
128 | View Code Duplication | public function toArray() |
|
136 | } |
||
137 |
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.