Complex classes like OptimizeChoose 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 OptimizeChoose, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class OptimizeChoose extends TemplateNormalization |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var DOMElement Current xsl:choose element |
||
| 19 | */ |
||
| 20 | protected $choose; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var DOMXPath XPath object for current template |
||
| 24 | */ |
||
| 25 | protected $xpath; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Optimize xsl:choose branches by moving their common children out of them |
||
| 29 | * |
||
| 30 | * @param DOMElement $template <xsl:template/> node |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function normalize(DOMElement $template) |
||
| 34 | { |
||
| 35 | $this->xpath = new DOMXPath($template->ownerDocument); |
||
| 36 | foreach ($template->getElementsByTagNameNS(self::XMLNS_XSL, 'choose') as $choose) |
||
| 37 | { |
||
| 38 | $this->choose = $choose; |
||
| 39 | $this->optimizeChooseElement(); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Adopt the children of given element's only child |
||
| 45 | * |
||
| 46 | * @param DOMElement $branch |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | protected function adoptChildren(DOMElement $branch) |
||
| 50 | { |
||
| 51 | while ($branch->firstChild->firstChild) |
||
| 52 | { |
||
| 53 | $branch->appendChild($branch->firstChild->removeChild($branch->firstChild->firstChild)); |
||
| 54 | } |
||
| 55 | $branch->removeChild($branch->firstChild); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Retrieve a list of attributes from given element |
||
| 60 | * |
||
| 61 | * @return array NamespaceURI#nodeName as keys, attribute values as values |
||
| 62 | */ |
||
| 63 | protected function getAttributes(DOMElement $element) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return a list the xsl:when and xsl:otherwise children of current xsl:choose element |
||
| 77 | * |
||
| 78 | * @return DOMElement[] |
||
| 79 | */ |
||
| 80 | protected function getBranches() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Test whether current xsl:choose element has no content besides xsl:when and xsl:otherwise |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | protected function hasNoContent() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Test whether current xsl:choose element has an xsl:otherwise child |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | protected function hasOtherwise() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Test whether two nodes are identical |
||
| 111 | * |
||
| 112 | * ext/dom does not support isEqualNode() from DOM Level 3 so this is a makeshift replacement. |
||
| 113 | * Unlike the DOM 3 function, attributes order matters |
||
| 114 | * |
||
| 115 | * @param DOMNode $node1 |
||
| 116 | * @param DOMNode $node2 |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | protected function isEqualNode(DOMNode $node1, DOMNode $node2) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Test whether two elements have the same start tag |
||
| 126 | * |
||
| 127 | * @param DOMElement $el1 |
||
| 128 | * @param DOMElement $el2 |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | protected function isEqualTag(DOMElement $el1, DOMElement $el2) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Test whether given node is an xsl:choose element |
||
| 138 | * |
||
| 139 | * @param DOMNode $node |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | protected function isXslChoose(DOMNode $node) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Test whether all branches of current xsl:choose element share a common firstChild/lastChild |
||
| 149 | * |
||
| 150 | * @param string $childType Either firstChild or lastChild |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | protected function matchBranches($childType) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Test whether all branches of current xsl:choose element have a single child with the same start tag |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | protected function matchOnlyChild() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Move the firstChild of each branch before current xsl:choose |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | protected function moveFirstChildBefore() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Move the lastChild of each branch after current xsl:choose |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | protected function moveLastChildAfter() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Optimize current xsl:choose element |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | protected function optimizeChooseElement() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Optimize current xsl:choose by moving out the first child of each branch if they match |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | protected function optimizeCommonFirstChild() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Optimize current xsl:choose by moving out the last child of each branch if they match |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function optimizeCommonLastChild() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Optimize current xsl:choose by moving out only child of each branch if they match |
||
| 298 | * |
||
| 299 | * This will reorder xsl:choose/xsl:when/div into div/xsl:choose/xsl:when if every branch has |
||
| 300 | * the same only child (excluding the child's own descendants) |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | protected function optimizeCommonOnlyChild() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Optimize away the xsl:otherwise child of current xsl:choose if it's empty |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | protected function optimizeEmptyOtherwise() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Replace current xsl:choose with xsl:if if it has only one branch |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | protected function optimizeSingleBranch() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Reorder the current xsl:choose tree to make it a child of the first child of its first branch |
||
| 351 | * |
||
| 352 | * This will reorder xsl:choose/xsl:when/div into div/xsl:choose/xsl:when |
||
| 353 | * |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | protected function reparentChild() |
||
| 367 | } |