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 |
||
48 | class Html { |
||
49 | // List of void elements from HTML5, section 8.1.2 as of 2016-09-19 |
||
50 | private static $voidElements = [ |
||
51 | 'area', |
||
52 | 'base', |
||
53 | 'br', |
||
54 | 'col', |
||
55 | 'embed', |
||
56 | 'hr', |
||
57 | 'img', |
||
58 | 'input', |
||
59 | 'keygen', |
||
60 | 'link', |
||
61 | 'meta', |
||
62 | 'param', |
||
63 | 'source', |
||
64 | 'track', |
||
65 | 'wbr', |
||
66 | ]; |
||
67 | |||
68 | // Boolean attributes, which may have the value omitted entirely. Manually |
||
69 | // collected from the HTML5 spec as of 2011-08-12. |
||
70 | private static $boolAttribs = [ |
||
71 | 'async', |
||
72 | 'autofocus', |
||
73 | 'autoplay', |
||
74 | 'checked', |
||
75 | 'controls', |
||
76 | 'default', |
||
77 | 'defer', |
||
78 | 'disabled', |
||
79 | 'formnovalidate', |
||
80 | 'hidden', |
||
81 | 'ismap', |
||
82 | 'itemscope', |
||
83 | 'loop', |
||
84 | 'multiple', |
||
85 | 'muted', |
||
86 | 'novalidate', |
||
87 | 'open', |
||
88 | 'pubdate', |
||
89 | 'readonly', |
||
90 | 'required', |
||
91 | 'reversed', |
||
92 | 'scoped', |
||
93 | 'seamless', |
||
94 | 'selected', |
||
95 | 'truespeed', |
||
96 | 'typemustmatch', |
||
97 | // HTML5 Microdata |
||
98 | 'itemscope', |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * Modifies a set of attributes meant for button elements |
||
103 | * and apply a set of default attributes when $wgUseMediaWikiUIEverywhere enabled. |
||
104 | * @param array $attrs HTML attributes in an associative array |
||
105 | * @param string[] $modifiers classes to add to the button |
||
106 | * @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers |
||
107 | * @return array $attrs A modified attribute array |
||
108 | */ |
||
109 | public static function buttonAttributes( array $attrs, array $modifiers = [] ) { |
||
128 | |||
129 | /** |
||
130 | * Modifies a set of attributes meant for text input elements |
||
131 | * and apply a set of default attributes. |
||
132 | * Removes size attribute when $wgUseMediaWikiUIEverywhere enabled. |
||
133 | * @param array $attrs An attribute array. |
||
134 | * @return array $attrs A modified attribute array |
||
135 | */ |
||
136 | public static function getTextInputAttributes( array $attrs ) { |
||
151 | |||
152 | /** |
||
153 | * Returns an HTML link element in a string styled as a button |
||
154 | * (when $wgUseMediaWikiUIEverywhere is enabled). |
||
155 | * |
||
156 | * @param string $contents The raw HTML contents of the element: *not* |
||
157 | * escaped! |
||
158 | * @param array $attrs Associative array of attributes, e.g., [ |
||
159 | * 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for |
||
160 | * further documentation. |
||
161 | * @param string[] $modifiers classes to add to the button |
||
162 | * @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers |
||
163 | * @return string Raw HTML |
||
164 | */ |
||
165 | public static function linkButton( $contents, array $attrs, array $modifiers = [] ) { |
||
171 | |||
172 | /** |
||
173 | * Returns an HTML link element in a string styled as a button |
||
174 | * (when $wgUseMediaWikiUIEverywhere is enabled). |
||
175 | * |
||
176 | * @param string $contents The raw HTML contents of the element: *not* |
||
177 | * escaped! |
||
178 | * @param array $attrs Associative array of attributes, e.g., [ |
||
179 | * 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for |
||
180 | * further documentation. |
||
181 | * @param string[] $modifiers classes to add to the button |
||
182 | * @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers |
||
183 | * @return string Raw HTML |
||
184 | */ |
||
185 | public static function submitButton( $contents, array $attrs, array $modifiers = [] ) { |
||
190 | |||
191 | /** |
||
192 | * Returns an HTML element in a string. The major advantage here over |
||
193 | * manually typing out the HTML is that it will escape all attribute |
||
194 | * values. |
||
195 | * |
||
196 | * This is quite similar to Xml::tags(), but it implements some useful |
||
197 | * HTML-specific logic. For instance, there is no $allowShortTag |
||
198 | * parameter: the closing tag is magically omitted if $element has an empty |
||
199 | * content model. |
||
200 | * |
||
201 | * @param string $element The element's name, e.g., 'a' |
||
202 | * @param array $attribs Associative array of attributes, e.g., [ |
||
203 | * 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for |
||
204 | * further documentation. |
||
205 | * @param string $contents The raw HTML contents of the element: *not* |
||
206 | * escaped! |
||
207 | * @return string Raw HTML |
||
208 | */ |
||
209 | public static function rawElement( $element, $attribs = [], $contents = '' ) { |
||
218 | |||
219 | /** |
||
220 | * Identical to rawElement(), but HTML-escapes $contents (like |
||
221 | * Xml::element()). |
||
222 | * |
||
223 | * @param string $element |
||
224 | * @param array $attribs |
||
225 | * @param string $contents |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public static function element( $element, $attribs = [], $contents = '' ) { |
||
237 | |||
238 | /** |
||
239 | * Identical to rawElement(), but has no third parameter and omits the end |
||
240 | * tag (and the self-closing '/' in XML mode for empty elements). |
||
241 | * |
||
242 | * @param string $element |
||
243 | * @param array $attribs |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public static function openElement( $element, $attribs = [] ) { |
||
297 | |||
298 | /** |
||
299 | * Returns "</$element>" |
||
300 | * |
||
301 | * @since 1.17 |
||
302 | * @param string $element Name of the element, e.g., 'a' |
||
303 | * @return string A closing tag |
||
304 | */ |
||
305 | public static function closeElement( $element ) { |
||
310 | |||
311 | /** |
||
312 | * Given an element name and an associative array of element attributes, |
||
313 | * return an array that is functionally identical to the input array, but |
||
314 | * possibly smaller. In particular, attributes might be stripped if they |
||
315 | * are given their default values. |
||
316 | * |
||
317 | * This method is not guaranteed to remove all redundant attributes, only |
||
318 | * some common ones and some others selected arbitrarily at random. It |
||
319 | * only guarantees that the output array should be functionally identical |
||
320 | * to the input array (currently per the HTML 5 draft as of 2009-09-06). |
||
321 | * |
||
322 | * @param string $element Name of the element, e.g., 'a' |
||
323 | * @param array $attribs Associative array of attributes, e.g., [ |
||
324 | * 'href' => 'https://www.mediawiki.org/' ]. See expandAttributes() for |
||
325 | * further documentation. |
||
326 | * @return array An array of attributes functionally identical to $attribs |
||
327 | */ |
||
328 | private static function dropDefaults( $element, array $attribs ) { |
||
430 | |||
431 | /** |
||
432 | * Given an associative array of element attributes, generate a string |
||
433 | * to stick after the element name in HTML output. Like [ 'href' => |
||
434 | * 'https://www.mediawiki.org/' ] becomes something like |
||
435 | * ' href="https://www.mediawiki.org"'. Again, this is like |
||
436 | * Xml::expandAttributes(), but it implements some HTML-specific logic. |
||
437 | * |
||
438 | * Attributes that can contain space-separated lists ('class', 'accesskey' and 'rel') array |
||
439 | * values are allowed as well, which will automagically be normalized |
||
440 | * and converted to a space-separated string. In addition to a numerical |
||
441 | * array, the attribute value may also be an associative array. See the |
||
442 | * example below for how that works. |
||
443 | * |
||
444 | * @par Numerical array |
||
445 | * @code |
||
446 | * Html::element( 'em', [ |
||
447 | * 'class' => [ 'foo', 'bar' ] |
||
448 | * ] ); |
||
449 | * // gives '<em class="foo bar"></em>' |
||
450 | * @endcode |
||
451 | * |
||
452 | * @par Associative array |
||
453 | * @code |
||
454 | * Html::element( 'em', [ |
||
455 | * 'class' => [ 'foo', 'bar', 'foo' => false, 'quux' => true ] |
||
456 | * ] ); |
||
457 | * // gives '<em class="bar quux"></em>' |
||
458 | * @endcode |
||
459 | * |
||
460 | * @param array $attribs Associative array of attributes, e.g., [ |
||
461 | * 'href' => 'https://www.mediawiki.org/' ]. Values will be HTML-escaped. |
||
462 | * A value of false means to omit the attribute. For boolean attributes, |
||
463 | * you can omit the key, e.g., [ 'checked' ] instead of |
||
464 | * [ 'checked' => 'checked' ] or such. |
||
465 | * |
||
466 | * @throws MWException If an attribute that doesn't allow lists is set to an array |
||
467 | * @return string HTML fragment that goes between element name and '>' |
||
468 | * (starting with a space if at least one attribute is output) |
||
469 | */ |
||
470 | public static function expandAttributes( array $attribs ) { |
||
585 | |||
586 | /** |
||
587 | * Output a "<script>" tag with the given contents. |
||
588 | * |
||
589 | * @todo do some useful escaping as well, like if $contents contains |
||
590 | * literal "</script>" or (for XML) literal "]]>". |
||
591 | * |
||
592 | * @param string $contents JavaScript |
||
593 | * @return string Raw HTML |
||
594 | */ |
||
595 | public static function inlineScript( $contents ) { |
||
604 | |||
605 | /** |
||
606 | * Output a "<script>" tag linking to the given URL, e.g., |
||
607 | * "<script src=foo.js></script>". |
||
608 | * |
||
609 | * @param string $url |
||
610 | * @return string Raw HTML |
||
611 | */ |
||
612 | public static function linkedScript( $url ) { |
||
617 | |||
618 | /** |
||
619 | * Output a "<style>" tag with the given contents for the given media type |
||
620 | * (if any). TODO: do some useful escaping as well, like if $contents |
||
621 | * contains literal "</style>" (admittedly unlikely). |
||
622 | * |
||
623 | * @param string $contents CSS |
||
624 | * @param string $media A media type string, like 'screen' |
||
625 | * @return string Raw HTML |
||
626 | */ |
||
627 | public static function inlineStyle( $contents, $media = 'all' ) { |
||
647 | |||
648 | /** |
||
649 | * Output a "<link rel=stylesheet>" linking to the given URL for the given |
||
650 | * media type (if any). |
||
651 | * |
||
652 | * @param string $url |
||
653 | * @param string $media A media type string, like 'screen' |
||
654 | * @return string Raw HTML |
||
655 | */ |
||
656 | public static function linkedStyle( $url, $media = 'all' ) { |
||
663 | |||
664 | /** |
||
665 | * Convenience function to produce an "<input>" element. This supports the |
||
666 | * new HTML5 input types and attributes. |
||
667 | * |
||
668 | * @param string $name Name attribute |
||
669 | * @param string $value Value attribute |
||
670 | * @param string $type Type attribute |
||
671 | * @param array $attribs Associative array of miscellaneous extra |
||
672 | * attributes, passed to Html::element() |
||
673 | * @return string Raw HTML |
||
674 | */ |
||
675 | public static function input( $name, $value = '', $type = 'text', array $attribs = [] ) { |
||
687 | |||
688 | /** |
||
689 | * Convenience function to produce a checkbox (input element with type=checkbox) |
||
690 | * |
||
691 | * @param string $name Name attribute |
||
692 | * @param bool $checked Whether the checkbox is checked or not |
||
693 | * @param array $attribs Array of additional attributes |
||
694 | * @return string Raw HTML |
||
695 | */ |
||
696 | View Code Duplication | public static function check( $name, $checked = false, array $attribs = [] ) { |
|
710 | |||
711 | /** |
||
712 | * Convenience function to produce a radio button (input element with type=radio) |
||
713 | * |
||
714 | * @param string $name Name attribute |
||
715 | * @param bool $checked Whether the radio button is checked or not |
||
716 | * @param array $attribs Array of additional attributes |
||
717 | * @return string Raw HTML |
||
718 | */ |
||
719 | View Code Duplication | public static function radio( $name, $checked = false, array $attribs = [] ) { |
|
733 | |||
734 | /** |
||
735 | * Convenience function for generating a label for inputs. |
||
736 | * |
||
737 | * @param string $label Contents of the label |
||
738 | * @param string $id ID of the element being labeled |
||
739 | * @param array $attribs Additional attributes |
||
740 | * @return string Raw HTML |
||
741 | */ |
||
742 | public static function label( $label, $id, array $attribs = [] ) { |
||
748 | |||
749 | /** |
||
750 | * Convenience function to produce an input element with type=hidden |
||
751 | * |
||
752 | * @param string $name Name attribute |
||
753 | * @param string $value Value attribute |
||
754 | * @param array $attribs Associative array of miscellaneous extra |
||
755 | * attributes, passed to Html::element() |
||
756 | * @return string Raw HTML |
||
757 | */ |
||
758 | public static function hidden( $name, $value, array $attribs = [] ) { |
||
761 | |||
762 | /** |
||
763 | * Convenience function to produce a <textarea> element. |
||
764 | * |
||
765 | * This supports leaving out the cols= and rows= which Xml requires and are |
||
766 | * required by HTML4/XHTML but not required by HTML5. |
||
767 | * |
||
768 | * @param string $name Name attribute |
||
769 | * @param string $value Value attribute |
||
770 | * @param array $attribs Associative array of miscellaneous extra |
||
771 | * attributes, passed to Html::element() |
||
772 | * @return string Raw HTML |
||
773 | */ |
||
774 | public static function textarea( $name, $value = '', array $attribs = [] ) { |
||
788 | |||
789 | /** |
||
790 | * Helper for Html::namespaceSelector(). |
||
791 | * @param array $params See Html::namespaceSelector() |
||
792 | * @return array |
||
793 | */ |
||
794 | public static function namespaceSelectorOptions( array $params = [] ) { |
||
829 | |||
830 | /** |
||
831 | * Build a drop-down box for selecting a namespace |
||
832 | * |
||
833 | * @param array $params Params to set. |
||
834 | * - selected: [optional] Id of namespace which should be pre-selected |
||
835 | * - all: [optional] Value of item for "all namespaces". If null or unset, |
||
836 | * no "<option>" is generated to select all namespaces. |
||
837 | * - label: text for label to add before the field. |
||
838 | * - exclude: [optional] Array of namespace ids to exclude. |
||
839 | * - disable: [optional] Array of namespace ids for which the option should |
||
840 | * be disabled in the selector. |
||
841 | * @param array $selectAttribs HTML attributes for the generated select element. |
||
842 | * - id: [optional], default: 'namespace'. |
||
843 | * - name: [optional], default: 'namespace'. |
||
844 | * @return string HTML code to select a namespace. |
||
845 | */ |
||
846 | public static function namespaceSelector( array $params = [], |
||
910 | |||
911 | /** |
||
912 | * Constructs the opening html-tag with necessary doctypes depending on |
||
913 | * global variables. |
||
914 | * |
||
915 | * @param array $attribs Associative array of miscellaneous extra |
||
916 | * attributes, passed to Html::element() of html tag. |
||
917 | * @return string Raw HTML |
||
918 | */ |
||
919 | public static function htmlHeader( array $attribs = [] ) { |
||
951 | |||
952 | /** |
||
953 | * Determines if the given MIME type is xml. |
||
954 | * |
||
955 | * @param string $mimetype MIME type |
||
956 | * @return bool |
||
957 | */ |
||
958 | public static function isXmlMimeType( $mimetype ) { |
||
965 | |||
966 | /** |
||
967 | * Get HTML for an info box with an icon. |
||
968 | * |
||
969 | * @param string $text Wikitext, get this with wfMessage()->plain() |
||
970 | * @param string $icon Path to icon file (used as 'src' attribute) |
||
971 | * @param string $alt Alternate text for the icon |
||
972 | * @param string $class Additional class name to add to the wrapper div |
||
973 | * |
||
974 | * @return string |
||
975 | */ |
||
976 | static function infoBox( $text, $icon, $alt, $class = '' ) { |
||
999 | |||
1000 | /** |
||
1001 | * Generate a srcset attribute value. |
||
1002 | * |
||
1003 | * Generates a srcset attribute value from an array mapping pixel densities |
||
1004 | * to URLs. A trailing 'x' in pixel density values is optional. |
||
1005 | * |
||
1006 | * @note srcset width and height values are not supported. |
||
1007 | * |
||
1008 | * @see https://html.spec.whatwg.org/#attr-img-srcset |
||
1009 | * |
||
1010 | * @par Example: |
||
1011 | * @code |
||
1012 | * Html::srcSet( [ |
||
1013 | * '1x' => 'standard.jpeg', |
||
1014 | * '1.5x' => 'large.jpeg', |
||
1015 | * '3x' => 'extra-large.jpeg', |
||
1016 | * ] ); |
||
1017 | * // gives 'standard.jpeg 1x, large.jpeg 1.5x, extra-large.jpeg 2x' |
||
1018 | * @endcode |
||
1019 | * |
||
1020 | * @param string[] $urls |
||
1021 | * @return string |
||
1022 | */ |
||
1023 | static function srcSet( array $urls ) { |
||
1043 | } |
||
1044 |
This check looks for the bodies of
elseif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseif
bodies can be removed. If you have an empty elseif but statements in theelse
branch, consider inverting the condition.