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 |
||
| 13 | class OptimizeChoose extends AbstractNormalization |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var DOMElement Current xsl:choose element |
||
| 17 | */ |
||
| 18 | protected $choose; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritdoc} |
||
| 22 | */ |
||
| 23 | protected $queries = ['//xsl:choose']; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Adopt the children of given element's only child |
||
| 27 | * |
||
| 28 | * @param DOMElement $branch |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | protected function adoptChildren(DOMElement $branch) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Retrieve a list of attributes from given element |
||
| 42 | * |
||
| 43 | * @return array NamespaceURI#nodeName as keys, attribute values as values |
||
| 44 | */ |
||
| 45 | protected function getAttributes(DOMElement $element) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return a list the xsl:when and xsl:otherwise children of current xsl:choose element |
||
| 59 | * |
||
| 60 | * @return DOMElement[] |
||
| 61 | */ |
||
| 62 | protected function getBranches() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Test whether current xsl:choose element has no content besides xsl:when and xsl:otherwise |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | protected function hasNoContent() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Test whether current xsl:choose element has an xsl:otherwise child |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | protected function hasOtherwise() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Test whether two nodes are identical |
||
| 93 | * |
||
| 94 | * ext/dom does not support isEqualNode() from DOM Level 3 so this is a makeshift replacement. |
||
| 95 | * Unlike the DOM 3 function, attributes order matters |
||
| 96 | * |
||
| 97 | * @param DOMNode $node1 |
||
| 98 | * @param DOMNode $node2 |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | protected function isEqualNode(DOMNode $node1, DOMNode $node2) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Test whether two elements have the same start tag |
||
| 108 | * |
||
| 109 | * @param DOMElement $el1 |
||
| 110 | * @param DOMElement $el2 |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | protected function isEqualTag(DOMElement $el1, DOMElement $el2) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Test whether all branches of current xsl:choose element share a common firstChild/lastChild |
||
| 120 | * |
||
| 121 | * @param string $childType Either firstChild or lastChild |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | protected function matchBranches($childType) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Test whether all branches of current xsl:choose element have a single child with the same start tag |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | protected function matchOnlyChild() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Move the firstChild of each branch before current xsl:choose |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | protected function moveFirstChildBefore() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Move the lastChild of each branch after current xsl:choose |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | protected function moveLastChildAfter() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | protected function normalizeElement(DOMElement $element) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Optimize current xsl:choose by moving out the first child of each branch if they match |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | protected function optimizeCommonFirstChild() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Optimize current xsl:choose by moving out the last child of each branch if they match |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | protected function optimizeCommonLastChild() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Optimize current xsl:choose by moving out only child of each branch if they match |
||
| 268 | * |
||
| 269 | * This will reorder xsl:choose/xsl:when/div into div/xsl:choose/xsl:when if every branch has |
||
| 270 | * the same only child (excluding the child's own descendants) |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | protected function optimizeCommonOnlyChild() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Optimize away the xsl:otherwise child of current xsl:choose if it's empty |
||
| 284 | * |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | protected function optimizeEmptyOtherwise() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Replace current xsl:choose with xsl:if if it has only one branch |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | protected function optimizeSingleBranch() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Reorder the current xsl:choose tree to make it a child of the first child of its first branch |
||
| 321 | * |
||
| 322 | * This will reorder xsl:choose/xsl:when/div into div/xsl:choose/xsl:when |
||
| 323 | * |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | protected function reparentChild() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | protected function reset() |
||
| 346 | } |