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 |
||
31 | class ElemInput implements ModelInterface, ElemInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sName; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $sValue; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $sType; |
||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $sDataType; |
||
49 | /** |
||
50 | * @var integer |
||
51 | */ |
||
52 | private $iMaxLength; |
||
53 | /** |
||
54 | * @var \mrcnpdlk\Grandstream\XMLApp\Application\Model\Styles |
||
55 | */ |
||
56 | private $oStyles; |
||
57 | /** |
||
58 | * Only for Radio |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $sGroupName; |
||
63 | /** |
||
64 | * Only for Radio |
||
65 | * |
||
66 | * @var boolean |
||
67 | */ |
||
68 | private $isSelected; |
||
69 | /** |
||
70 | * Only for Radio & CheckBox |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $sLabel; |
||
75 | |||
76 | /** |
||
77 | * ElemInput constructor. |
||
78 | * |
||
79 | * @param string $sName |
||
80 | * @param string|null $sValue |
||
81 | * @param string $sType |
||
82 | */ |
||
83 | 1 | public function __construct(string $sName, string $sValue = null, string $sType = ModelConstant::TYPE_TEXT) |
|
91 | |||
92 | /** |
||
93 | * @param string $sDataType |
||
94 | * |
||
95 | * @return ElemInput |
||
96 | */ |
||
97 | 1 | public function setDataType(string $sDataType = ModelConstant::DATATYPE_STRING) |
|
103 | |||
104 | /** |
||
105 | * @param Styles $oStyles |
||
106 | * |
||
107 | * @return ElemInput |
||
108 | */ |
||
109 | 1 | View Code Duplication | public function setStyles(Styles $oStyles = null) |
118 | |||
119 | /** |
||
120 | * @param int $iMaxLength |
||
121 | * |
||
122 | * @return ElemInput |
||
123 | */ |
||
124 | public function setMaxLength(int $iMaxLength) |
||
130 | |||
131 | /** |
||
132 | * @param string $sGroupName |
||
133 | * |
||
134 | * @return ElemInput |
||
135 | */ |
||
136 | public function setGroupName(string $sGroupName) |
||
142 | |||
143 | /** |
||
144 | * @param string $sLabel |
||
145 | * |
||
146 | * @return ElemInput |
||
147 | */ |
||
148 | public function setLabel(string $sLabel) |
||
155 | |||
156 | /** |
||
157 | * @param bool $isSelected |
||
158 | * |
||
159 | * @return ElemInput |
||
160 | */ |
||
161 | public function setSelected(bool $isSelected = true) |
||
167 | |||
168 | /** |
||
169 | * @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
||
170 | */ |
||
171 | 1 | public function getXml(): MyXML |
|
184 | |||
185 | /** |
||
186 | * @return \mrcnpdlk\Grandstream\XMLApp\Application\Model\Styles |
||
187 | */ |
||
188 | 1 | public function getStyles() |
|
192 | |||
193 | /** |
||
194 | * @param int $iX |
||
195 | * @param int $iY |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function move(int $iX, int $iY) |
||
205 | } |
||
206 |
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.