Complex classes like TemplateParser 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 TemplateParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class TemplateParser |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * XSL namespace |
||
| 20 | */ |
||
| 21 | const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string Regexp that matches the names of all void elements |
||
| 25 | * @link http://www.w3.org/TR/html-markup/syntax.html#void-elements |
||
| 26 | */ |
||
| 27 | public static $voidRegexp = '/^(?:area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/Di'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Parse a template into an internal representation |
||
| 31 | * |
||
| 32 | * @param string $template Source template |
||
| 33 | * @return DOMDocument Internal representation |
||
| 34 | */ |
||
| 35 | public static function parse($template) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Parse an XPath expression that is composed entirely of equality tests between a variable part |
||
| 53 | * and a constant part |
||
| 54 | * |
||
| 55 | * @param string $expr |
||
| 56 | * @return array|false |
||
| 57 | */ |
||
| 58 | public static function parseEqualityExpr($expr) |
||
| 111 | |||
| 112 | //========================================================================== |
||
| 113 | // General parsing |
||
| 114 | //========================================================================== |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Parse all the children of a given element |
||
| 118 | * |
||
| 119 | * @param DOMElement $ir Node in the internal representation that represents the parent node |
||
| 120 | * @param DOMElement $parent Parent node |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | protected static function parseChildren(DOMElement $ir, DOMElement $parent) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Parse a given node into the internal representation |
||
| 152 | * |
||
| 153 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 154 | * @param DOMElement $node Node to parse |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | protected static function parseNode(DOMElement $ir, DOMElement $node) |
||
| 201 | |||
| 202 | //========================================================================== |
||
| 203 | // XSL parsing |
||
| 204 | //========================================================================== |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Parse an <xsl:apply-templates/> node into the internal representation |
||
| 208 | * |
||
| 209 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 210 | * @param DOMElement $node <xsl:apply-templates/> node |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | protected static function parseXslApplyTemplates(DOMElement $ir, DOMElement $node) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Parse an <xsl:attribute/> node into the internal representation |
||
| 228 | * |
||
| 229 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 230 | * @param DOMElement $node <xsl:attribute/> node |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | protected static function parseXslAttribute(DOMElement $ir, DOMElement $node) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Parse an <xsl:choose/> node and its <xsl:when/> and <xsl:otherwise/> children into the |
||
| 251 | * internal representation |
||
| 252 | * |
||
| 253 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 254 | * @param DOMElement $node <xsl:choose/> node |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | protected static function parseXslChoose(DOMElement $ir, DOMElement $node) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Parse an <xsl:comment/> node into the internal representation |
||
| 298 | * |
||
| 299 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 300 | * @param DOMElement $node <xsl:comment/> node |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | protected static function parseXslComment(DOMElement $ir, DOMElement $node) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Parse an <xsl:copy-of/> node into the internal representation |
||
| 313 | * |
||
| 314 | * NOTE: only attributes are supported |
||
| 315 | * |
||
| 316 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 317 | * @param DOMElement $node <xsl:copy-of/> node |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | protected static function parseXslCopyOf(DOMElement $ir, DOMElement $node) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Parse an <xsl:element/> node into the internal representation |
||
| 355 | * |
||
| 356 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 357 | * @param DOMElement $node <xsl:element/> node |
||
| 358 | * @return void |
||
| 359 | */ |
||
| 360 | protected static function parseXslElement(DOMElement $ir, DOMElement $node) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Parse an <xsl:if/> node into the internal representation |
||
| 378 | * |
||
| 379 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 380 | * @param DOMElement $node <xsl:if/> node |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | protected static function parseXslIf(DOMElement $ir, DOMElement $node) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Parse an <xsl:text/> node into the internal representation |
||
| 396 | * |
||
| 397 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 398 | * @param DOMElement $node <xsl:text/> node |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | protected static function parseXslText(DOMElement $ir, DOMElement $node) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Parse an <xsl:value-of/> node into the internal representation |
||
| 408 | * |
||
| 409 | * @param DOMElement $ir Node in the internal representation that represents the node's parent |
||
| 410 | * @param DOMElement $node <xsl:value-of/> node |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | protected static function parseXslValueOf(DOMElement $ir, DOMElement $node) |
||
| 417 | |||
| 418 | //========================================================================== |
||
| 419 | // IR optimization |
||
| 420 | //========================================================================== |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Normalize an IR |
||
| 424 | * |
||
| 425 | * @param DOMDocument $ir |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | protected static function normalize(DOMDocument $ir) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Add an empty default <case/> to <switch/> nodes that don't have one |
||
| 442 | * |
||
| 443 | * @param DOMDocument $ir |
||
| 444 | * @return void |
||
| 445 | */ |
||
| 446 | protected static function addDefaultCase(DOMDocument $ir) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Add an id attribute to <element/> nodes |
||
| 457 | * |
||
| 458 | * @param DOMDocument $ir |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | protected static function addElementIds(DOMDocument $ir) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add <closeTag/> elements everywhere an open start tag should be closed |
||
| 472 | * |
||
| 473 | * @param DOMDocument $ir |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | protected static function addCloseTagElements(DOMDocument $ir) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Mark conditional <closeTag/> nodes |
||
| 506 | * |
||
| 507 | * @param DOMDocument $ir |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | protected static function markConditionalCloseTagElements(DOMDocument $ir) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Mark void elements and elements with no content |
||
| 537 | * |
||
| 538 | * @param DOMDocument $ir |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | protected static function markEmptyElements(DOMDocument $ir) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Get the context type for given output element |
||
| 569 | * |
||
| 570 | * @param DOMNode $output |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | protected static function getOutputContext(DOMNode $output) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get the ID of the closest "element" ancestor |
||
| 591 | * |
||
| 592 | * @param DOMNode $node Context node |
||
| 593 | * @return string|null |
||
| 594 | */ |
||
| 595 | protected static function getParentElementId(DOMNode $node) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Fill in output context |
||
| 610 | * |
||
| 611 | * @param DOMDocument $ir |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | protected static function setOutputContext(DOMDocument $ir) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Optimize an IR |
||
| 624 | * |
||
| 625 | * @param DOMDocument $ir |
||
| 626 | * @return void |
||
| 627 | */ |
||
| 628 | protected static function optimize(DOMDocument $ir) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Remove redundant closeTag siblings after a switch |
||
| 653 | * |
||
| 654 | * If all branches of a switch have a closeTag we can remove any closeTag siblings of the switch |
||
| 655 | * |
||
| 656 | * @param DOMDocument $ir |
||
| 657 | * @return void |
||
| 658 | */ |
||
| 659 | protected static function removeCloseTagSiblings(DOMDocument $ir) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Remove empty default cases (no test and no descendants) |
||
| 667 | * |
||
| 668 | * @param DOMDocument $ir |
||
| 669 | * @return void |
||
| 670 | */ |
||
| 671 | protected static function removeEmptyDefaultCases(DOMDocument $ir) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Merge consecutive literal outputs |
||
| 679 | * |
||
| 680 | * @param DOMDocument $ir |
||
| 681 | * @return void |
||
| 682 | */ |
||
| 683 | protected static function mergeConsecutiveLiteralOutputElements(DOMDocument $ir) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Optimize closeTags elements |
||
| 701 | * |
||
| 702 | * @param DOMDocument $ir |
||
| 703 | * @return void |
||
| 704 | */ |
||
| 705 | protected static function optimizeCloseTagElements(DOMDocument $ir) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Clone closeTag elements that follow a switch into said switch |
||
| 715 | * |
||
| 716 | * If there's a <closeTag/> right after a <switch/>, clone the <closeTag/> at the end of |
||
| 717 | * the every <case/> that does not end with a <closeTag/> |
||
| 718 | * |
||
| 719 | * @param DOMDocument $ir |
||
| 720 | * @return void |
||
| 721 | */ |
||
| 722 | protected static function cloneCloseTagElementsIntoSwitch(DOMDocument $ir) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Clone closeTag elements from the head of a switch's cases before said switch |
||
| 741 | * |
||
| 742 | * If there's a <closeTag/> at the beginning of every <case/>, clone it and insert it |
||
| 743 | * right before the <switch/> unless there's already one |
||
| 744 | * |
||
| 745 | * @param DOMDocument $ir |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | protected static function cloneCloseTagElementsOutOfSwitch(DOMDocument $ir) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Remove all nodes that match given XPath query |
||
| 771 | * |
||
| 772 | * @param DOMDocument $ir |
||
| 773 | * @param string $query |
||
| 774 | * @param DOMNode $contextNode |
||
| 775 | * @return void |
||
| 776 | */ |
||
| 777 | protected static function removeNodes(DOMDocument $ir, $query, DOMNode $contextNode = null) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Remove redundant closeTag elements from the tail of a switch's cases |
||
| 791 | * |
||
| 792 | * If there's a <closeTag/> right after a <switch/>, remove all <closeTag/> nodes at the |
||
| 793 | * end of every <case/> |
||
| 794 | * |
||
| 795 | * @param DOMDocument $ir |
||
| 796 | * @return void |
||
| 797 | */ |
||
| 798 | protected static function removeRedundantCloseTagElementsInSwitch(DOMDocument $ir) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Remove redundant closeTag elements from the tail of a switch's cases |
||
| 816 | * |
||
| 817 | * For each <closeTag/> remove duplicate <closeTag/> nodes that are either siblings or |
||
| 818 | * descendants of a sibling |
||
| 819 | * |
||
| 820 | * @param DOMDocument $ir |
||
| 821 | * @return void |
||
| 822 | */ |
||
| 823 | protected static function removeRedundantCloseTagElements(DOMDocument $ir) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Remove content from void elements |
||
| 837 | * |
||
| 838 | * For each void element, we find whichever <closeTag/> elements close it and remove everything |
||
| 839 | * after |
||
| 840 | * |
||
| 841 | * @param DOMDocument $ir |
||
| 842 | * @return void |
||
| 843 | */ |
||
| 844 | protected static function removeContentFromVoidElements(DOMDocument $ir) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Mark switch elements that are used as branch tables |
||
| 858 | * |
||
| 859 | * If a switch is used for a series of equality tests against the same attribute or variable, the |
||
| 860 | * attribute/variable is stored within the switch as "branch-key" and the values it is compared |
||
| 861 | * against are stored JSON-encoded in the case as "branch-values". It can be used to create |
||
| 862 | * optimized branch tables |
||
| 863 | * |
||
| 864 | * @param DOMDocument $ir |
||
| 865 | * @return void |
||
| 866 | */ |
||
| 867 | protected static function markBranchTables(DOMDocument $ir) |
||
| 916 | |||
| 917 | //========================================================================== |
||
| 918 | // Misc |
||
| 919 | //========================================================================== |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Create and append an element to given node in the IR |
||
| 923 | * |
||
| 924 | * @param DOMElement $parentNode Parent node of the element |
||
| 925 | * @param string $name Tag name of the element |
||
| 926 | * @param string $value Value of the element |
||
| 927 | * @return DOMElement The created element |
||
| 928 | */ |
||
| 929 | protected static function appendElement(DOMElement $parentNode, $name, $value = '') |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Append an <output/> element to given node in the IR |
||
| 947 | * |
||
| 948 | * @param DOMElement $ir Parent node |
||
| 949 | * @param string $type Either 'avt', 'literal' or 'xpath' |
||
| 950 | * @param string $content Content to output |
||
| 951 | * @return void |
||
| 952 | */ |
||
| 953 | protected static function appendOutput(DOMElement $ir, $type, $content) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Test whether given element will be empty at runtime (no content, no children) |
||
| 985 | * |
||
| 986 | * @param DOMElement $ir Element in the IR |
||
| 987 | * @return string 'yes', 'maybe' or 'no' |
||
| 988 | */ |
||
| 989 | protected static function isEmpty(DOMElement $ir) |
||
| 1033 | } |