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 | * Replace parts of an attribute that match given regexp |
||
| 504 | * |
||
| 505 | * @param DOMAttr $attribute Attribute |
||
| 506 | * @param string $regexp Regexp for matching parts that need replacement |
||
| 507 | * @param callback $fn Callback used to get the replacement |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | protected static function replaceTokensAttribute(DOMAttr $attribute, $regexp, $fn) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Replace parts of a text node that match given regexp |
||
| 540 | * |
||
| 541 | * @param DOMText $node Text node |
||
| 542 | * @param string $regexp Regexp for matching parts that need replacement |
||
| 543 | * @param callback $fn Callback used to get the replacement |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | protected static function replaceTokensText(DOMText $node, $regexp, $fn) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Serialize a loaded template back into a string |
||
| 594 | * |
||
| 595 | * NOTE: removes the root node created by loadTemplate() |
||
| 596 | * |
||
| 597 | * @param DOMDocument $dom |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | public static function saveTemplate(DOMDocument $dom) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Replace HTML entities and unescaped ampersands in given template |
||
| 607 | * |
||
| 608 | * @param string $template |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | protected static function fixEntities($template) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the XML content of an element |
||
| 625 | * |
||
| 626 | * @param DOMElement $element |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | protected static function innerXML(DOMElement $element) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Load given HTML template in a DOM document |
||
| 650 | * |
||
| 651 | * @param string $template Original template |
||
| 652 | * @return DOMDocument |
||
| 653 | */ |
||
| 654 | protected static function loadTemplateAsHTML($template) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Load given XSL template in a DOM document |
||
| 676 | * |
||
| 677 | * @param string $template Original template |
||
| 678 | * @return bool|DOMDocument DOMDocument on success, FALSE otherwise |
||
| 679 | */ |
||
| 680 | protected static function loadTemplateAsXML($template) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Remove attributes with an invalid name from given DOM document |
||
| 695 | * |
||
| 696 | * @param DOMDocument $dom |
||
| 697 | * @return void |
||
| 698 | */ |
||
| 699 | protected static function removeInvalidAttributes(DOMDocument $dom) |
||
| 710 | } |