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:
Complex classes like form_Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use form_Core, and based on these observations, apply Extract Interface, too.
1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
12 | class form_Core |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Generates an opening HTML form tag. |
||
17 | * |
||
18 | * @param string form action attribute |
||
19 | * @param array extra attributes |
||
20 | * @param array hidden fields to be created immediately after the form tag |
||
21 | * @return string |
||
22 | */ |
||
23 | public static function open($action = null, $attr = array(), $hidden = null) |
||
52 | |||
53 | /** |
||
54 | * Generates an opening HTML form tag that can be used for uploading files. |
||
55 | * |
||
56 | * @param string form action attribute |
||
57 | * @param array extra attributes |
||
58 | * @param array hidden fields to be created immediately after the form tag |
||
59 | * @return string |
||
60 | */ |
||
61 | public static function open_multipart($action = null, $attr = array(), $hidden = array()) |
||
68 | |||
69 | /** |
||
70 | * Generates a fieldset opening tag. |
||
71 | * |
||
72 | * @param array html attributes |
||
73 | * @param string a string to be attached to the end of the attributes |
||
74 | * @return string |
||
75 | */ |
||
76 | public static function open_fieldset($data = null, $extra = '') |
||
80 | |||
81 | /** |
||
82 | * Generates a fieldset closing tag. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public static function close_fieldset() |
||
90 | |||
91 | /** |
||
92 | * Generates a legend tag for use with a fieldset. |
||
93 | * |
||
94 | * @param string legend text |
||
95 | * @param array HTML attributes |
||
96 | * @param string a string to be attached to the end of the attributes |
||
97 | * @return string |
||
98 | */ |
||
99 | public static function legend($text = '', $data = null, $extra = '') |
||
103 | |||
104 | /** |
||
105 | * Generates hidden form fields. |
||
106 | * You can pass a simple key/value string or an associative array with multiple values. |
||
107 | * |
||
108 | * @param string|array input name (string) or key/value pairs (array) |
||
109 | * @param string input value, if using an input name |
||
110 | * @return string |
||
111 | */ |
||
112 | public static function hidden($data, $value = '') |
||
133 | |||
134 | /** |
||
135 | * Creates an HTML form input tag. Defaults to a text type. |
||
136 | * |
||
137 | * @param string|array input name or an array of HTML attributes |
||
138 | * @param string input value, when using a name |
||
139 | * @param string a string to be attached to the end of the attributes |
||
140 | * @return string |
||
141 | */ |
||
142 | public static function input($data, $value = '', $extra = '') |
||
156 | |||
157 | /** |
||
158 | * Creates a HTML form password input tag. |
||
159 | * |
||
160 | * @param string|array input name or an array of HTML attributes |
||
161 | * @param string input value, when using a name |
||
162 | * @param string a string to be attached to the end of the attributes |
||
163 | * @return string |
||
164 | */ |
||
165 | View Code Duplication | public static function password($data, $value = '', $extra = '') |
|
175 | |||
176 | /** |
||
177 | * Creates an HTML form upload input tag. |
||
178 | * |
||
179 | * @param string|array input name or an array of HTML attributes |
||
180 | * @param string input value, when using a name |
||
181 | * @param string a string to be attached to the end of the attributes |
||
182 | * @return string |
||
183 | */ |
||
184 | View Code Duplication | public static function upload($data, $value = '', $extra = '') |
|
194 | |||
195 | /** |
||
196 | * Creates an HTML form textarea tag. |
||
197 | * |
||
198 | * @param string|array input name or an array of HTML attributes |
||
199 | * @param string input value, when using a name |
||
200 | * @param string a string to be attached to the end of the attributes |
||
201 | * @param boolean encode existing entities |
||
202 | * @return string |
||
203 | */ |
||
204 | public static function textarea($data, $value = '', $extra = '', $double_encode = true) |
||
218 | |||
219 | /** |
||
220 | * Creates an HTML form select tag, or "dropdown menu". |
||
221 | * |
||
222 | * @param string|array input name or an array of HTML attributes |
||
223 | * @param array select options, when using a name |
||
224 | * @param string|array option key(s) that should be selected by default |
||
225 | * @param string a string to be attached to the end of the attributes |
||
226 | * @return string |
||
227 | */ |
||
228 | public static function dropdown($data, $options = null, $selected = null, $extra = '') |
||
276 | |||
277 | /** |
||
278 | * Creates an HTML form checkbox input tag. |
||
279 | * |
||
280 | * @param string|array input name or an array of HTML attributes |
||
281 | * @param string input value, when using a name |
||
282 | * @param boolean make the checkbox checked by default |
||
283 | * @param string a string to be attached to the end of the attributes |
||
284 | * @return string |
||
285 | */ |
||
286 | View Code Duplication | public static function checkbox($data, $value = '', $checked = false, $extra = '') |
|
302 | |||
303 | /** |
||
304 | * Creates an HTML form radio input tag. |
||
305 | * |
||
306 | * @param string|array input name or an array of HTML attributes |
||
307 | * @param string input value, when using a name |
||
308 | * @param boolean make the radio selected by default |
||
309 | * @param string a string to be attached to the end of the attributes |
||
310 | * @return string |
||
311 | */ |
||
312 | View Code Duplication | public static function radio($data = '', $value = '', $checked = false, $extra = '') |
|
328 | |||
329 | /** |
||
330 | * Creates an HTML form submit input tag. |
||
331 | * |
||
332 | * @param string|array input name or an array of HTML attributes |
||
333 | * @param string input value, when using a name |
||
334 | * @param string a string to be attached to the end of the attributes |
||
335 | * @return string |
||
336 | */ |
||
337 | public static function submit($data = '', $value = '', $extra = '') |
||
352 | |||
353 | /** |
||
354 | * Creates an HTML form button input tag. |
||
355 | * |
||
356 | * @param string|array input name or an array of HTML attributes |
||
357 | * @param string input value, when using a name |
||
358 | * @param string a string to be attached to the end of the attributes |
||
359 | * @return string |
||
360 | */ |
||
361 | public static function button($data = '', $value = '', $extra = '') |
||
378 | |||
379 | /** |
||
380 | * Closes an open form tag. |
||
381 | * |
||
382 | * @param string string to be attached after the closing tag |
||
383 | * @return string |
||
384 | */ |
||
385 | public static function close($extra = '') |
||
389 | |||
390 | /** |
||
391 | * Creates an HTML form label tag. |
||
392 | * |
||
393 | * @param string|array label "for" name or an array of HTML attributes |
||
394 | * @param string label text or HTML |
||
395 | * @param string a string to be attached to the end of the attributes |
||
396 | * @return string |
||
397 | */ |
||
398 | public static function label($data = '', $text = null, $extra = '') |
||
417 | |||
418 | /** |
||
419 | * Sorts a key/value array of HTML attributes, putting form attributes first, |
||
420 | * and returns an attribute string. |
||
421 | * |
||
422 | * @param array HTML attributes array |
||
423 | * @return string |
||
424 | */ |
||
425 | public static function attributes($attr, $type = null) |
||
493 | } // End form |
||
494 |
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.