Complex classes like TemplateHelper 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 TemplateHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class TemplateHelper |
||
22 | { |
||
23 | /** |
||
24 | * XSL namespace |
||
25 | */ |
||
26 | const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
||
27 | |||
28 | /** |
||
29 | * Return all attributes (literal or generated) that match given regexp |
||
30 | * |
||
31 | * @param DOMDocument $dom Document |
||
32 | * @param string $regexp Regexp |
||
33 | * @return array Array of DOMNode instances |
||
34 | */ |
||
35 | public static function getAttributesByRegexp(DOMDocument $dom, $regexp) |
||
72 | |||
73 | /** |
||
74 | * Return all DOMNodes whose content is CSS |
||
75 | * |
||
76 | * @param DOMDocument $dom Document |
||
77 | * @return array Array of DOMNode instances |
||
78 | */ |
||
79 | public static function getCSSNodes(DOMDocument $dom) |
||
89 | |||
90 | /** |
||
91 | * Return all elements (literal or generated) that match given regexp |
||
92 | * |
||
93 | * @param DOMDocument $dom Document |
||
94 | * @param string $regexp Regexp |
||
95 | * @return array Array of DOMNode instances |
||
96 | */ |
||
97 | public static function getElementsByRegexp(DOMDocument $dom, $regexp) |
||
135 | |||
136 | /** |
||
137 | * Return all DOMNodes whose content is JavaScript |
||
138 | * |
||
139 | * @param DOMDocument $dom Document |
||
140 | * @return array Array of DOMNode instances |
||
141 | */ |
||
142 | public static function getJSNodes(DOMDocument $dom) |
||
152 | |||
153 | /** |
||
154 | * Return all elements (literal or generated) that match given regexp |
||
155 | * |
||
156 | * Will return all <param/> descendants of <object/> and all attributes of <embed/> whose name |
||
157 | * matches given regexp. This method will NOT catch <param/> elements whose 'name' attribute is |
||
158 | * set via an <xsl:attribute/> |
||
159 | * |
||
160 | * @param DOMDocument $dom Document |
||
161 | * @param string $regexp |
||
162 | * @return array Array of DOMNode instances |
||
163 | */ |
||
164 | public static function getObjectParamsByRegexp(DOMDocument $dom, $regexp) |
||
200 | |||
201 | /** |
||
202 | * Return a list of parameters in use in given XSL |
||
203 | * |
||
204 | * @param string $xsl XSL source |
||
205 | * @return array Alphabetically sorted list of unique parameter names |
||
206 | */ |
||
207 | public static function getParametersFromXSL($xsl) |
||
274 | |||
275 | /** |
||
276 | * Return all DOMNodes whose content is an URL |
||
277 | * |
||
278 | * NOTE: it will also return HTML4 nodes whose content is an URI |
||
279 | * |
||
280 | * @param DOMDocument $dom Document |
||
281 | * @return array Array of DOMNode instances |
||
282 | */ |
||
283 | public static function getURLNodes(DOMDocument $dom) |
||
303 | |||
304 | /** |
||
305 | * Highlight the source of a node inside of a template |
||
306 | * |
||
307 | * @param DOMNode $node Node to highlight |
||
308 | * @param string $prepend HTML to prepend |
||
309 | * @param string $append HTML to append |
||
310 | * @return string Template's source, as HTML |
||
311 | */ |
||
312 | public static function highlightNode(DOMNode $node, $prepend, $append) |
||
362 | |||
363 | /** |
||
364 | * Load a template as an xsl:template node |
||
365 | * |
||
366 | * Will attempt to load it as XML first, then as HTML as a fallback. Either way, an xsl:template |
||
367 | * node is returned |
||
368 | * |
||
369 | * @param string $template |
||
370 | * @return DOMDocument |
||
371 | */ |
||
372 | public static function loadTemplate($template) |
||
396 | |||
397 | /** |
||
398 | * Replace simple templates (in an array, in-place) with a common template |
||
399 | * |
||
400 | * In some situations, renderers can take advantage of multiple tags having the same template. In |
||
401 | * any configuration, there's almost always a number of "simple" tags that are rendered as an |
||
402 | * HTML element of the same name with no HTML attributes. For instance, the system tag "p" used |
||
403 | * for paragraphs, "B" tags used for "b" HTML elements, etc... This method replaces those |
||
404 | * templates with a common template that uses a dynamic element name based on the tag's name, |
||
405 | * either its nodeName or localName depending on whether the tag is namespaced, and normalized to |
||
406 | * lowercase using XPath's translate() function |
||
407 | * |
||
408 | * @param array<string> &$templates Associative array of [tagName => template] |
||
409 | * @param integer $minCount |
||
410 | * @return void |
||
411 | */ |
||
412 | public static function replaceHomogeneousTemplates(array &$templates, $minCount = 3) |
||
464 | |||
465 | /** |
||
466 | * Replace parts of a template that match given regexp |
||
467 | * |
||
468 | * Treats attribute values as plain text. Replacements within XPath expression is unsupported. |
||
469 | * The callback must return an array with two elements. The first must be either of 'expression', |
||
470 | * 'literal' or 'passthrough', and the second element depends on the first. |
||
471 | * |
||
472 | * - 'expression' indicates that the replacement must be treated as an XPath expression such as |
||
473 | * '@foo', which must be passed as the second element. |
||
474 | * - 'literal' indicates a literal (plain text) replacement, passed as its second element. |
||
475 | * - 'passthrough' indicates that the replacement should the tag's content. It works differently |
||
476 | * whether it is inside an attribute's value or a text node. Within an attribute's value, the |
||
477 | * replacement will be the text content of the tag. Within a text node, the replacement |
||
478 | * becomes an <xsl:apply-templates/> node. |
||
479 | * |
||
480 | * @param string $template Original template |
||
481 | * @param string $regexp Regexp for matching parts that need replacement |
||
482 | * @param callback $fn Callback used to get the replacement |
||
483 | * @return string Processed template |
||
484 | */ |
||
485 | public static function replaceTokens($template, $regexp, $fn) |
||
501 | |||
502 | /** |
||
503 | * Create a node that implements given replacement strategy |
||
504 | * |
||
505 | * @param DOMDocument $dom |
||
506 | * @param array $replacement |
||
507 | * @return DOMNode |
||
508 | */ |
||
509 | protected static function createReplacementNode(DOMDocument $dom, array $replacement) |
||
531 | |||
532 | /** |
||
533 | * Replace parts of an attribute that match given regexp |
||
534 | * |
||
535 | * @param DOMAttr $attribute Attribute |
||
536 | * @param string $regexp Regexp for matching parts that need replacement |
||
537 | * @param callback $fn Callback used to get the replacement |
||
538 | * @return void |
||
539 | */ |
||
540 | protected static function replaceTokensInAttribute(DOMAttr $attribute, $regexp, $fn) |
||
563 | |||
564 | /** |
||
565 | * Replace parts of a text node that match given regexp |
||
566 | * |
||
567 | * @param DOMText $node Text node |
||
568 | * @param string $regexp Regexp for matching parts that need replacement |
||
569 | * @param callback $fn Callback used to get the replacement |
||
570 | * @return void |
||
571 | */ |
||
572 | protected static function replaceTokensInText(DOMText $node, $regexp, $fn) |
||
605 | |||
606 | /** |
||
607 | * Serialize a loaded template back into a string |
||
608 | * |
||
609 | * NOTE: removes the root node created by loadTemplate() |
||
610 | * |
||
611 | * @param DOMDocument $dom |
||
612 | * @return string |
||
613 | */ |
||
614 | public static function saveTemplate(DOMDocument $dom) |
||
618 | |||
619 | /** |
||
620 | * Replace HTML entities and unescaped ampersands in given template |
||
621 | * |
||
622 | * @param string $template |
||
623 | * @return string |
||
624 | */ |
||
625 | protected static function fixEntities($template) |
||
636 | |||
637 | /** |
||
638 | * Get the XML content of an element |
||
639 | * |
||
640 | * @param DOMElement $element |
||
641 | * @return string |
||
642 | */ |
||
643 | protected static function innerXML(DOMElement $element) |
||
661 | |||
662 | /** |
||
663 | * Load given HTML template in a DOM document |
||
664 | * |
||
665 | * @param string $template Original template |
||
666 | * @return DOMDocument |
||
667 | */ |
||
668 | protected static function loadTemplateAsHTML($template) |
||
687 | |||
688 | /** |
||
689 | * Load given XSL template in a DOM document |
||
690 | * |
||
691 | * @param string $template Original template |
||
692 | * @return bool|DOMDocument DOMDocument on success, FALSE otherwise |
||
693 | */ |
||
694 | protected static function loadTemplateAsXML($template) |
||
706 | |||
707 | /** |
||
708 | * Remove attributes with an invalid name from given DOM document |
||
709 | * |
||
710 | * @param DOMDocument $dom |
||
711 | * @return void |
||
712 | */ |
||
713 | protected static function removeInvalidAttributes(DOMDocument $dom) |
||
724 | } |