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 |
||
16 | class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface |
||
17 | { |
||
18 | /** |
||
19 | * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0 |
||
20 | */ |
||
21 | const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml'; |
||
22 | |||
23 | const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML'; |
||
24 | |||
25 | const NAMESPACE_SVG = 'http://www.w3.org/2000/svg'; |
||
26 | |||
27 | const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink'; |
||
28 | |||
29 | const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace'; |
||
30 | |||
31 | const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/'; |
||
32 | |||
33 | /** |
||
34 | * Holds the HTML5 element names that causes a namespace switch |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $implicitNamespaces = array( |
||
39 | self::NAMESPACE_HTML, |
||
40 | self::NAMESPACE_SVG, |
||
41 | self::NAMESPACE_MATHML, |
||
42 | self::NAMESPACE_XML, |
||
43 | self::NAMESPACE_XMLNS, |
||
44 | ); |
||
45 | |||
46 | const IM_IN_HTML = 1; |
||
47 | |||
48 | const IM_IN_SVG = 2; |
||
49 | |||
50 | const IM_IN_MATHML = 3; |
||
51 | |||
52 | /** |
||
53 | * Used as cache to detect if is available ENT_HTML5 |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private $hasHTML5 = false; |
||
57 | |||
58 | protected $traverser; |
||
59 | |||
60 | protected $encode = false; |
||
61 | |||
62 | protected $out; |
||
63 | |||
64 | protected $outputMode; |
||
65 | |||
66 | private $xpath; |
||
67 | |||
68 | protected $nonBooleanAttributes = array( |
||
69 | /* |
||
70 | array( |
||
71 | 'nodeNamespace'=>'http://www.w3.org/1999/xhtml', |
||
72 | 'attrNamespace'=>'http://www.w3.org/1999/xhtml', |
||
73 | |||
74 | 'nodeName'=>'img', 'nodeName'=>array('img', 'a'), |
||
75 | 'attrName'=>'alt', 'attrName'=>array('title', 'alt'), |
||
76 | ), |
||
77 | */ |
||
78 | array( |
||
79 | 'nodeNamespace' => 'http://www.w3.org/1999/xhtml', |
||
80 | 'attrName' => array('href', |
||
81 | 'hreflang', |
||
82 | 'http-equiv', |
||
83 | 'icon', |
||
84 | 'id', |
||
85 | 'keytype', |
||
86 | 'kind', |
||
87 | 'label', |
||
88 | 'lang', |
||
89 | 'language', |
||
90 | 'list', |
||
91 | 'maxlength', |
||
92 | 'media', |
||
93 | 'method', |
||
94 | 'name', |
||
95 | 'placeholder', |
||
96 | 'rel', |
||
97 | 'rows', |
||
98 | 'rowspan', |
||
99 | 'sandbox', |
||
100 | 'spellcheck', |
||
101 | 'scope', |
||
102 | 'seamless', |
||
103 | 'shape', |
||
104 | 'size', |
||
105 | 'sizes', |
||
106 | 'span', |
||
107 | 'src', |
||
108 | 'srcdoc', |
||
109 | 'srclang', |
||
110 | 'srcset', |
||
111 | 'start', |
||
112 | 'step', |
||
113 | 'style', |
||
114 | 'summary', |
||
115 | 'tabindex', |
||
116 | 'target', |
||
117 | 'title', |
||
118 | 'type', |
||
119 | 'value', |
||
120 | 'width', |
||
121 | 'border', |
||
122 | 'charset', |
||
123 | 'cite', |
||
124 | 'class', |
||
125 | 'code', |
||
126 | 'codebase', |
||
127 | 'color', |
||
128 | 'cols', |
||
129 | 'colspan', |
||
130 | 'content', |
||
131 | 'coords', |
||
132 | 'data', |
||
133 | 'datetime', |
||
134 | 'default', |
||
135 | 'dir', |
||
136 | 'dirname', |
||
137 | 'enctype', |
||
138 | 'for', |
||
139 | 'form', |
||
140 | 'formaction', |
||
141 | 'headers', |
||
142 | 'height', |
||
143 | 'accept', |
||
144 | 'accept-charset', |
||
145 | 'accesskey', |
||
146 | 'action', |
||
147 | 'align', |
||
148 | 'alt', |
||
149 | 'bgcolor', |
||
150 | ), |
||
151 | ), |
||
152 | array( |
||
153 | 'nodeNamespace' => 'http://www.w3.org/1999/xhtml', |
||
154 | 'xpath' => 'starts-with(local-name(), \'data-\')', |
||
155 | ), |
||
156 | ); |
||
157 | |||
158 | const DOCTYPE = '<!DOCTYPE html>'; |
||
159 | |||
160 | 64 | public function __construct($output, $options = array()) |
|
176 | |||
177 | 64 | public function setTraverser(\Masterminds\HTML5\Serializer\Traverser $traverser) |
|
183 | |||
184 | 18 | public function document($dom) |
|
194 | |||
195 | 19 | protected function doctype() |
|
200 | |||
201 | 27 | public function element($ele) |
|
247 | |||
248 | /** |
||
249 | * Write a text node. |
||
250 | * |
||
251 | * @param \DOMText $ele |
||
252 | * The text node to write. |
||
253 | */ |
||
254 | 24 | public function text($ele) |
|
264 | |||
265 | 2 | public function cdata($ele) |
|
270 | |||
271 | 3 | public function comment($ele) |
|
277 | |||
278 | 3 | public function processorInstruction($ele) |
|
286 | /** |
||
287 | * Write the namespace attributes |
||
288 | * |
||
289 | * |
||
290 | * @param \DOMNode $ele |
||
291 | * The element being written. |
||
292 | */ |
||
293 | 28 | protected function namespaceAttrs($ele) |
|
305 | |||
306 | /** |
||
307 | * Write the opening tag. |
||
308 | * |
||
309 | * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
||
310 | * qualified name (8.3). |
||
311 | * |
||
312 | * @param \DOMNode $ele |
||
313 | * The element being written. |
||
314 | */ |
||
315 | 28 | protected function openTag($ele) |
|
336 | |||
337 | 39 | protected function attrs($ele) |
|
373 | |||
374 | |||
375 | 10 | protected function nonBooleanAttribute(\DOMAttr $attr) |
|
416 | |||
417 | 9 | private function getXPath(\DOMNode $node){ |
|
423 | |||
424 | /** |
||
425 | * Write the closing tag. |
||
426 | * |
||
427 | * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
||
428 | * qualified name (8.3). |
||
429 | * |
||
430 | * @param \DOMNode $ele |
||
431 | * The element being written. |
||
432 | */ |
||
433 | 27 | protected function closeTag($ele) |
|
439 | |||
440 | /** |
||
441 | * Write to the output. |
||
442 | * |
||
443 | * @param string $text |
||
444 | * The string to put into the output. |
||
445 | * |
||
446 | * @return \Masterminds\HTML5\Serializer\Traverser $this so it can be used in chaining. |
||
447 | */ |
||
448 | 48 | protected function wr($text) |
|
453 | |||
454 | /** |
||
455 | * Write a new line character. |
||
456 | * |
||
457 | * @return \Masterminds\HTML5\Serializer\Traverser $this so it can be used in chaining. |
||
458 | */ |
||
459 | 20 | protected function nl() |
|
464 | |||
465 | /** |
||
466 | * Encode text. |
||
467 | * |
||
468 | * When encode is set to false, the default value, the text passed in is |
||
469 | * escaped per section 8.3 of the html5 spec. For details on how text is |
||
470 | * escaped see the escape() method. |
||
471 | * |
||
472 | * When encoding is set to true the text is converted to named character |
||
473 | * references where appropriate. Section 8.1.4 Character references of the |
||
474 | * html5 spec refers to using named character references. This is useful for |
||
475 | * characters that can't otherwise legally be used in the text. |
||
476 | * |
||
477 | * The named character references are listed in section 8.5. |
||
478 | * |
||
479 | * @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. |
||
480 | * This includes such characters as +.# and many other common ones. By default |
||
481 | * encoding here will just escape &'<>". |
||
482 | * |
||
483 | * Note, PHP 5.4+ has better html5 encoding. |
||
484 | * |
||
485 | * @todo Use the Entities class in php 5.3 to have html5 entities. |
||
486 | * |
||
487 | * @param string $text |
||
488 | * text to encode. |
||
489 | * @param boolean $attribute |
||
490 | * True if we are encoding an attrubute, false otherwise |
||
491 | * |
||
492 | * @return string The encoded text. |
||
493 | */ |
||
494 | 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 |
||
529 | * text to escape. |
||
530 | * @param boolean $attribute |
||
531 | * True if we are escaping an attrubute, false otherwise |
||
532 | */ |
||
533 | 51 | protected function escape($text, $attribute = false) |
|
556 | } |
||
557 |