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 /** MicroHtml */ |
||
17 | class Html |
||
18 | { |
||
19 | use TagTrait, HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait { |
||
20 | TagTrait::openTag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
21 | TagTrait::closeTag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
22 | TagTrait::tag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
23 | TagTrait::br insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
24 | TagTrait::embed insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
25 | TagTrait::heading insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
26 | TagTrait::href insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
27 | TagTrait::image insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
28 | TagTrait::imageMap insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
29 | TagTrait::lists insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
30 | TagTrait::mailto insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
31 | TagTrait::object insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait; |
||
32 | } |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Render begin form tag |
||
37 | * |
||
38 | * @access public |
||
39 | * |
||
40 | * @param string $action path to URL action |
||
41 | * @param string $method method of request |
||
42 | * @param array $attributes attributes tag |
||
43 | * |
||
44 | * @return string |
||
45 | * @static |
||
46 | */ |
||
47 | View Code Duplication | public static function beginForm($action, $method = 'POST', array $attributes = []) |
|
54 | |||
55 | /** |
||
56 | * Render end form tag |
||
57 | * |
||
58 | * @access public |
||
59 | * @return string |
||
60 | * @static |
||
61 | */ |
||
62 | public static function endForm() |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Render image button tag |
||
70 | * |
||
71 | * @access public |
||
72 | * |
||
73 | * @param string $name image name |
||
74 | * @param string $file image file path |
||
75 | * @param array $attributesButton attributes for button |
||
76 | * @param array $attributesImage attributes for image |
||
77 | * |
||
78 | * @return string |
||
79 | * @static |
||
80 | */ |
||
81 | public static function imageButton($name, $file, array $attributesButton = [], array $attributesImage = []) |
||
85 | |||
86 | /** |
||
87 | * Render button tag |
||
88 | * |
||
89 | * @access public |
||
90 | * |
||
91 | * @param string $text text for button |
||
92 | * @param array $attributes attributes tag |
||
93 | * |
||
94 | * @return string |
||
95 | * @static |
||
96 | */ |
||
97 | public static function button($text, array $attributes = []) |
||
101 | |||
102 | /** |
||
103 | * Render textArea tag |
||
104 | * |
||
105 | * @access public |
||
106 | * |
||
107 | * @param string $name textArea name |
||
108 | * @param string $text textArea text |
||
109 | * @param array $attributes attributes tag |
||
110 | * |
||
111 | * @return string |
||
112 | * @static |
||
113 | */ |
||
114 | View Code Duplication | public static function textArea($name, $text, array $attributes = []) |
|
121 | |||
122 | /** |
||
123 | * Render legend tag |
||
124 | * |
||
125 | * @access public |
||
126 | * |
||
127 | * @param string $text legend text |
||
128 | * @param array $attributes attributes tag |
||
129 | * |
||
130 | * @return string |
||
131 | * @static |
||
132 | */ |
||
133 | public static function legend($text, array $attributes = []) |
||
137 | |||
138 | /** |
||
139 | * Render label tag |
||
140 | * |
||
141 | * @access public |
||
142 | * |
||
143 | * @param string $name label name |
||
144 | * @param string $elemId element ID |
||
145 | * @param array $attributes attributes tag |
||
146 | * |
||
147 | * @return string |
||
148 | * @static |
||
149 | */ |
||
150 | public static function label($name, $elemId = '', array $attributes = []) |
||
156 | |||
157 | /** |
||
158 | * Render dropDownList (select tag) |
||
159 | * |
||
160 | * @access public |
||
161 | * |
||
162 | * @param string $name dropDown name |
||
163 | * @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
||
164 | * @param array $attributes attributes tag |
||
165 | * |
||
166 | * @return string |
||
167 | * @static |
||
168 | */ |
||
169 | public static function dropDownList($name, array $options = [], array $attributes = []) |
||
176 | |||
177 | /** |
||
178 | * Render listBox (select tag) |
||
179 | * |
||
180 | * @access public |
||
181 | * |
||
182 | * @param string $name listBox name |
||
183 | * @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
||
184 | * @param array $attributes attributes tag |
||
185 | * |
||
186 | * @return string |
||
187 | * @static |
||
188 | */ |
||
189 | public static function listBox($name, array $options = [], array $attributes = []) |
||
219 | |||
220 | /** |
||
221 | * Render optGroup tag |
||
222 | * |
||
223 | * @access public |
||
224 | * |
||
225 | * @param string $label label for options group |
||
226 | * @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
||
227 | * @param array $attributes attributes tag |
||
228 | * |
||
229 | * @return string |
||
230 | * @static |
||
231 | */ |
||
232 | public static function optGroup($label, array $options = [], array $attributes = []) |
||
247 | |||
248 | /** |
||
249 | * Render option tag |
||
250 | * |
||
251 | * @access public |
||
252 | * |
||
253 | * @param string $value option value |
||
254 | * @param string $text label for option |
||
255 | * @param array $attributes attributes tag |
||
256 | * |
||
257 | * @return string |
||
258 | * @static |
||
259 | */ |
||
260 | public static function option($value, $text, array $attributes = []) |
||
266 | |||
267 | /** |
||
268 | * Converting array to options |
||
269 | * |
||
270 | * @param array $arr Input array |
||
271 | * |
||
272 | * @return array|null Output array |
||
273 | */ |
||
274 | public static function arrayToOptions(array $arr = []) |
||
284 | |||
285 | /** |
||
286 | * Render reset button tag |
||
287 | * |
||
288 | * @access public |
||
289 | * |
||
290 | * @param string $label text for label on button |
||
291 | * @param array $attributes attributes tag |
||
292 | * |
||
293 | * @return string |
||
294 | * @static |
||
295 | */ |
||
296 | View Code Duplication | public static function resetButton($label = 'Reset', array $attributes = []) |
|
303 | |||
304 | /** |
||
305 | * Render submit button tag |
||
306 | * |
||
307 | * @access public |
||
308 | * |
||
309 | * @param string $label text for label on button |
||
310 | * @param array $attributes attributes tag |
||
311 | * |
||
312 | * @return string |
||
313 | * @static |
||
314 | */ |
||
315 | View Code Duplication | public static function submitButton($label = 'Submit', array $attributes = []) |
|
322 | } |
||
323 |
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.