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 |
||
22 | class Xml { |
||
23 | /** |
||
24 | * Format an XML element with given attributes and, optionally, text content. |
||
25 | * Element and attribute names are assumed to be ready for literal inclusion. |
||
26 | * Strings are assumed to not contain XML-illegal characters; special |
||
27 | * characters (<, >, &) are escaped but illegals are not touched. |
||
28 | * |
||
29 | * @param string|null $element element name |
||
30 | * @param array $attribs Name=>value pairs. Values will be escaped. |
||
31 | * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default) |
||
32 | * @param bool $allowShortTag whether '' in $contents will result in a contentless closed tag |
||
33 | * @return string |
||
34 | */ |
||
35 | public static function element( $element = null, $attribs = null, $contents = '', $allowShortTag = true ) { |
||
51 | |||
52 | static function encodeAttribute( $text ) { |
||
68 | |||
69 | /** |
||
70 | * Given an array of ('attributename' => 'value'), it generates the code |
||
71 | * to set the XML attributes : attributename="value". |
||
72 | * The values are passed to self::encodeAttribute. |
||
73 | * Return null if no attributes given. |
||
74 | * @param array|null $attribs of attributes for an XML element |
||
75 | * @throws Exception |
||
76 | * @return null|string |
||
77 | */ |
||
78 | public static function expandAttributes( $attribs = null ) { |
||
91 | |||
92 | /** |
||
93 | * Format an XML element as with self::element(), but run text through the |
||
94 | * $wgContLang->normalize() validator first to ensure that no invalid UTF-8 |
||
95 | * is passed. |
||
96 | * |
||
97 | * @param $element String: |
||
98 | * @param array $attribs Name=>value pairs. Values will be escaped. |
||
99 | * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default) |
||
100 | * @return string |
||
101 | */ |
||
102 | public static function elementClean( $element, $attribs = array(), $contents = '' ) { |
||
111 | |||
112 | /** |
||
113 | * This opens an XML element |
||
114 | * |
||
115 | * @param string $element name of the element |
||
116 | * @param array $attribs of attributes, see self::expandAttributes() |
||
117 | * @return string |
||
118 | */ |
||
119 | public static function openElement( $element, $attribs = null ) { |
||
122 | |||
123 | /** |
||
124 | * Shortcut to close an XML element |
||
125 | * @param string $element element name |
||
126 | * @return string |
||
127 | */ |
||
128 | public static function closeElement( $element ) { |
||
131 | |||
132 | /** |
||
133 | * Same as self::element(), but does not escape contents. Handy when the |
||
134 | * content you have is already valid xml. |
||
135 | * |
||
136 | * @param string $element element name |
||
137 | * @param array $attribs of attributes |
||
138 | * @param string $contents content of the element |
||
139 | * @return string |
||
140 | */ |
||
141 | public static function tags( $element, $attribs = null, $contents ) { |
||
144 | |||
145 | /** |
||
146 | * Shortcut to make a span element |
||
147 | * @param string $text content of the element, will be escaped |
||
148 | * @param string $class class name of the span element |
||
149 | * @param array $attribs other attributes |
||
150 | * @return string |
||
151 | */ |
||
152 | public static function span( $text, $class, $attribs = array() ) { |
||
155 | |||
156 | /** |
||
157 | * Shortcut to make a specific element with a class attribute |
||
158 | * @param string $text content of the element, will be escaped |
||
159 | * @param string $class class name of the span element |
||
160 | * @param string $pgTag element name |
||
161 | * @param array $attribs other attributes |
||
162 | * @return string |
||
163 | */ |
||
164 | public static function wrapClass( $text, $class, $pgTag = 'span', $attribs = array() ) { |
||
167 | |||
168 | /** |
||
169 | * Convenience function to build an HTML text input field |
||
170 | * @param string $name value of the name attribute |
||
171 | * @param bool|int $size value of the size attribute |
||
172 | * @param mixed $value mixed value of the value attribute |
||
173 | * @param array $attribs other attributes |
||
174 | * @return string HTML |
||
175 | */ |
||
176 | public static function input( $name, $size = false, $value = false, $attribs = array() ) { |
||
189 | |||
190 | /** |
||
191 | * Convenience function to build an HTML password input field |
||
192 | * @param string $name value of the name attribute |
||
193 | * @param bool|int $size value of the size attribute |
||
194 | * @param $value mixed value of the value attribute |
||
195 | * @param array $attribs other attributes |
||
196 | * @return string HTML |
||
197 | */ |
||
198 | public static function password( $name, $size = false, $value = false, $attribs = array() ) { |
||
201 | |||
202 | /** |
||
203 | * Internal function for use in checkboxes and radio buttons and such. |
||
204 | * |
||
205 | * @param $name string |
||
206 | * @param $present bool |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public static function attrib( $name, $present = true ) { |
||
213 | |||
214 | /** |
||
215 | * Convenience function to build an HTML checkbox |
||
216 | * @param string $name value of the name attribute |
||
217 | * @param bool $checked Whether the checkbox is checked or not |
||
218 | * @param array $attribs other attributes |
||
219 | * @return string HTML |
||
220 | */ |
||
221 | public static function check( $name, $checked = false, $attribs = array() ) { |
||
234 | |||
235 | /** |
||
236 | * Convenience function to build an HTML radio button |
||
237 | * @param string $name value of the name attribute |
||
238 | * @param string $value value of the value attribute |
||
239 | * @param bool $checked Whether the checkbox is checked or not |
||
240 | * @param array $attribs other attributes |
||
241 | * @return string HTML |
||
242 | */ |
||
243 | public static function radio( $name, $value, $checked = false, $attribs = array() ) { |
||
252 | |||
253 | /** |
||
254 | * Convenience function to build an HTML form label |
||
255 | * @param string $label text of the label |
||
256 | * @param $id |
||
257 | * @param array $attribs an attribute array. This will usually be |
||
258 | * the same array as is passed to the corresponding input element, |
||
259 | * so this function will cherry-pick appropriate attributes to |
||
260 | * apply to the label as well; only class and title are applied. |
||
261 | * @return string HTML |
||
262 | */ |
||
263 | public static function label( $label, $id, $attribs = array() ) { |
||
276 | |||
277 | /** |
||
278 | * Convenience function to build an HTML text input field with a label |
||
279 | * @param string $label text of the label |
||
280 | * @param string $name value of the name attribute |
||
281 | * @param string $id id of the input |
||
282 | * @param int|Bool $size value of the size attribute |
||
283 | * @param string|Bool $value value of the value attribute |
||
284 | * @param array $attribs other attributes |
||
285 | * @return string HTML |
||
286 | */ |
||
287 | public static function inputLabel( $label, $name, $id, $size = false, $value = false, $attribs = array() ) { |
||
291 | |||
292 | /** |
||
293 | * Same as self::inputLabel() but return input and label in an array |
||
294 | * |
||
295 | * @param $label String |
||
296 | * @param $name String |
||
297 | * @param $id String |
||
298 | * @param $size Int|Bool |
||
299 | * @param $value String|Bool |
||
300 | * @param $attribs array |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public static function inputLabelSep( $label, $name, $id, $size = false, $value = false, $attribs = array() ) { |
||
310 | |||
311 | /** |
||
312 | * Convenience function to build an HTML checkbox with a label |
||
313 | * |
||
314 | * @param $label |
||
315 | * @param $name |
||
316 | * @param $id |
||
317 | * @param $checked bool |
||
318 | * @param $attribs array |
||
319 | * |
||
320 | * @return string HTML |
||
321 | */ |
||
322 | public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) { |
||
327 | |||
328 | /** |
||
329 | * Convenience function to build an HTML radio button with a label |
||
330 | * |
||
331 | * @param $label |
||
332 | * @param $name |
||
333 | * @param $value |
||
334 | * @param $id |
||
335 | * @param $checked bool |
||
336 | * @param $attribs array |
||
337 | * |
||
338 | * @return string HTML |
||
339 | */ |
||
340 | public static function radioLabel( $label, $name, $value, $id, $checked = false, $attribs = array() ) { |
||
345 | |||
346 | /** |
||
347 | * Convenience function to build an HTML submit button |
||
348 | * @param string $value label text for the button |
||
349 | * @param array $attribs optional custom attributes |
||
350 | * @return string HTML |
||
351 | */ |
||
352 | public static function submitButton( $value, $attribs = array() ) { |
||
355 | |||
356 | /** |
||
357 | * Convenience function to build an HTML drop-down list item. |
||
358 | * @param string $text text for this item. Will be HTML escaped |
||
359 | * @param string $value form submission value; if empty, use text |
||
360 | * @param $selected boolean: if true, will be the default selected item |
||
361 | * @param array $attribs optional additional HTML attributes |
||
362 | * @return string HTML |
||
363 | */ |
||
364 | public static function option( $text, $value = null, $selected = false, |
||
374 | |||
375 | /** |
||
376 | * Build a drop-down box from a textual list. |
||
377 | * |
||
378 | * @param $name Mixed: Name and id for the drop-down |
||
379 | * @param $list Mixed: Correctly formatted text (newline delimited) to be used to generate the options |
||
380 | * @param $other Mixed: Text for the "Other reasons" option |
||
381 | * @param $selected Mixed: Option which should be pre-selected |
||
382 | * @param $class Mixed: CSS classes for the drop-down |
||
383 | * @param $tabindex Mixed: Value of the tabindex attribute |
||
384 | * @return string |
||
385 | */ |
||
386 | public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) { |
||
442 | |||
443 | /** |
||
444 | * Shortcut for creating fieldsets. |
||
445 | * |
||
446 | * @param string|bool $legend Legend of the fieldset. If evaluates to false, legend is not added. |
||
447 | * @param string|bool $content Pre-escaped content for the fieldset. If false, only open fieldset is returned. |
||
448 | * @param array $attribs Any attributes to fieldset-element. |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | public static function fieldset( $legend = false, $content = false, $attribs = array() ) { |
||
466 | |||
467 | /** |
||
468 | * Shortcut for creating textareas. |
||
469 | * |
||
470 | * @param string $name The 'name' for the textarea |
||
471 | * @param string $content Content for the textarea |
||
472 | * @param int $cols The number of columns for the textarea |
||
473 | * @param int $rows The number of rows for the textarea |
||
474 | * @param array $attribs Any other attributes for the textarea |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) { |
||
490 | |||
491 | /** |
||
492 | * Returns an escaped string suitable for inclusion in a string literal |
||
493 | * for JavaScript source code. |
||
494 | * Illegal control characters are assumed not to be present. |
||
495 | * |
||
496 | * @param string $string to escape |
||
497 | * @return String |
||
498 | */ |
||
499 | public static function escapeJsString( $string ) { |
||
525 | |||
526 | /** |
||
527 | * Encode a variable of unknown type to JavaScript. |
||
528 | * Arrays are converted to JS arrays, objects are converted to JS associative |
||
529 | * arrays (objects). So cast your PHP associative arrays to objects before |
||
530 | * passing them to here. |
||
531 | * |
||
532 | * @param $value |
||
533 | * @return int|string |
||
534 | */ |
||
535 | public static function encodeJsVar( $value ) { |
||
570 | |||
571 | /** |
||
572 | * Create a call to a JavaScript function. The supplied arguments will be |
||
573 | * encoded using self::encodeJsVar(). |
||
574 | * |
||
575 | * @since 1.17 |
||
576 | * @param string $name The name of the function to call, or a JavaScript expression |
||
577 | * which evaluates to a function object which is called. |
||
578 | * @param array $args The arguments to pass to the function. |
||
579 | * @param bool $pretty If true, add non-significant whitespace to improve readability. |
||
580 | * @return string|bool: String if successful; false upon failure |
||
581 | */ |
||
582 | public static function encodeJsCall( $name, $args, $pretty = false ) { |
||
595 | |||
596 | /** |
||
597 | * Check if a string is well-formed XML. |
||
598 | * Must include the surrounding tag. |
||
599 | * |
||
600 | * @param string $text string to test. |
||
601 | * @return bool |
||
602 | * |
||
603 | * @todo Error position reporting return |
||
604 | */ |
||
605 | public static function isWellFormed( $text ) { |
||
624 | |||
625 | /** |
||
626 | * Check if a string is a well-formed XML fragment. |
||
627 | * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment |
||
628 | * and can use HTML named entities. |
||
629 | * |
||
630 | * @param $text String: |
||
631 | * @return bool |
||
632 | */ |
||
633 | public static function isWellFormedXmlFragment( $text ) { |
||
642 | |||
643 | static function hackDocType() { |
||
905 | |||
906 | /** |
||
907 | * Replace " > and < with their respective HTML entities ( ", |
||
908 | * >, <) |
||
909 | * |
||
910 | * @param string $in text that might contain HTML tags. |
||
911 | * @return string Escaped string |
||
912 | */ |
||
913 | public static function escapeTagsOnly( $in ) { |
||
920 | |||
921 | /** |
||
922 | * Build a table of data |
||
923 | * @param array $rows An array of arrays of strings, each to be a row in a table |
||
924 | * @param array $attribs An array of attributes to apply to the table tag [optional] |
||
925 | * @param array $headers An array of strings to use as table headers [optional] |
||
926 | * @return string |
||
927 | */ |
||
928 | public static function buildTable( $rows, $attribs = array(), $headers = null ) { |
||
960 | |||
961 | /** |
||
962 | * Build a row for a table |
||
963 | * @param array $attribs An array of attributes to apply to the tr tag |
||
964 | * @param array $cells An array of strings to put in <td> |
||
965 | * @return string |
||
966 | */ |
||
967 | public static function buildTableRow( $attribs, $cells ) { |
||
984 | } |
||
985 | |||
1090 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.