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 Xml 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 Xml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Xml { |
||
27 | /** |
||
28 | * Format an XML element with given attributes and, optionally, text content. |
||
29 | * Element and attribute names are assumed to be ready for literal inclusion. |
||
30 | * Strings are assumed to not contain XML-illegal characters; special |
||
31 | * characters (<, >, &) are escaped but illegals are not touched. |
||
32 | * |
||
33 | * @param string $element Element name |
||
34 | * @param array $attribs Name=>value pairs. Values will be escaped. |
||
35 | * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default) |
||
36 | * @param bool $allowShortTag Whether '' in $contents will result in a contentless closed tag |
||
37 | * @return string |
||
38 | */ |
||
39 | public static function element( $element, $attribs = null, $contents = '', |
||
57 | |||
58 | /** |
||
59 | * Given an array of ('attributename' => 'value'), it generates the code |
||
60 | * to set the XML attributes : attributename="value". |
||
61 | * The values are passed to Sanitizer::encodeAttribute. |
||
62 | * Return null if no attributes given. |
||
63 | * @param array $attribs Array of attributes for an XML element |
||
64 | * @throws MWException |
||
65 | * @return null|string |
||
66 | */ |
||
67 | public static function expandAttributes( $attribs ) { |
||
80 | |||
81 | /** |
||
82 | * Format an XML element as with self::element(), but run text through the |
||
83 | * $wgContLang->normalize() validator first to ensure that no invalid UTF-8 |
||
84 | * is passed. |
||
85 | * |
||
86 | * @param string $element |
||
87 | * @param array $attribs Name=>value pairs. Values will be escaped. |
||
88 | * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default) |
||
89 | * @return string |
||
90 | */ |
||
91 | public static function elementClean( $element, $attribs = [], $contents = '' ) { |
||
101 | |||
102 | /** |
||
103 | * This opens an XML element |
||
104 | * |
||
105 | * @param string $element Name of the element |
||
106 | * @param array $attribs Array of attributes, see Xml::expandAttributes() |
||
107 | * @return string |
||
108 | */ |
||
109 | public static function openElement( $element, $attribs = null ) { |
||
112 | |||
113 | /** |
||
114 | * Shortcut to close an XML element |
||
115 | * @param string $element Element name |
||
116 | * @return string |
||
117 | */ |
||
118 | public static function closeElement( $element ) { |
||
121 | |||
122 | /** |
||
123 | * Same as Xml::element(), but does not escape contents. Handy when the |
||
124 | * content you have is already valid xml. |
||
125 | * |
||
126 | * @param string $element Element name |
||
127 | * @param array $attribs Array of attributes |
||
128 | * @param string $contents Content of the element |
||
129 | * @return string |
||
130 | */ |
||
131 | public static function tags( $element, $attribs = null, $contents ) { |
||
134 | |||
135 | /** |
||
136 | * Create a date selector |
||
137 | * |
||
138 | * @param string $selected The month which should be selected, default ''. |
||
139 | * @param string $allmonths Value of a special item denoting all month. |
||
140 | * Null to not include (default). |
||
141 | * @param string $id Element identifier |
||
142 | * @return string Html string containing the month selector |
||
143 | */ |
||
144 | public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) { |
||
161 | |||
162 | /** |
||
163 | * @param int $year |
||
164 | * @param int $month |
||
165 | * @return string Formatted HTML |
||
166 | */ |
||
167 | public static function dateMenu( $year, $month ) { |
||
193 | |||
194 | /** |
||
195 | * Construct a language selector appropriate for use in a form or preferences |
||
196 | * |
||
197 | * @param string $selected The language code of the selected language |
||
198 | * @param bool $customisedOnly If true only languages which have some content are listed |
||
199 | * @param string $inLanguage The ISO code of the language to display the select list in (optional) |
||
200 | * @param array $overrideAttrs Override the attributes of the select tag (since 1.20) |
||
201 | * @param Message|null $msg Label message key (since 1.20) |
||
202 | * @return array Array containing 2 items: label HTML and select list HTML |
||
203 | */ |
||
204 | public static function languageSelector( $selected, $customisedOnly = true, |
||
242 | |||
243 | /** |
||
244 | * Shortcut to make a span element |
||
245 | * @param string $text Content of the element, will be escaped |
||
246 | * @param string $class Class name of the span element |
||
247 | * @param array $attribs Other attributes |
||
248 | * @return string |
||
249 | */ |
||
250 | public static function span( $text, $class, $attribs = [] ) { |
||
253 | |||
254 | /** |
||
255 | * Shortcut to make a specific element with a class attribute |
||
256 | * @param string $text Content of the element, will be escaped |
||
257 | * @param string $class Class name of the span element |
||
258 | * @param string $tag Element name |
||
259 | * @param array $attribs Other attributes |
||
260 | * @return string |
||
261 | */ |
||
262 | public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) { |
||
265 | |||
266 | /** |
||
267 | * Convenience function to build an HTML text input field |
||
268 | * @param string $name Value of the name attribute |
||
269 | * @param int $size Value of the size attribute |
||
270 | * @param mixed $value Value of the value attribute |
||
271 | * @param array $attribs Other attributes |
||
272 | * @return string HTML |
||
273 | */ |
||
274 | public static function input( $name, $size = false, $value = false, $attribs = [] ) { |
||
288 | |||
289 | /** |
||
290 | * Convenience function to build an HTML password input field |
||
291 | * @param string $name Value of the name attribute |
||
292 | * @param int $size Value of the size attribute |
||
293 | * @param mixed $value Value of the value attribute |
||
294 | * @param array $attribs Other attributes |
||
295 | * @return string HTML |
||
296 | */ |
||
297 | public static function password( $name, $size = false, $value = false, |
||
303 | |||
304 | /** |
||
305 | * Internal function for use in checkboxes and radio buttons and such. |
||
306 | * |
||
307 | * @param string $name |
||
308 | * @param bool $present |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | public static function attrib( $name, $present = true ) { |
||
315 | |||
316 | /** |
||
317 | * Convenience function to build an HTML checkbox |
||
318 | * @param string $name Value of the name attribute |
||
319 | * @param bool $checked Whether the checkbox is checked or not |
||
320 | * @param array $attribs Array other attributes |
||
321 | * @return string HTML |
||
322 | */ |
||
323 | public static function check( $name, $checked = false, $attribs = [] ) { |
||
332 | |||
333 | /** |
||
334 | * Convenience function to build an HTML radio button |
||
335 | * @param string $name Value of the name attribute |
||
336 | * @param string $value Value of the value attribute |
||
337 | * @param bool $checked Whether the checkbox is checked or not |
||
338 | * @param array $attribs Other attributes |
||
339 | * @return string HTML |
||
340 | */ |
||
341 | public static function radio( $name, $value, $checked = false, $attribs = [] ) { |
||
347 | |||
348 | /** |
||
349 | * Convenience function to build an HTML form label |
||
350 | * @param string $label Text of the label |
||
351 | * @param string $id |
||
352 | * @param array $attribs An attribute array. This will usually be |
||
353 | * the same array as is passed to the corresponding input element, |
||
354 | * so this function will cherry-pick appropriate attributes to |
||
355 | * apply to the label as well; only class and title are applied. |
||
356 | * @return string HTML |
||
357 | */ |
||
358 | public static function label( $label, $id, $attribs = [] ) { |
||
369 | |||
370 | /** |
||
371 | * Convenience function to build an HTML text input field with a label |
||
372 | * @param string $label Text of the label |
||
373 | * @param string $name Value of the name attribute |
||
374 | * @param string $id Id of the input |
||
375 | * @param int|bool $size Value of the size attribute |
||
376 | * @param string|bool $value Value of the value attribute |
||
377 | * @param array $attribs Other attributes |
||
378 | * @return string HTML |
||
379 | */ |
||
380 | public static function inputLabel( $label, $name, $id, $size = false, |
||
386 | |||
387 | /** |
||
388 | * Same as Xml::inputLabel() but return input and label in an array |
||
389 | * |
||
390 | * @param string $label |
||
391 | * @param string $name |
||
392 | * @param string $id |
||
393 | * @param int|bool $size |
||
394 | * @param string|bool $value |
||
395 | * @param array $attribs |
||
396 | * |
||
397 | * @return array |
||
398 | */ |
||
399 | public static function inputLabelSep( $label, $name, $id, $size = false, |
||
407 | |||
408 | /** |
||
409 | * Convenience function to build an HTML checkbox with a label |
||
410 | * |
||
411 | * @param string $label |
||
412 | * @param string $name |
||
413 | * @param string $id |
||
414 | * @param bool $checked |
||
415 | * @param array $attribs |
||
416 | * |
||
417 | * @return string HTML |
||
418 | */ |
||
419 | public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) { |
||
431 | |||
432 | /** |
||
433 | * Convenience function to build an HTML radio button with a label |
||
434 | * |
||
435 | * @param string $label |
||
436 | * @param string $name |
||
437 | * @param string $value |
||
438 | * @param string $id |
||
439 | * @param bool $checked |
||
440 | * @param array $attribs |
||
441 | * |
||
442 | * @return string HTML |
||
443 | */ |
||
444 | public static function radioLabel( $label, $name, $value, $id, |
||
451 | |||
452 | /** |
||
453 | * Convenience function to build an HTML submit button |
||
454 | * When $wgUseMediaWikiUIEverywhere is true it will default to a progressive button |
||
455 | * @param string $value Label text for the button |
||
456 | * @param array $attribs Optional custom attributes |
||
457 | * @return string HTML |
||
458 | */ |
||
459 | public static function submitButton( $value, $attribs = [] ) { |
||
475 | |||
476 | /** |
||
477 | * Convenience function to build an HTML drop-down list item. |
||
478 | * @param string $text Text for this item. Will be HTML escaped |
||
479 | * @param string $value Form submission value; if empty, use text |
||
480 | * @param bool $selected If true, will be the default selected item |
||
481 | * @param array $attribs Optional additional HTML attributes |
||
482 | * @return string HTML |
||
483 | */ |
||
484 | public static function option( $text, $value = null, $selected = false, |
||
494 | |||
495 | /** |
||
496 | * Build a drop-down box from a textual list. |
||
497 | * |
||
498 | * @param string $name Name and id for the drop-down |
||
499 | * @param string $list Correctly formatted text (newline delimited) to be |
||
500 | * used to generate the options. |
||
501 | * @param string $other Text for the "Other reasons" option |
||
502 | * @param string $selected Option which should be pre-selected |
||
503 | * @param string $class CSS classes for the drop-down |
||
504 | * @param int $tabindex Value of the tabindex attribute |
||
505 | * @return string |
||
506 | */ |
||
507 | public static function listDropDown( $name = '', $list = '', $other = '', |
||
565 | |||
566 | /** |
||
567 | * Shortcut for creating fieldsets. |
||
568 | * |
||
569 | * @param string|bool $legend Legend of the fieldset. If evaluates to false, |
||
570 | * legend is not added. |
||
571 | * @param string $content Pre-escaped content for the fieldset. If false, |
||
572 | * only open fieldset is returned. |
||
573 | * @param array $attribs Any attributes to fieldset-element. |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | public static function fieldset( $legend = false, $content = false, $attribs = [] ) { |
||
591 | |||
592 | /** |
||
593 | * Shortcut for creating textareas. |
||
594 | * |
||
595 | * @param string $name The 'name' for the textarea |
||
596 | * @param string $content Content for the textarea |
||
597 | * @param int $cols The number of columns for the textarea |
||
598 | * @param int $rows The number of rows for the textarea |
||
599 | * @param array $attribs Any other attributes for the textarea |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) { |
||
615 | |||
616 | /** |
||
617 | * Returns an escaped string suitable for inclusion in a string literal |
||
618 | * for JavaScript source code. |
||
619 | * Illegal control characters are assumed not to be present. |
||
620 | * |
||
621 | * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead |
||
622 | * @param string $string String to escape |
||
623 | * @return string |
||
624 | */ |
||
625 | public static function escapeJsString( $string ) { |
||
651 | |||
652 | /** |
||
653 | * Encode a variable of arbitrary type to JavaScript. |
||
654 | * If the value is an XmlJsCode object, pass through the object's value verbatim. |
||
655 | * |
||
656 | * @note Only use this function for generating JavaScript code. If generating output |
||
657 | * for a proper JSON parser, just call FormatJson::encode() directly. |
||
658 | * |
||
659 | * @param mixed $value The value being encoded. Can be any type except a resource. |
||
660 | * @param bool $pretty If true, add non-significant whitespace to improve readability. |
||
661 | * @return string|bool String if successful; false upon failure |
||
662 | */ |
||
663 | public static function encodeJsVar( $value, $pretty = false ) { |
||
669 | |||
670 | /** |
||
671 | * Create a call to a JavaScript function. The supplied arguments will be |
||
672 | * encoded using Xml::encodeJsVar(). |
||
673 | * |
||
674 | * @since 1.17 |
||
675 | * @param string $name The name of the function to call, or a JavaScript expression |
||
676 | * which evaluates to a function object which is called. |
||
677 | * @param array $args The arguments to pass to the function. |
||
678 | * @param bool $pretty If true, add non-significant whitespace to improve readability. |
||
679 | * @return string|bool String if successful; false upon failure |
||
680 | */ |
||
681 | public static function encodeJsCall( $name, $args, $pretty = false ) { |
||
694 | |||
695 | /** |
||
696 | * Check if a string is well-formed XML. |
||
697 | * Must include the surrounding tag. |
||
698 | * This function is a DoS vector if an attacker can define |
||
699 | * entities in $text. |
||
700 | * |
||
701 | * @param string $text String to test. |
||
702 | * @return bool |
||
703 | * |
||
704 | * @todo Error position reporting return |
||
705 | */ |
||
706 | private static function isWellFormed( $text ) { |
||
725 | |||
726 | /** |
||
727 | * Check if a string is a well-formed XML fragment. |
||
728 | * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment |
||
729 | * and can use HTML named entities. |
||
730 | * |
||
731 | * @param string $text |
||
732 | * @return bool |
||
733 | */ |
||
734 | public static function isWellFormedXmlFragment( $text ) { |
||
743 | |||
744 | /** |
||
745 | * Replace " > and < with their respective HTML entities ( ", |
||
746 | * >, <) |
||
747 | * |
||
748 | * @param string $in Text that might contain HTML tags. |
||
749 | * @return string Escaped string |
||
750 | */ |
||
751 | public static function escapeTagsOnly( $in ) { |
||
757 | |||
758 | /** |
||
759 | * Generate a form (without the opening form element). |
||
760 | * Output optionally includes a submit button. |
||
761 | * @param array $fields Associative array, key is the name of a message that |
||
762 | * contains a description for the field, value is an HTML string |
||
763 | * containing the appropriate input. |
||
764 | * @param string $submitLabel The name of a message containing a label for |
||
765 | * the submit button. |
||
766 | * @param array $submitAttribs The attributes to add to the submit button |
||
767 | * @return string HTML form. |
||
768 | */ |
||
769 | public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) { |
||
800 | |||
801 | /** |
||
802 | * Build a table of data |
||
803 | * @param array $rows An array of arrays of strings, each to be a row in a table |
||
804 | * @param array $attribs An array of attributes to apply to the table tag [optional] |
||
805 | * @param array $headers An array of strings to use as table headers [optional] |
||
806 | * @return string |
||
807 | */ |
||
808 | public static function buildTable( $rows, $attribs = [], $headers = null ) { |
||
840 | |||
841 | /** |
||
842 | * Build a row for a table |
||
843 | * @param array $attribs An array of attributes to apply to the tr tag |
||
844 | * @param array $cells An array of strings to put in <td> |
||
845 | * @return string |
||
846 | */ |
||
847 | public static function buildTableRow( $attribs, $cells ) { |
||
864 | } |
||
865 | |||
890 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.