Complex classes like OutputRules 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.
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 OutputRules, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class OutputRules implements RulesInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0. |
||
| 21 | */ |
||
| 22 | const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml'; |
||
| 23 | |||
| 24 | const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML'; |
||
| 25 | |||
| 26 | const NAMESPACE_SVG = 'http://www.w3.org/2000/svg'; |
||
| 27 | |||
| 28 | const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink'; |
||
| 29 | |||
| 30 | const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace'; |
||
| 31 | |||
| 32 | const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Holds the HTML5 element names that causes a namespace switch. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $implicitNamespaces = array( |
||
| 40 | self::NAMESPACE_HTML, |
||
| 41 | self::NAMESPACE_SVG, |
||
| 42 | self::NAMESPACE_MATHML, |
||
| 43 | self::NAMESPACE_XML, |
||
| 44 | self::NAMESPACE_XMLNS, |
||
| 45 | ); |
||
| 46 | |||
| 47 | const IM_IN_HTML = 1; |
||
| 48 | |||
| 49 | const IM_IN_SVG = 2; |
||
| 50 | |||
| 51 | const IM_IN_MATHML = 3; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Used as cache to detect if is available ENT_HTML5. |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $hasHTML5 = false; |
||
| 59 | |||
| 60 | protected $traverser; |
||
| 61 | |||
| 62 | protected $encode = false; |
||
| 63 | |||
| 64 | protected $out; |
||
| 65 | |||
| 66 | protected $outputMode; |
||
| 67 | |||
| 68 | private $xpath; |
||
| 69 | |||
| 70 | protected $nonBooleanAttributes = array( |
||
| 71 | /* |
||
| 72 | array( |
||
| 73 | 'nodeNamespace'=>'http://www.w3.org/1999/xhtml', |
||
| 74 | 'attrNamespace'=>'http://www.w3.org/1999/xhtml', |
||
| 75 | |||
| 76 | 'nodeName'=>'img', 'nodeName'=>array('img', 'a'), |
||
| 77 | 'attrName'=>'alt', 'attrName'=>array('title', 'alt'), |
||
| 78 | ), |
||
| 79 | */ |
||
| 80 | array( |
||
| 81 | 'nodeNamespace' => 'http://www.w3.org/1999/xhtml', |
||
| 82 | 'attrName' => array('href', |
||
| 83 | 'hreflang', |
||
| 84 | 'http-equiv', |
||
| 85 | 'icon', |
||
| 86 | 'id', |
||
| 87 | 'keytype', |
||
| 88 | 'kind', |
||
| 89 | 'label', |
||
| 90 | 'lang', |
||
| 91 | 'language', |
||
| 92 | 'list', |
||
| 93 | 'maxlength', |
||
| 94 | 'media', |
||
| 95 | 'method', |
||
| 96 | 'name', |
||
| 97 | 'placeholder', |
||
| 98 | 'rel', |
||
| 99 | 'rows', |
||
| 100 | 'rowspan', |
||
| 101 | 'sandbox', |
||
| 102 | 'spellcheck', |
||
| 103 | 'scope', |
||
| 104 | 'seamless', |
||
| 105 | 'shape', |
||
| 106 | 'size', |
||
| 107 | 'sizes', |
||
| 108 | 'span', |
||
| 109 | 'src', |
||
| 110 | 'srcdoc', |
||
| 111 | 'srclang', |
||
| 112 | 'srcset', |
||
| 113 | 'start', |
||
| 114 | 'step', |
||
| 115 | 'style', |
||
| 116 | 'summary', |
||
| 117 | 'tabindex', |
||
| 118 | 'target', |
||
| 119 | 'title', |
||
| 120 | 'type', |
||
| 121 | 'value', |
||
| 122 | 'width', |
||
| 123 | 'border', |
||
| 124 | 'charset', |
||
| 125 | 'cite', |
||
| 126 | 'class', |
||
| 127 | 'code', |
||
| 128 | 'codebase', |
||
| 129 | 'color', |
||
| 130 | 'cols', |
||
| 131 | 'colspan', |
||
| 132 | 'content', |
||
| 133 | 'coords', |
||
| 134 | 'data', |
||
| 135 | 'datetime', |
||
| 136 | 'default', |
||
| 137 | 'dir', |
||
| 138 | 'dirname', |
||
| 139 | 'enctype', |
||
| 140 | 'for', |
||
| 141 | 'form', |
||
| 142 | 'formaction', |
||
| 143 | 'headers', |
||
| 144 | 'height', |
||
| 145 | 'accept', |
||
| 146 | 'accept-charset', |
||
| 147 | 'accesskey', |
||
| 148 | 'action', |
||
| 149 | 'align', |
||
| 150 | 'alt', |
||
| 151 | 'bgcolor', |
||
| 152 | ), |
||
| 153 | ), |
||
| 154 | array( |
||
| 155 | 'nodeNamespace' => 'http://www.w3.org/1999/xhtml', |
||
| 156 | 'xpath' => 'starts-with(local-name(), \'data-\')', |
||
| 157 | ), |
||
| 158 | ); |
||
| 159 | |||
| 160 | const DOCTYPE = '<!DOCTYPE html>'; |
||
| 161 | |||
| 162 | 64 | public function __construct($output, $options = array()) |
|
| 172 | |||
| 173 | public function addRule(array $rule) |
||
| 177 | |||
| 178 | 64 | public function setTraverser(Traverser $traverser) |
|
| 184 | |||
| 185 | 17 | public function unsetTraverser() |
|
| 191 | |||
| 192 | 18 | public function document($dom) |
|
| 202 | |||
| 203 | 19 | protected function doctype() |
|
| 208 | |||
| 209 | 27 | public function element($ele) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Write a text node. |
||
| 258 | * |
||
| 259 | * @param \DOMText $ele The text node to write. |
||
| 260 | */ |
||
| 261 | 24 | public function text($ele) |
|
| 272 | |||
| 273 | 2 | public function cdata($ele) |
|
| 278 | |||
| 279 | 3 | public function comment($ele) |
|
| 285 | |||
| 286 | 3 | public function processorInstruction($ele) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Write the namespace attributes. |
||
| 297 | * |
||
| 298 | * @param \DOMNode $ele The element being written. |
||
| 299 | */ |
||
| 300 | 28 | protected function namespaceAttrs($ele) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Write the opening tag. |
||
| 315 | * |
||
| 316 | * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
||
| 317 | * qualified name (8.3). |
||
| 318 | * |
||
| 319 | * @param \DOMNode $ele The element being written. |
||
| 320 | */ |
||
| 321 | 28 | protected function openTag($ele) |
|
| 340 | |||
| 341 | 39 | protected function attrs($ele) |
|
| 377 | |||
| 378 | 10 | protected function nonBooleanAttribute(\DOMAttr $attr) |
|
| 417 | |||
| 418 | 9 | private function getXPath(\DOMNode $node) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Write the closing tag. |
||
| 429 | * |
||
| 430 | * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
||
| 431 | * qualified name (8.3). |
||
| 432 | * |
||
| 433 | * @param \DOMNode $ele The element being written. |
||
| 434 | */ |
||
| 435 | 27 | protected function closeTag($ele) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Write to the output. |
||
| 444 | * |
||
| 445 | * @param string $text The string to put into the output |
||
| 446 | * |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | 48 | protected function wr($text) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Write a new line character. |
||
| 458 | * |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | 20 | protected function nl() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Encode text. |
||
| 470 | * |
||
| 471 | * When encode is set to false, the default value, the text passed in is |
||
| 472 | * escaped per section 8.3 of the html5 spec. For details on how text is |
||
| 473 | * escaped see the escape() method. |
||
| 474 | * |
||
| 475 | * When encoding is set to true the text is converted to named character |
||
| 476 | * references where appropriate. Section 8.1.4 Character references of the |
||
| 477 | * html5 spec refers to using named character references. This is useful for |
||
| 478 | * characters that can't otherwise legally be used in the text. |
||
| 479 | * |
||
| 480 | * The named character references are listed in section 8.5. |
||
| 481 | * |
||
| 482 | * @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#named-character-references True encoding will turn all named character references into their entities. |
||
| 483 | * This includes such characters as +.# and many other common ones. By default |
||
| 484 | * encoding here will just escape &'<>". |
||
| 485 | * |
||
| 486 | * Note, PHP 5.4+ has better html5 encoding. |
||
| 487 | * |
||
| 488 | * @todo Use the Entities class in php 5.3 to have html5 entities. |
||
| 489 | * |
||
| 490 | * @param string $text Text to encode. |
||
| 491 | * @param bool $attribute True if we are encoding an attrubute, false otherwise. |
||
| 492 | * |
||
| 493 | * @return string The encoded text. |
||
| 494 | */ |
||
| 495 | 44 | protected function enc($text, $attribute = false) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Escape test. |
||
| 516 | * |
||
| 517 | * According to the html5 spec section 8.3 Serializing HTML fragments, text |
||
| 518 | * within tags that are not style, script, xmp, iframe, noembed, and noframes |
||
| 519 | * need to be properly escaped. |
||
| 520 | * |
||
| 521 | * The & should be converted to &, no breaking space unicode characters |
||
| 522 | * converted to , when in attribute mode the " should be converted to |
||
| 523 | * ", and when not in attribute mode the < and > should be converted to |
||
| 524 | * < and >. |
||
| 525 | * |
||
| 526 | * @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#escapingString |
||
| 527 | * |
||
| 528 | * @param string $text Text to escape. |
||
| 529 | * @param bool $attribute True if we are escaping an attrubute, false otherwise. |
||
| 530 | */ |
||
| 531 | 51 | protected function escape($text, $attribute = false) |
|
| 553 | } |
||
| 554 |