Complex classes like TemplateInspector 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 TemplateInspector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class TemplateInspector |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * XSL namespace |
||
| 34 | */ |
||
| 35 | const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string[] allowChild bitfield for each branch |
||
| 39 | */ |
||
| 40 | protected $allowChildBitfields = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool Whether elements are allowed as children |
||
| 44 | */ |
||
| 45 | protected $allowsChildElements; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var bool Whether text nodes are allowed as children |
||
| 49 | */ |
||
| 50 | protected $allowsText; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array[] Array of array of DOMElement instances |
||
| 54 | */ |
||
| 55 | protected $branches; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string OR-ed bitfield representing all of the categories used by this template |
||
| 59 | */ |
||
| 60 | protected $contentBitfield = "\0"; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var |
||
| 64 | */ |
||
| 65 | protected $defaultBranchBitfield; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string denyDescendant bitfield |
||
| 69 | */ |
||
| 70 | protected $denyDescendantBitfield = "\0"; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var \DOMDocument Document containing the template |
||
| 74 | */ |
||
| 75 | protected $dom; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool Whether this template contains any HTML elements |
||
| 79 | */ |
||
| 80 | protected $hasElements = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool Whether this template renders non-whitespace text nodes at its root |
||
| 84 | */ |
||
| 85 | protected $hasRootText; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool Whether this template should be considered a block-level element |
||
| 89 | */ |
||
| 90 | protected $isBlock = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool Whether the template uses the "empty" content model |
||
| 94 | */ |
||
| 95 | protected $isEmpty; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool Whether this template adds to the list of active formatting elements |
||
| 99 | */ |
||
| 100 | protected $isFormattingElement; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var bool Whether this template lets content through via an xsl:apply-templates element |
||
| 104 | */ |
||
| 105 | protected $isPassthrough = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool Whether all branches use the transparent content model |
||
| 109 | */ |
||
| 110 | protected $isTransparent = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool Whether all branches have an ancestor that is a void element |
||
| 114 | */ |
||
| 115 | protected $isVoid; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array Last HTML element that precedes an <xsl:apply-templates/> node |
||
| 119 | */ |
||
| 120 | protected $leafNodes = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool Whether any branch has an element that preserves new lines by default (e.g. <pre>) |
||
| 124 | */ |
||
| 125 | protected $preservesNewLines = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array Bitfield of the first HTML element of every branch |
||
| 129 | */ |
||
| 130 | protected $rootBitfields = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array Every HTML element that has no HTML parent |
||
| 134 | */ |
||
| 135 | protected $rootNodes = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var DOMXPath XPath engine associated with $this->dom |
||
| 139 | */ |
||
| 140 | protected $xpath; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Constructor |
||
| 144 | * |
||
| 145 | * @param string $template Template content |
||
| 146 | */ |
||
| 147 | public function __construct($template) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return whether this template allows a given child |
||
| 161 | * |
||
| 162 | * @param TemplateInspector $child |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function allowsChild(TemplateInspector $child) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return whether this template allows a given descendant |
||
| 189 | * |
||
| 190 | * @param TemplateInspector $descendant |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function allowsDescendant(TemplateInspector $descendant) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return whether this template allows elements as children |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function allowsChildElements() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Return whether this template allows text nodes as children |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function allowsText() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Return whether this template automatically closes given parent template |
||
| 227 | * |
||
| 228 | * @param TemplateInspector $parent |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function closesParent(TemplateInspector $parent) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Evaluate an XPath expression |
||
| 249 | * |
||
| 250 | * @param string $expr XPath expression |
||
| 251 | * @param DOMElement $node Context node |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | public function evaluate($expr, DOMElement $node = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return whether this template should be considered a block-level element |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function isBlock() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return whether this template adds to the list of active formatting elements |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function isFormattingElement() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return whether this template uses the "empty" content model |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function isEmpty() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return whether this template lets content through via an xsl:apply-templates element |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function isPassthrough() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return whether this template uses the "transparent" content model |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function isTransparent() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return whether all branches have an ancestor that is a void element |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function isVoid() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Return whether this template preserves the whitespace in its descendants |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function preservesNewLines() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Analyses the content of the whole template and set $this->contentBitfield accordingly |
||
| 331 | */ |
||
| 332 | protected function analyseContent() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Records the HTML elements (and their bitfield) rendered at the root of the template |
||
| 348 | */ |
||
| 349 | protected function analyseRootNodes() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Analyses each branch that leads to an <xsl:apply-templates/> tag |
||
| 387 | */ |
||
| 388 | protected function analyseBranches() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Test whether any branch of this template has an element that has given property |
||
| 410 | * |
||
| 411 | * @param string $methodName |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | protected function anyBranchHasProperty($methodName) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Compute the allowChildBitfields and denyDescendantBitfield properties |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | protected function computeBitfields() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Compute the allowsChildElements property |
||
| 474 | * |
||
| 475 | * A template allows child Elements if it has at least one xsl:apply-templates and none of its |
||
| 476 | * ancestors have the text-only ("to") property |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | protected function computeAllowsChildElements() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Compute the allowsText property |
||
| 487 | * |
||
| 488 | * A template is said to allow text if none of the leaf elements disallow text |
||
| 489 | * |
||
| 490 | * @return void |
||
| 491 | */ |
||
| 492 | protected function computeAllowsText() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Compute the isFormattingElement property |
||
| 508 | * |
||
| 509 | * A template is said to be a formatting element if all (non-zero) of its branches are entirely |
||
| 510 | * composed of formatting elements |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | protected function computeFormattingElement() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Compute the isEmpty property |
||
| 533 | * |
||
| 534 | * A template is said to be empty if it has no xsl:apply-templates elements or any there is a empty |
||
| 535 | * element ancestor to an xsl:apply-templates element |
||
| 536 | * |
||
| 537 | * @return void |
||
| 538 | */ |
||
| 539 | protected function computeIsEmpty() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Compute the isTransparent property |
||
| 546 | * |
||
| 547 | * A template is said to be transparent if it has at least one branch and no non-transparent |
||
| 548 | * elements in its path |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | protected function computeIsTransparent() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Compute the isVoid property |
||
| 571 | * |
||
| 572 | * A template is said to be void if it has no xsl:apply-templates elements or any there is a void |
||
| 573 | * element ancestor to an xsl:apply-templates element |
||
| 574 | * |
||
| 575 | * @return void |
||
| 576 | */ |
||
| 577 | protected function computeIsVoid() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Compute the preservesNewLines property |
||
| 584 | * |
||
| 585 | * @return void |
||
| 586 | */ |
||
| 587 | protected function computePreservesNewLines() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Test whether given element is a block-level element |
||
| 609 | * |
||
| 610 | * @param DOMElement $element |
||
| 611 | * @return bool |
||
| 612 | */ |
||
| 613 | protected function elementIsBlock(DOMElement $element) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Retrieve and return the inline style assigned to given element |
||
| 630 | * |
||
| 631 | * @param DOMElement $node Context node |
||
| 632 | * @param bool $deep Whether to retrieve the content of all xsl:attribute descendants |
||
| 633 | * @return string |
||
| 634 | */ |
||
| 635 | protected function getStyle(DOMElement $node, $deep = false) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Test whether given node is a span element used for formatting |
||
| 656 | * |
||
| 657 | * Will return TRUE if the node is a span element with a class attribute and/or a style attribute |
||
| 658 | * and no other attributes |
||
| 659 | * |
||
| 660 | * @param DOMElement $node |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | protected function isFormattingSpan(DOMElement $node) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Store the names of every leaf node |
||
| 688 | * |
||
| 689 | * A leaf node is defined as the closest non-XSL ancestor to an xsl:apply-templates element |
||
| 690 | * |
||
| 691 | * @return void |
||
| 692 | */ |
||
| 693 | protected function storeLeafNodes() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Test whether two bitfields have any bits in common |
||
| 703 | * |
||
| 704 | * @param string $bitfield1 |
||
| 705 | * @param string $bitfield2 |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | protected static function match($bitfield1, $bitfield2) |
||
| 712 | } |