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 Html 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 Html, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Html |
||
40 | { |
||
41 | /** |
||
42 | * Default variables for Html utility set |
||
43 | * |
||
44 | * @type array $defaults array of default variables |
||
45 | */ |
||
46 | public static $defaults = [ |
||
47 | "pager_pagesize" => 10, |
||
48 | "pager_numlinks" => 10, |
||
49 | |||
50 | "pager_firstlast" => true, |
||
51 | "pager_first" => "<<", |
||
52 | "pager_prev" => "<", |
||
53 | "pager_next" => ">", |
||
54 | "pager_last" => ">>", |
||
55 | |||
56 | "pager_divider" => "", |
||
57 | "pager_dots" => " ... ", |
||
58 | |||
59 | "table_table" => "<table>", |
||
60 | "table_header" => "<th>{value}</th>", |
||
61 | "table_cell" => "<td>{value}</td>" |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Set of attribute placement order |
||
66 | * |
||
67 | * @type array $attributeOrder array of attribute orders |
||
68 | */ |
||
69 | public static $attributeOrder = [ |
||
70 | "action", "method", "type", "id", "name", "value", |
||
71 | "href", "src", "width", "height", "cols", "rows", |
||
72 | "size", "maxlength", "rel", "media", "accept-charset", |
||
73 | "accept", "tabindex", "accesskey", "alt", "title", "class", |
||
74 | "style", "selected", "checked", "readonly", "disabled" |
||
75 | ]; |
||
76 | |||
77 | |||
78 | /** |
||
79 | * Constructor to prevent new instances of Html class |
||
80 | * |
||
81 | * @return Html |
||
|
|||
82 | */ |
||
83 | final private function __construct() |
||
86 | |||
87 | /** |
||
88 | * Clone method to prevent duplication of Html class |
||
89 | * |
||
90 | * @return Html |
||
91 | */ |
||
92 | final private function __clone() |
||
95 | |||
96 | /** |
||
97 | * Unserialization method to prevent restoration of Html class |
||
98 | * |
||
99 | * @return Html |
||
100 | */ |
||
101 | final private function __wakeup() |
||
104 | |||
105 | /** |
||
106 | * Sets the default variables |
||
107 | * |
||
108 | * @param array $uDefaults variables to be set |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public static function setDefaults($uDefaults) |
||
116 | |||
117 | /** |
||
118 | * Creates an Html tag |
||
119 | * |
||
120 | * @param string $uName name of the element |
||
121 | * @param array $uAttributes set of the tag attributes |
||
122 | * @param null|string $uValue value |
||
123 | * |
||
124 | * @return string html output |
||
125 | */ |
||
126 | public static function tag($uName, array $uAttributes = [], $uValue = null) |
||
141 | |||
142 | /** |
||
143 | * Creates attributes array |
||
144 | * |
||
145 | * @param array $uAttributes set of the tag attributes |
||
146 | * |
||
147 | * @return string html output |
||
148 | */ |
||
149 | public static function attributes(array $uAttributes) |
||
165 | |||
166 | /** |
||
167 | * Creates options array for select element |
||
168 | * |
||
169 | * @param array $uOptions set of values |
||
170 | * @param mixed $uDefault default selected value |
||
171 | * @param mixed $uField field key for array values |
||
172 | * @param string $uExtra additional markup for each option tag |
||
173 | * |
||
174 | * @return string html output |
||
175 | */ |
||
176 | public static function selectOptions(array $uOptions, $uDefault = null, $uField = null, $uExtra = "") |
||
191 | |||
192 | /** |
||
193 | * Creates options array for select element and returns it in array |
||
194 | * |
||
195 | * @param array $uOptions set of values |
||
196 | * @param mixed $uDefault default selected value |
||
197 | * @param mixed $uField field key for array values |
||
198 | * @param string $uExtra additional markup for each option tag |
||
199 | * |
||
200 | * @return string set of html outputs |
||
201 | */ |
||
202 | public static function selectOptionsArray(array $uOptions, $uDefault = null, $uField = null, $uExtra = "") |
||
218 | |||
219 | /** |
||
220 | * Creates options array for input type="radio" element |
||
221 | * |
||
222 | * @param string $uName name of the element |
||
223 | * @param array $uOptions set of values |
||
224 | * @param mixed $uDefault default selected value |
||
225 | * @param mixed $uField field key for array values |
||
226 | * @param string $uExtra additional markup for each option tag |
||
227 | * |
||
228 | * @return string html output |
||
229 | */ |
||
230 | public static function radioOptions($uName, array $uOptions, $uDefault = null, $uField = null, $uExtra = "") |
||
258 | |||
259 | /** |
||
260 | * Creates options array for input type="radio" element and returns it in array |
||
261 | * |
||
262 | * @param string $uName name of the element |
||
263 | * @param array $uOptions set of values |
||
264 | * @param mixed $uDefault default selected value |
||
265 | * @param mixed $uField field key for array values |
||
266 | * @param string $uExtra additional markup for each option tag |
||
267 | * |
||
268 | * @return string set of html outputs |
||
269 | */ |
||
270 | public static function radioOptionsArray($uName, array $uOptions, $uDefault = null, $uField = null, $uExtra = "") |
||
299 | |||
300 | /** |
||
301 | * Creates a textbox element |
||
302 | * |
||
303 | * @param string $uName name of the element |
||
304 | * @param mixed $uValue default value |
||
305 | * @param array $uAttributes set of the tag attributes |
||
306 | * |
||
307 | * @return string html output |
||
308 | */ |
||
309 | public static function textBox($uName, $uValue = "", array $uAttributes = []) |
||
318 | |||
319 | /** |
||
320 | * Creates a checkbox element |
||
321 | * |
||
322 | * @param string $uName name of the element |
||
323 | * @param mixed $uValue value |
||
324 | * @param mixed $uCurrentValue default value |
||
325 | * @param string $uText caption |
||
326 | * @param array $uAttributes set of the tag attributes |
||
327 | * |
||
328 | * @return string html output |
||
329 | */ |
||
330 | public static function checkBox($uName, $uValue, $uCurrentValue = null, $uText = null, array $uAttributes = []) |
||
349 | |||
350 | /** |
||
351 | * Returns doctype header to be printed out |
||
352 | * |
||
353 | * @param string $uType the type of the document |
||
354 | * |
||
355 | * @return bool|string html output |
||
356 | */ |
||
357 | public static function doctype($uType = "html5") |
||
400 | |||
401 | /** |
||
402 | * Creates a script element |
||
403 | * |
||
404 | * @param string $uHref hypertext reference of script file |
||
405 | * @param array $uAttributes set of the tag attributes |
||
406 | * |
||
407 | * @return string html output |
||
408 | */ |
||
409 | View Code Duplication | public static function script($uHref, array $uAttributes = []) |
|
417 | |||
418 | /** |
||
419 | * Creates an inline script element |
||
420 | * |
||
421 | * @param string $uScriptContent script content |
||
422 | * @param array $uAttributes set of the tag attributes |
||
423 | * |
||
424 | * @return string html output |
||
425 | */ |
||
426 | View Code Duplication | public static function scriptInline($uScriptContent, array $uAttributes = []) |
|
434 | |||
435 | /** |
||
436 | * Creates a link element |
||
437 | * |
||
438 | * @param string $uRelation relation type |
||
439 | * @param string $uHref hypertext reference of linked file |
||
440 | * @param array $uAttributes set of the tag attributes |
||
441 | * |
||
442 | * @return string html output |
||
443 | */ |
||
444 | View Code Duplication | public static function link($uRelation, $uHref, array $uAttributes = []) |
|
453 | |||
454 | /** |
||
455 | * Creates a link element to link a stylesheet file |
||
456 | * |
||
457 | * @param string $uHref hypertext reference of linked file |
||
458 | * @param string $uMediaType target media type |
||
459 | * @param array $uAttributes set of the tag attributes |
||
460 | * |
||
461 | * @return string html output |
||
462 | */ |
||
463 | public static function linkStyleSheet($uHref, $uMediaType = "all", array $uAttributes = []) |
||
473 | |||
474 | /** |
||
475 | * Creates a pager widget |
||
476 | * |
||
477 | * @param int $uCurrent current page |
||
478 | * @param int $uTotal total number of pages |
||
479 | * @param array $uVariables variables |
||
480 | * |
||
481 | * @return string html output |
||
482 | */ |
||
483 | public static function pager($uCurrent, $uTotal, array $uVariables) |
||
638 | |||
639 | /** |
||
640 | * Creates a table widget |
||
641 | * |
||
642 | * @param array $uHeaders table headers |
||
643 | * @param iterable $uData table data |
||
644 | * @param array $uVariables variables |
||
645 | * |
||
646 | * @return string html output |
||
647 | */ |
||
648 | public static function table(array $uHeaders, $uData, array $uVariables) |
||
684 | } |
||
685 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.