Complex classes like Compiler 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 Compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Compiler { |
||
16 | const T_IF = 'x-if'; |
||
|
|||
17 | const T_EACH = 'x-each'; |
||
18 | const T_WITH = 'x-with'; |
||
19 | const T_LITERAL = 'x-literal'; |
||
20 | const T_AS = 'x-as'; |
||
21 | const T_COMPONENT = 'x-component'; |
||
22 | const T_CHILDREN = 'x-children'; |
||
23 | const T_BLOCK = 'x-block'; |
||
24 | const T_ELSE = 'x-else'; |
||
25 | const T_EMPTY = 'x-empty'; |
||
26 | const T_X = 'x'; |
||
27 | const T_INCLUDE = 'x-include'; |
||
28 | const T_EBI = 'ebi'; |
||
29 | const T_UNESCAPE = 'x-unescape'; |
||
30 | const T_TAG = 'x-tag'; |
||
31 | |||
32 | const IDENT_REGEX = '`^([a-z0-9-]+)$`i'; |
||
33 | |||
34 | protected static $special = [ |
||
35 | self::T_COMPONENT => 1, |
||
36 | self::T_IF => 2, |
||
37 | self::T_ELSE => 3, |
||
38 | self::T_EACH => 4, |
||
39 | self::T_EMPTY => 5, |
||
40 | self::T_CHILDREN => 6, |
||
41 | self::T_INCLUDE => 7, |
||
42 | self::T_WITH => 8, |
||
43 | self::T_BLOCK => 9, |
||
44 | self::T_LITERAL => 10, |
||
45 | self::T_AS => 11, |
||
46 | self::T_UNESCAPE => 12, |
||
47 | self::T_TAG => 13 |
||
48 | ]; |
||
49 | |||
50 | protected static $htmlTags = [ |
||
51 | 'a' => 'i', |
||
52 | 'abbr' => 'i', |
||
53 | 'acronym' => 'i', // deprecated |
||
54 | 'address' => 'b', |
||
55 | // 'applet' => 'i', // deprecated |
||
56 | 'area' => 'i', |
||
57 | 'article' => 'b', |
||
58 | 'aside' => 'b', |
||
59 | 'audio' => 'i', |
||
60 | 'b' => 'i', |
||
61 | 'base' => 'i', |
||
62 | // 'basefont' => 'i', |
||
63 | 'bdi' => 'i', |
||
64 | 'bdo' => 'i', |
||
65 | // 'bgsound' => 'i', |
||
66 | // 'big' => 'i', |
||
67 | 'x' => 'i', |
||
68 | // 'blink' => 'i', |
||
69 | 'blockquote' => 'b', |
||
70 | 'body' => 'b', |
||
71 | 'br' => 'i', |
||
72 | 'button' => 'i', |
||
73 | 'canvas' => 'b', |
||
74 | 'caption' => 'i', |
||
75 | // 'center' => 'b', |
||
76 | 'cite' => 'i', |
||
77 | 'code' => 'i', |
||
78 | 'col' => 'i', |
||
79 | 'colgroup' => 'i', |
||
80 | // 'command' => 'i', |
||
81 | 'content' => 'i', |
||
82 | 'data' => 'i', |
||
83 | 'datalist' => 'i', |
||
84 | 'dd' => 'b', |
||
85 | 'del' => 'i', |
||
86 | 'details' => 'i', |
||
87 | 'dfn' => 'i', |
||
88 | 'dialog' => 'i', |
||
89 | // 'dir' => 'i', |
||
90 | 'div' => 'i', |
||
91 | 'dl' => 'b', |
||
92 | 'dt' => 'b', |
||
93 | // 'element' => 'i', |
||
94 | 'em' => 'i', |
||
95 | 'embed' => 'i', |
||
96 | 'fieldset' => 'b', |
||
97 | 'figcaption' => 'b', |
||
98 | 'figure' => 'b', |
||
99 | // 'font' => 'i', |
||
100 | 'footer' => 'b', |
||
101 | 'form' => 'b', |
||
102 | 'frame' => 'i', |
||
103 | 'frameset' => 'i', |
||
104 | 'h1' => 'b', |
||
105 | 'h2' => 'b', |
||
106 | 'h3' => 'b', |
||
107 | 'h4' => 'b', |
||
108 | 'h5' => 'b', |
||
109 | 'h6' => 'b', |
||
110 | 'head' => 'b', |
||
111 | 'header' => 'b', |
||
112 | 'hgroup' => 'b', |
||
113 | 'hr' => 'b', |
||
114 | 'html' => 'b', |
||
115 | 'i' => 'i', |
||
116 | 'iframe' => 'i', |
||
117 | 'image' => 'i', |
||
118 | 'img' => 'i', |
||
119 | 'input' => 'i', |
||
120 | 'ins' => 'i', |
||
121 | 'isindex' => 'i', |
||
122 | 'kbd' => 'i', |
||
123 | 'keygen' => 'i', |
||
124 | 'label' => 'i', |
||
125 | 'legend' => 'i', |
||
126 | 'li' => 'i', |
||
127 | 'link' => 'i', |
||
128 | // 'listing' => 'i', |
||
129 | 'main' => 'b', |
||
130 | 'map' => 'i', |
||
131 | 'mark' => 'i', |
||
132 | // 'marquee' => 'i', |
||
133 | 'menu' => 'i', |
||
134 | 'menuitem' => 'i', |
||
135 | 'meta' => 'i', |
||
136 | 'meter' => 'i', |
||
137 | 'multicol' => 'i', |
||
138 | 'nav' => 'b', |
||
139 | 'nobr' => 'i', |
||
140 | 'noembed' => 'i', |
||
141 | 'noframes' => 'i', |
||
142 | 'noscript' => 'b', |
||
143 | 'object' => 'i', |
||
144 | 'ol' => 'b', |
||
145 | 'optgroup' => 'i', |
||
146 | 'option' => 'b', |
||
147 | 'output' => 'i', |
||
148 | 'p' => 'b', |
||
149 | 'param' => 'i', |
||
150 | 'picture' => 'i', |
||
151 | // 'plaintext' => 'i', |
||
152 | 'pre' => 'b', |
||
153 | 'progress' => 'i', |
||
154 | 'q' => 'i', |
||
155 | 'rp' => 'i', |
||
156 | 'rt' => 'i', |
||
157 | 'rtc' => 'i', |
||
158 | 'ruby' => 'i', |
||
159 | 's' => 'i', |
||
160 | 'samp' => 'i', |
||
161 | 'script' => 'i', |
||
162 | 'section' => 'b', |
||
163 | 'select' => 'i', |
||
164 | // 'shadow' => 'i', |
||
165 | 'slot' => 'i', |
||
166 | 'small' => 'i', |
||
167 | 'source' => 'i', |
||
168 | // 'spacer' => 'i', |
||
169 | 'span' => 'i', |
||
170 | // 'strike' => 'i', |
||
171 | 'strong' => 'i', |
||
172 | 'style' => 'i', |
||
173 | 'sub' => 'i', |
||
174 | 'summary' => 'i', |
||
175 | 'sup' => 'i', |
||
176 | 'table' => 'b', |
||
177 | 'tbody' => 'i', |
||
178 | 'td' => 'i', |
||
179 | 'template' => 'i', |
||
180 | 'textarea' => 'i', |
||
181 | 'tfoot' => 'b', |
||
182 | 'th' => 'i', |
||
183 | 'thead' => 'i', |
||
184 | 'time' => 'i', |
||
185 | 'title' => 'i', |
||
186 | 'tr' => 'i', |
||
187 | 'track' => 'i', |
||
188 | // 'tt' => 'i', |
||
189 | 'u' => 'i', |
||
190 | 'ul' => 'b', |
||
191 | 'var' => 'i', |
||
192 | 'video' => 'b', |
||
193 | 'wbr' => 'i', |
||
194 | |||
195 | /// SVG /// |
||
196 | 'animate' => 's', |
||
197 | 'animateColor' => 's', |
||
198 | 'animateMotion' => 's', |
||
199 | 'animateTransform' => 's', |
||
200 | // 'canvas' => 's', |
||
201 | 'circle' => 's', |
||
202 | 'desc' => 's', |
||
203 | 'defs' => 's', |
||
204 | 'discard' => 's', |
||
205 | 'ellipse' => 's', |
||
206 | 'g' => 's', |
||
207 | // 'image' => 's', |
||
208 | 'line' => 's', |
||
209 | 'marker' => 's', |
||
210 | 'mask' => 's', |
||
211 | 'missing-glyph' => 's', |
||
212 | 'mpath' => 's', |
||
213 | 'metadata' => 's', |
||
214 | 'path' => 's', |
||
215 | 'pattern' => 's', |
||
216 | 'polygon' => 's', |
||
217 | 'polyline' => 's', |
||
218 | 'rect' => 's', |
||
219 | 'set' => 's', |
||
220 | 'svg' => 's', |
||
221 | 'switch' => 's', |
||
222 | 'symbol' => 's', |
||
223 | 'text' => 's', |
||
224 | // 'unknown' => 's', |
||
225 | 'use' => 's', |
||
226 | ]; |
||
227 | |||
228 | protected static $boolAttributes = [ |
||
229 | 'checked' => 1, |
||
230 | 'itemscope' => 1, |
||
231 | 'required' => 1, |
||
232 | 'selected' => 1, |
||
233 | ]; |
||
234 | |||
235 | /** |
||
236 | * @var ExpressionLanguage |
||
237 | */ |
||
238 | protected $expressions; |
||
239 | |||
240 | 75 | public function __construct() { |
|
252 | |||
253 | /** |
||
254 | * Register a runtime function. |
||
255 | * |
||
256 | * @param string $name The name of the function. |
||
257 | * @param callable $function The function callback. |
||
258 | */ |
||
259 | 73 | public function defineFunction($name, $function = null) { |
|
270 | |||
271 | 73 | private function getFunctionEvaluator($function) { |
|
284 | |||
285 | 73 | private function getFunctionCompiler($name, $function) { |
|
286 | 73 | $var = var_export(strtolower($name), true); |
|
287 | $fn = function ($expr) use ($var) { |
||
288 | 1 | return "\$this->call($var, $expr)"; |
|
289 | 73 | }; |
|
290 | |||
291 | 73 | if (is_string($function)) { |
|
292 | $fn = function (...$args) use ($function) { |
||
293 | 15 | return $function.'('.implode(', ', $args).')'; |
|
294 | 73 | }; |
|
295 | 73 | } elseif (is_array($function)) { |
|
296 | 72 | if (is_string($function[0])) { |
|
297 | $fn = function (...$args) use ($function) { |
||
298 | return "$function[0]::$function[1](".implode(', ', $args).')'; |
||
299 | }; |
||
300 | 72 | } elseif ($function[0] instanceof Ebi) { |
|
301 | $fn = function (...$args) use ($function) { |
||
302 | 8 | return "\$this->$function[1](".implode(', ', $args).')'; |
|
303 | 72 | }; |
|
304 | 72 | } |
|
305 | 72 | } |
|
306 | |||
307 | 73 | return $fn; |
|
308 | 1 | } |
|
309 | |||
310 | 67 | public function compile($src, array $options = []) { |
|
364 | |||
365 | 51 | protected function isComponent($tag) { |
|
368 | |||
369 | 67 | protected function compileNode(DOMNode $node, CompilerBuffer $out) { |
|
397 | |||
398 | protected function domToArray(DOMNode $root) { |
||
435 | |||
436 | 1 | protected function newline(DOMNode $node, CompilerBuffer $out) { |
|
441 | |||
442 | 1 | protected function compileCommentNode(\DOMComment $node, CompilerBuffer $out) { |
|
450 | |||
451 | 55 | protected function compileTextNode(DOMNode $node, CompilerBuffer $out) { |
|
482 | |||
483 | 63 | protected function compileElementNode(DOMElement $node, CompilerBuffer $out) { |
|
494 | |||
495 | 55 | protected function splitExpressions($value) { |
|
499 | |||
500 | /** |
||
501 | * Compile the PHP for an expression |
||
502 | * |
||
503 | * @param string $expr The expression to compile. |
||
504 | * @param CompilerBuffer $out The current output buffer. |
||
505 | * @param DOMAttr|null $attr The owner of the expression. |
||
506 | * @return string Returns a string of PHP code. |
||
507 | */ |
||
508 | 59 | protected function expr($expr, CompilerBuffer $out, DOMAttr $attr = null) { |
|
535 | |||
536 | /** |
||
537 | * Get the compiler function to wrap an attribute. |
||
538 | * |
||
539 | * Attribute functions are regular expression functions, but with a special naming convention. The following naming |
||
540 | * conventions are supported: |
||
541 | * |
||
542 | * - **@tag:attribute**: Applies to an attribute only on a specific tag. |
||
543 | * - **@attribute**: Applies to all attributes with a given name. |
||
544 | * |
||
545 | * @param DOMAttr $attr The attribute to look at. |
||
546 | * @return callable|null A function or **null** if the attribute doesn't have a function. |
||
547 | */ |
||
548 | 31 | private function getAttributeFunction(DOMAttr $attr) { |
|
557 | |||
558 | /** |
||
559 | * @param DOMElement $node |
||
560 | */ |
||
561 | 63 | protected function splitAttributes(DOMElement $node) { |
|
562 | 63 | $attributes = []; |
|
563 | 63 | $special = []; |
|
564 | |||
565 | 63 | foreach ($node->attributes as $name => $attribute) { |
|
566 | 59 | if (isset(static::$special[$name])) { |
|
567 | 44 | $special[$name] = $attribute; |
|
568 | 44 | } else { |
|
569 | 23 | $attributes[$name] = $attribute; |
|
570 | } |
||
571 | 63 | } |
|
572 | |||
573 | uksort($special, function ($a, $b) { |
||
574 | 12 | return strnatcmp(static::$special[$a], static::$special[$b]); |
|
575 | 63 | }); |
|
576 | |||
577 | 63 | return [$attributes, $special]; |
|
578 | } |
||
579 | |||
580 | 40 | protected function compileSpecialNode(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
627 | |||
628 | /** |
||
629 | * Compile component registering. |
||
630 | * |
||
631 | * @param DOMElement $node |
||
632 | * @param $attributes |
||
633 | * @param $special |
||
634 | * @param CompilerBuffer $out |
||
635 | */ |
||
636 | 10 | public function compileComponentRegister(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
656 | |||
657 | 3 | private function compileBlock(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
658 | // Blocks must be direct descendants of component includes. |
||
659 | 3 | if (!$out->getNodeProp($node->parentNode, self::T_INCLUDE)) { |
|
660 | 1 | throw $out->createCompilerException($node, new \Exception("Blocks must be direct descendants of component includes.")); |
|
661 | } |
||
662 | |||
663 | 2 | $name = strtolower($special[self::T_BLOCK]->value); |
|
664 | 2 | if (empty($name)) { |
|
665 | throw $out->createCompilerException($special[self::T_BLOCK], new \Exception("Block names cannot be empty.")); |
||
666 | } |
||
667 | 2 | if (!preg_match(self::IDENT_REGEX, $name)) { |
|
668 | throw $out->createCompilerException($special[self::T_BLOCK], new \Exception("The block name isn't a valid identifier.")); |
||
669 | } |
||
670 | |||
671 | 2 | unset($special[self::T_BLOCK]); |
|
672 | |||
673 | 2 | $prev = $out->select($name, true); |
|
674 | |||
675 | 2 | $vars = array_filter(array_unique($out->getScopeVariables())); |
|
676 | 2 | $vars[] = 'children'; |
|
677 | 2 | $use = '$'.implode(', $', array_unique($vars)); |
|
678 | |||
679 | 2 | $out->appendCode("function () use ($use) {\n"); |
|
680 | 2 | $out->pushScope(['this' => 'props']); |
|
681 | 2 | $out->indent(+1); |
|
682 | |||
683 | try { |
||
684 | 2 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
685 | 2 | } finally { |
|
686 | 2 | $out->indent(-1); |
|
687 | 2 | $out->popScope(); |
|
688 | 2 | $out->appendCode("}"); |
|
689 | 2 | $out->select($prev); |
|
690 | 2 | } |
|
691 | |||
692 | 2 | return $out; |
|
693 | } |
||
694 | |||
695 | /** |
||
696 | * Compile component inclusion and rendering. |
||
697 | * |
||
698 | * @param DOMElement $node |
||
699 | * @param DOMAttr[] $attributes |
||
700 | * @param DOMAttr[] $special |
||
701 | * @param CompilerBuffer $out |
||
702 | */ |
||
703 | 13 | protected function compileComponentInclude(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
704 | // Mark the node as a component include. |
||
705 | 13 | $out->setNodeProp($node, self::T_INCLUDE, true); |
|
706 | |||
707 | // Generate the attributes into a property array. |
||
708 | 13 | $props = []; |
|
709 | 13 | foreach ($attributes as $name => $attribute) { |
|
710 | /* @var DOMAttr $attr */ |
||
711 | 5 | if ($this->isExpression($attribute->value)) { |
|
712 | 4 | $expr = $this->expr(substr($attribute->value, 1, -1), $out, $attribute); |
|
713 | 4 | } else { |
|
714 | 1 | $expr = var_export($attribute->value, true); |
|
715 | } |
||
716 | |||
717 | 5 | $props[] = var_export($name, true).' => '.$expr; |
|
718 | 13 | } |
|
719 | 13 | $propsStr = '['.implode(', ', $props).']'; |
|
720 | |||
721 | 13 | if (isset($special[self::T_WITH])) { |
|
722 | 3 | $withExpr = $this->expr($special[self::T_WITH]->value, $out, $special[self::T_WITH]); |
|
723 | 3 | unset($special[self::T_WITH]); |
|
724 | |||
725 | 3 | $propsStr = empty($props) ? $withExpr : $propsStr.' + (array)'.$withExpr; |
|
726 | 13 | } elseif (empty($props)) { |
|
727 | // By default the current context is passed to components. |
||
728 | 6 | $propsStr = $this->expr('this', $out); |
|
729 | 6 | } |
|
730 | |||
731 | // Compile the children blocks. |
||
732 | 13 | $blocks = $this->compileComponentBlocks($node, $out); |
|
733 | 12 | $blocksStr = $blocks->flush(); |
|
734 | |||
735 | 12 | if (isset($special[self::T_INCLUDE])) { |
|
736 | 1 | $name = $this->expr($special[self::T_INCLUDE]->value, $out, $special[self::T_INCLUDE]); |
|
737 | 1 | } else { |
|
738 | 11 | $name = var_export($node->tagName, true); |
|
739 | } |
||
740 | |||
741 | 12 | $out->appendCode("\$this->write($name, $propsStr, $blocksStr);\n"); |
|
742 | 12 | } |
|
743 | |||
744 | /** |
||
745 | * @param DOMElement $parent |
||
746 | * @return CompilerBuffer |
||
747 | */ |
||
748 | 13 | protected function compileComponentBlocks(DOMElement $parent, CompilerBuffer $out) { |
|
749 | 13 | $blocksOut = new CompilerBuffer(CompilerBuffer::STYLE_ARRAY, [ |
|
750 | 13 | 'baseIndent' => $out->getIndent(), |
|
751 | 13 | 'indent' => $out->getIndent() + 1, |
|
752 | 13 | 'depth' => $out->getDepth(), |
|
753 | 13 | 'scopes' => $out->getAllScopes(), |
|
754 | 13 | 'nodeProps' => $out->getNodePropArray() |
|
755 | 13 | ]); |
|
756 | 13 | $blocksOut->setSource($out->getSource()); |
|
757 | |||
758 | 13 | if ($this->isEmptyNode($parent)) { |
|
759 | 9 | return $blocksOut; |
|
760 | } |
||
761 | |||
762 | 5 | $use = '$'.implode(', $', $blocksOut->getScopeVariables()).', $children'; |
|
763 | |||
764 | 5 | $blocksOut->appendCode("function () use ($use) {\n"); |
|
765 | 5 | $blocksOut->indent(+1); |
|
766 | |||
767 | try { |
||
768 | 5 | foreach ($parent->childNodes as $node) { |
|
769 | 5 | $this->compileNode($node, $blocksOut); |
|
770 | 4 | } |
|
771 | 4 | } finally { |
|
772 | 5 | $blocksOut->indent(-1); |
|
773 | 5 | $blocksOut->appendCode("}"); |
|
774 | 5 | } |
|
775 | |||
776 | 4 | return $blocksOut; |
|
777 | } |
||
778 | |||
779 | 28 | protected function compileTagComment(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
780 | // Don't double up comments. |
||
781 | 28 | if ($node->previousSibling && $node->previousSibling->nodeType === XML_COMMENT_NODE) { |
|
782 | return; |
||
783 | } |
||
784 | |||
785 | 28 | $str = '<'.$node->tagName; |
|
786 | 28 | foreach ($special as $attr) { |
|
787 | /* @var DOMAttr $attr */ |
||
788 | 28 | $str .= ' '.$attr->name.(empty($attr->value) ? '' : '="'.htmlspecialchars($attr->value).'"'); |
|
789 | 28 | } |
|
790 | 28 | $str .= '>'; |
|
791 | 28 | $comments = explode("\n", $str); |
|
792 | 28 | foreach ($comments as $comment) { |
|
793 | 28 | $out->appendCode("// $comment\n"); |
|
794 | 28 | } |
|
795 | 28 | } |
|
796 | |||
797 | /** |
||
798 | * @param DOMElement $node |
||
799 | * @param DOMAttr[] $attributes |
||
800 | * @param DOMAttr[] $special |
||
801 | * @param CompilerBuffer $out |
||
802 | * @param bool $force |
||
803 | */ |
||
804 | 52 | protected function compileOpenTag(DOMElement $node, $attributes, $special, CompilerBuffer $out, $force = false) { |
|
805 | 52 | $tagNameExpr = !empty($special[self::T_TAG]) ? $special[self::T_TAG]->value : ''; |
|
806 | |||
807 | 52 | if ($node->tagName === self::T_X && empty($tagNameExpr)) { |
|
808 | 5 | return; |
|
809 | } |
||
810 | |||
811 | 50 | if (!empty($tagNameExpr)) { |
|
812 | 1 | $tagNameExpr = $this->expr($tagNameExpr, $out, $special[self::T_TAG]); |
|
813 | 1 | $out->echoLiteral('<'); |
|
814 | 1 | $out->echoCode($tagNameExpr); |
|
815 | 1 | } else { |
|
816 | 50 | $out->echoLiteral('<'.$node->tagName); |
|
817 | } |
||
818 | |||
819 | /* @var DOMAttr $attribute */ |
||
820 | 50 | foreach ($attributes as $name => $attribute) { |
|
821 | // Check for an attribute expression. |
||
822 | 18 | if ($this->isExpression($attribute->value)) { |
|
823 | 14 | $out->echoCode( |
|
824 | 14 | '$this->attribute('.var_export($name, true).', '. |
|
825 | 14 | $this->expr(substr($attribute->value, 1, -1), $out, $attribute). |
|
826 | 14 | ')'); |
|
827 | 18 | } elseif (null !== $fn = $this->getAttributeFunction($attribute)) { |
|
828 | 3 | $value = call_user_func($fn, var_export($attribute->value, true)); |
|
829 | |||
830 | 3 | $out->echoCode('$this->attribute('.var_export($name, true).', '.$value.')'); |
|
831 | 5 | } elseif ((empty($attribute->value) || $attribute->value === $name) && isset(self::$boolAttributes[$name])) { |
|
832 | 2 | $out->echoLiteral(' '.$name); |
|
833 | 2 | } else { |
|
834 | 4 | $out->echoLiteral(' '.$name.'="'); |
|
835 | 4 | $out->echoLiteral(htmlspecialchars($attribute->value)); |
|
836 | 4 | $out->echoLiteral('"'); |
|
837 | } |
||
838 | 50 | } |
|
839 | |||
840 | 50 | if ($node->hasChildNodes() || $force) { |
|
841 | 48 | $out->echoLiteral('>'); |
|
842 | 48 | } else { |
|
843 | 3 | $out->echoLiteral(" />"); |
|
844 | } |
||
845 | 50 | } |
|
846 | |||
847 | 21 | private function isExpression($value) { |
|
850 | |||
851 | 50 | protected function compileCloseTag(DOMElement $node, $special, CompilerBuffer $out, $force = false) { |
|
852 | 50 | if (!$force && !$node->hasChildNodes()) { |
|
853 | 3 | return; |
|
854 | } |
||
855 | |||
856 | 48 | $tagNameExpr = !empty($special[self::T_TAG]) ? $special[self::T_TAG]->value : ''; |
|
857 | 48 | if (!empty($tagNameExpr)) { |
|
858 | 1 | $tagNameExpr = $this->expr($tagNameExpr, $out, $special[self::T_TAG]); |
|
859 | 1 | $out->echoLiteral('</'); |
|
860 | 1 | $out->echoCode($tagNameExpr); |
|
861 | 1 | $out->echoLiteral('>'); |
|
862 | 48 | } elseif ($node->tagName !== self::T_X) { |
|
863 | 46 | $out->echoLiteral("</{$node->tagName}>"); |
|
864 | 46 | } |
|
865 | 48 | } |
|
866 | |||
867 | 4 | protected function isEmptyText(DOMNode $node) { |
|
870 | |||
871 | 13 | protected function isEmptyNode(DOMNode $node) { |
|
872 | 13 | if (!$node->hasChildNodes()) { |
|
873 | 9 | return true; |
|
874 | } |
||
875 | |||
876 | 5 | foreach ($node->childNodes as $childNode) { |
|
877 | 5 | if ($childNode instanceof DOMElement) { |
|
878 | 3 | return false; |
|
887 | |||
888 | 10 | protected function compileIf(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
916 | |||
917 | 15 | protected function compileEach(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
949 | |||
950 | 2 | protected function compileWith(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
978 | |||
979 | 2 | protected function compileLiteral(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
992 | |||
993 | 21 | protected function compileElement(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
1002 | |||
1003 | /** |
||
1004 | * Find a special node in relation to another node. |
||
1005 | * |
||
1006 | * This method is used to find things such as x-empty and x-else elements. |
||
1007 | * |
||
1008 | * @param DOMElement $node The node to search in relation to. |
||
1009 | * @param string $attribute The name of the attribute to search for. |
||
1010 | * @param string $parentAttribute The name of the parent attribute to resolve conflicts. |
||
1011 | * @return DOMElement|null Returns the found element node or **null** if not found. |
||
1012 | */ |
||
1013 | 23 | protected function findSpecialNode(DOMElement $node, $attribute, $parentAttribute) { |
|
1042 | |||
1043 | /** |
||
1044 | * @param DOMElement $node |
||
1045 | * @param array $attributes |
||
1046 | * @param array $special |
||
1047 | * @param CompilerBuffer $out |
||
1048 | */ |
||
1049 | 15 | private function compileEachLoop(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
1087 | |||
1088 | 55 | protected function ltrim($text, \DOMNode $node, CompilerBuffer $out) { |
|
1114 | |||
1115 | /** |
||
1116 | * Whether or not a node can be skipped for the purposes of trimming whitespace. |
||
1117 | * |
||
1118 | * @param DOMNode|null $node The node to test. |
||
1119 | * @param CompilerBuffer|null $out The compiler information. |
||
1120 | * @return bool Returns **true** if whitespace can be trimmed right up to the node or **false** otherwise. |
||
1121 | */ |
||
1122 | 12 | private function canSkip(\DOMNode $node, CompilerBuffer $out) { |
|
1145 | |||
1146 | 55 | protected function rtrim($text, \DOMNode $node, CompilerBuffer $out) { |
|
1168 | |||
1169 | 55 | protected function inPre(\DOMNode $node) { |
|
1177 | |||
1178 | 4 | private function compileChildBlock(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
1201 | |||
1202 | /** |
||
1203 | * Compile an `<script type="ebi">` node. |
||
1204 | * |
||
1205 | * @param DOMElement $node The node to compile. |
||
1206 | * @param DOMAttr[] $attributes The node's attributes. |
||
1207 | * @param DOMAttr[] $special An array of special attributes. |
||
1208 | * @param CompilerBuffer $out The compiler output. |
||
1209 | */ |
||
1210 | 9 | private function compileExpressionNode(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
1254 | |||
1255 | /** |
||
1256 | * |
||
1257 | * |
||
1258 | * @param DOMNode $node |
||
1259 | * @param callable $test |
||
1260 | * @return bool |
||
1261 | */ |
||
1262 | 6 | private function closest(\DOMNode $node, callable $test) { |
|
1268 | |||
1269 | /** |
||
1270 | * @param DOMElement $node |
||
1271 | * @param $attributes |
||
1272 | * @param $special |
||
1273 | * @param CompilerBuffer $out |
||
1274 | */ |
||
1275 | 30 | protected function compileBasicElement(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
1284 | |||
1285 | 29 | protected function compileEscape($php) { |
|
1289 | } |
||
1290 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.