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 | 44 | 'use' => 's', |
|
| 226 | 44 | ]; |
|
| 227 | 44 | ||
| 228 | 44 | protected static $boolAttributes = [ |
|
| 229 | 'checked' => 1, |
||
| 230 | 'itemscope' => 1, |
||
| 231 | 'required' => 1, |
||
| 232 | 'selected' => 1, |
||
| 233 | ]; |
||
| 234 | |||
| 235 | /** |
||
| 236 | 42 | * @var ExpressionLanguage |
|
| 237 | 42 | */ |
|
| 238 | 1 | protected $expressions; |
|
| 239 | |||
| 240 | public function __construct() { |
||
| 252 | 42 | ||
| 253 | 41 | /** |
|
| 254 | * Register a runtime function. |
||
| 255 | * |
||
| 256 | * @param string $name The name of the function. |
||
| 257 | * @param callable $function The function callback. |
||
| 258 | */ |
||
| 259 | 41 | public function defineFunction($name, $function = null) { |
|
| 270 | 7 | ||
| 271 | 42 | private function getFunctionEvaluator($function) { |
|
| 284 | 42 | ||
| 285 | private function getFunctionCompiler($name, $function) { |
||
| 309 | 37 | ||
| 310 | 37 | public function compile($src, array $options = []) { |
|
| 364 | |||
| 365 | 38 | protected function isComponent($tag) { |
|
| 368 | |||
| 369 | protected function compileNode(DOMNode $node, CompilerBuffer $out) { |
||
| 397 | |||
| 398 | protected function domToArray(DOMNode $root) { |
||
| 435 | |||
| 436 | protected function newline(DOMNode $node, CompilerBuffer $out) { |
||
| 441 | |||
| 442 | protected function compileCommentNode(\DOMComment $node, CompilerBuffer $out) { |
||
| 450 | |||
| 451 | 17 | protected function compileTextNode(DOMNode $node, CompilerBuffer $out) { |
|
| 452 | $nodeText = $node->nodeValue; |
||
| 453 | 17 | ||
| 454 | 17 | $items = $this->splitExpressions($nodeText); |
|
| 455 | foreach ($items as $i => list($text, $offset)) { |
||
| 456 | if (preg_match('`^{\S`', $text)) { |
||
| 457 | 17 | if (preg_match('`^{\s*unescape\((.+)\)\s*}$`', $text, $m)) { |
|
| 458 | $out->echoCode($this->expr($m[1], $out)); |
||
| 459 | 35 | } else { |
|
| 460 | try { |
||
| 461 | 36 | $expr = substr($text, 1, -1); |
|
| 462 | 36 | $out->echoCode($this->compileEscape($this->expr($expr, $out))); |
|
| 463 | 36 | } catch (SyntaxError $ex) { |
|
| 464 | $nodeLineCount = substr_count($nodeText, "\n"); |
||
| 465 | $offsetLineCount = substr_count($nodeText, "\n", 0, $offset); |
||
| 466 | 34 | $line = $node->getLineNo() - $nodeLineCount + $offsetLineCount; |
|
| 467 | 34 | throw $out->createCompilerException($node, $ex, ['source' => $expr, 'line' => $line]); |
|
| 468 | } |
||
| 469 | 34 | } |
|
| 470 | 34 | } else { |
|
| 471 | 16 | if ($i === 0) { |
|
| 472 | 29 | $text = $this->ltrim($text, $node, $out); |
|
| 473 | 1 | } |
|
| 474 | if ($i === count($items) - 1) { |
||
| 475 | 28 | $text = $this->rtrim($text, $node, $out); |
|
| 476 | } |
||
| 477 | 34 | ||
| 478 | $out->echoLiteral($text); |
||
| 479 | 34 | } |
|
| 480 | 4 | } |
|
| 481 | } |
||
| 482 | |||
| 483 | 34 | protected function compileElementNode(DOMElement $node, CompilerBuffer $out) { |
|
| 484 | list($attributes, $special) = $this->splitAttributes($node); |
||
| 485 | |||
| 486 | if ($node->tagName === 'script' && ((isset($attributes['type']) && $attributes['type']->value === self::T_EBI) || !empty($special[self::T_AS]) || !empty($special[self::T_UNESCAPE]))) { |
||
| 487 | $this->compileExpressionNode($node, $attributes, $special, $out); |
||
| 488 | } elseif (!empty($special) || $this->isComponent($node->tagName)) { |
||
| 489 | $this->compileSpecialNode($node, $attributes, $special, $out); |
||
| 490 | } else { |
||
| 491 | $this->compileBasicElement($node, $attributes, $special, $out); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | protected function splitExpressions($value) { |
||
| 496 | $values = preg_split('`({\S[^}]*?})`', $value, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE); |
||
| 497 | return $values; |
||
| 498 | 10 | } |
|
| 499 | 10 | ||
| 500 | /** |
||
| 501 | 10 | * Compile the PHP for an expression |
|
| 502 | 10 | * |
|
| 503 | 10 | * @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 | 6 | * @return string Returns a string of PHP code. |
|
| 507 | */ |
||
| 508 | protected function expr($expr, CompilerBuffer $out, DOMAttr $attr = null) { |
||
| 509 | $names = $out->getScopeVariables(); |
||
| 510 | |||
| 511 | 35 | try { |
|
| 512 | 35 | $compiled = $this->expressions->compile($expr, function ($name) use ($names) { |
|
| 513 | 35 | if (isset($names[$name])) { |
|
| 514 | return $names[$name]; |
||
| 515 | 35 | } elseif ($name[0] === '@') { |
|
| 516 | 34 | return 'this->meta['.var_export(substr($name, 1), true).']'; |
|
| 517 | 29 | } else { |
|
| 518 | return $names['this'].'['.var_export($name, true).']'; |
||
| 519 | 34 | } |
|
| 520 | }); |
||
| 521 | } catch (SyntaxError $ex) { |
||
| 522 | if ($attr !== null) { |
||
| 523 | 35 | throw $out->createCompilerException($attr, $ex); |
|
| 524 | 7 | } else { |
|
| 525 | 35 | throw $ex; |
|
| 526 | } |
||
| 527 | 35 | } |
|
| 528 | |||
| 529 | if ($attr !== null && null !== $fn = $this->getAttributeFunction($attr)) { |
||
| 530 | 30 | $compiled = call_user_func($fn, $compiled); |
|
| 531 | 30 | } |
|
| 532 | |||
| 533 | return $compiled; |
||
| 534 | 30 | } |
|
| 535 | 8 | ||
| 536 | 8 | /** |
|
| 537 | 30 | * Get the compiler function to wrap an attribute. |
|
| 538 | 7 | * |
|
| 539 | 7 | * Attribute functions are regular expression functions, but with a special naming convention. The following naming |
|
| 540 | 30 | * conventions are supported: |
|
| 541 | 12 | * |
|
| 542 | 12 | * - **@tag:attribute**: Applies to an attribute only on a specific tag. |
|
| 543 | 21 | * - **@attribute**: Applies to all attributes with a given name. |
|
| 544 | 1 | * |
|
| 545 | 1 | * @param DOMAttr $attr The attribute to look at. |
|
| 546 | 21 | * @return callable|null A function or **null** if the attribute doesn't have a function. |
|
| 547 | 2 | */ |
|
| 548 | 2 | private function getAttributeFunction(DOMAttr $attr) { |
|
| 557 | 1 | ||
| 558 | /** |
||
| 559 | 3 | * @param DOMElement $node |
|
| 560 | 21 | */ |
|
| 561 | 2 | protected function splitAttributes(DOMElement $node) { |
|
| 562 | 2 | $attributes = []; |
|
| 563 | 19 | $special = []; |
|
| 564 | 19 | ||
| 565 | 6 | foreach ($node->attributes as $name => $attribute) { |
|
| 566 | if (isset(static::$special[$name])) { |
||
| 567 | 18 | $special[$name] = $attribute; |
|
| 568 | } else { |
||
| 569 | 19 | $attributes[$name] = $attribute; |
|
| 570 | } |
||
| 571 | 30 | } |
|
| 572 | |||
| 573 | uksort($special, function ($a, $b) { |
||
| 574 | return strnatcmp(static::$special[$a], static::$special[$b]); |
||
| 575 | }); |
||
| 576 | |||
| 577 | return [$attributes, $special]; |
||
| 578 | } |
||
| 579 | |||
| 580 | protected function compileSpecialNode(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 581 | 8 | $specialName = key($special); |
|
| 582 | 8 | ||
| 583 | 8 | switch ($specialName) { |
|
| 584 | case self::T_COMPONENT: |
||
| 585 | 8 | $this->compileComponentRegister($node, $attributes, $special, $out); |
|
| 586 | break; |
||
| 587 | 8 | case self::T_IF: |
|
| 588 | 8 | $this->compileIf($node, $attributes, $special, $out); |
|
| 589 | 8 | break; |
|
| 590 | 8 | case self::T_EACH: |
|
| 591 | $this->compileEach($node, $attributes, $special, $out); |
||
| 592 | break; |
||
| 593 | 8 | case self::T_BLOCK: |
|
| 594 | 8 | $this->compileBlock($node, $attributes, $special, $out); |
|
| 595 | 8 | break; |
|
| 596 | 8 | case self::T_CHILDREN: |
|
| 597 | 8 | $this->compileChildBlock($node, $attributes, $special, $out); |
|
| 598 | 8 | break; |
|
| 599 | case self::T_INCLUDE: |
||
| 600 | 8 | $this->compileComponentInclude($node, $attributes, $special, $out); |
|
| 601 | break; |
||
| 602 | 1 | case self::T_WITH: |
|
| 603 | 1 | if ($this->isComponent($node->tagName)) { |
|
| 604 | 1 | // With has a special meaning in components. |
|
| 605 | $this->compileComponentInclude($node, $attributes, $special, $out); |
||
| 606 | 1 | } else { |
|
| 607 | $this->compileWith($node, $attributes, $special, $out); |
||
| 608 | 1 | } |
|
| 609 | break; |
||
| 610 | 1 | case self::T_LITERAL: |
|
| 611 | 1 | $this->compileLiteral($node, $attributes, $special, $out); |
|
| 612 | 1 | break; |
|
| 613 | case '': |
||
| 614 | if ($this->isComponent($node->tagName)) { |
||
| 615 | 1 | $this->compileComponentInclude($node, $attributes, $special, $out); |
|
| 616 | 1 | } else { |
|
| 617 | 1 | $this->compileElement($node, $attributes, $special, $out); |
|
| 618 | 1 | } |
|
| 619 | 1 | break; |
|
| 620 | 1 | case self::T_TAG: |
|
| 621 | default: |
||
| 622 | // This is only a tag node so it just gets compiled as an element. |
||
| 623 | 1 | $this->compileBasicElement($node, $attributes, $special, $out); |
|
| 624 | break; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Compile component registering. |
||
| 630 | * |
||
| 631 | * @param DOMElement $node |
||
| 632 | * @param $attributes |
||
| 633 | * @param $special |
||
| 634 | 9 | * @param CompilerBuffer $out |
|
| 635 | */ |
||
| 636 | 9 | public function compileComponentRegister(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
| 637 | 9 | $name = strtolower($special[self::T_COMPONENT]->value); |
|
| 638 | unset($special[self::T_COMPONENT]); |
||
| 639 | 5 | ||
| 640 | 4 | $prev = $out->select($name); |
|
| 641 | |||
| 642 | 1 | $varName = var_export($name, true); |
|
| 643 | $out->appendCode("\$this->defineComponent($varName, function (\$props = [], \$children = []) {\n"); |
||
| 644 | $out->pushScope(['this' => 'props']); |
||
| 645 | 5 | $out->indent(+1); |
|
| 646 | |||
| 647 | 9 | try { |
|
| 648 | $this->compileSpecialNode($node, $attributes, $special, $out); |
||
| 649 | 9 | } finally { |
|
| 650 | 2 | $out->popScope(); |
|
| 651 | 2 | $out->indent(-1); |
|
| 652 | $out->appendCode("});"); |
||
| 653 | 2 | $out->select($prev); |
|
| 654 | } |
||
| 655 | } |
||
| 656 | 3 | ||
| 657 | private function compileBlock(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 658 | // Blocks must be direct descendants of component includes. |
||
| 659 | if (!$out->getNodeProp($node->parentNode, self::T_INCLUDE)) { |
||
| 660 | 9 | throw $out->createCompilerException($node, new \Exception("Blocks must be direct descendants of component includes.")); |
|
| 661 | 9 | } |
|
| 662 | |||
| 663 | 9 | $name = strtolower($special[self::T_BLOCK]->value); |
|
| 664 | 1 | if (empty($name)) { |
|
| 665 | throw $out->createCompilerException($special[self::T_BLOCK], new \Exception("Block names cannot be empty.")); |
||
| 666 | 8 | } |
|
| 667 | 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 | 9 | } |
|
| 670 | 9 | ||
| 671 | unset($special[self::T_BLOCK]); |
||
| 672 | |||
| 673 | $prev = $out->select($name, true); |
||
| 674 | |||
| 675 | $vars = array_filter(array_unique($out->getScopeVariables())); |
||
| 676 | 9 | $vars[] = 'children'; |
|
| 677 | 9 | $use = '$'.implode(', $', array_unique($vars)); |
|
| 678 | 9 | ||
| 679 | 9 | $out->appendCode("function () use ($use) {\n"); |
|
| 680 | 9 | $out->pushScope(['this' => 'props']); |
|
| 681 | 9 | $out->indent(+1); |
|
| 682 | |||
| 683 | try { |
||
| 684 | 9 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 685 | 7 | } finally { |
|
| 686 | $out->indent(-1); |
||
| 687 | $out->popScope(); |
||
| 688 | 2 | $out->appendCode("}"); |
|
| 689 | $out->select($prev); |
||
| 690 | 2 | } |
|
| 691 | 2 | ||
| 692 | return $out; |
||
| 693 | } |
||
| 694 | 2 | ||
| 695 | 2 | /** |
|
| 696 | * Compile component inclusion and rendering. |
||
| 697 | 2 | * |
|
| 698 | 2 | * @param DOMElement $node |
|
| 699 | 2 | * @param DOMAttr[] $attributes |
|
| 700 | * @param DOMAttr[] $special |
||
| 701 | * @param CompilerBuffer $out |
||
| 702 | 2 | */ |
|
| 703 | protected function compileComponentInclude(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 704 | // Mark the node as a component include. |
||
| 705 | 22 | $out->setNodeProp($node, self::T_INCLUDE, true); |
|
| 706 | |||
| 707 | 22 | // Generate the attributes into a property array. |
|
| 708 | $props = []; |
||
| 709 | foreach ($attributes as $name => $attribute) { |
||
| 710 | /* @var DOMAttr $attr */ |
||
| 711 | 22 | if ($this->isExpression($attribute->value)) { |
|
| 712 | 22 | $expr = $this->expr(substr($attribute->value, 1, -1), $out, $attribute); |
|
| 713 | } else { |
||
| 714 | 22 | $expr = var_export($attribute->value, true); |
|
| 715 | } |
||
| 716 | 22 | ||
| 717 | 22 | $props[] = var_export($name, true).' => '.$expr; |
|
| 718 | 22 | } |
|
| 719 | 22 | $propsStr = '['.implode(', ', $props).']'; |
|
| 720 | |||
| 721 | 22 | if (isset($special[self::T_WITH])) { |
|
| 722 | $withExpr = $this->expr($special[self::T_WITH]->value, $out, $special[self::T_WITH]); |
||
| 723 | 35 | unset($special[self::T_WITH]); |
|
| 724 | 35 | ||
| 725 | 3 | $propsStr = empty($props) ? $withExpr : $propsStr.' + (array)'.$withExpr; |
|
| 726 | } elseif (empty($props)) { |
||
| 727 | // By default the current context is passed to components. |
||
| 728 | 34 | $propsStr = $this->expr('this', $out); |
|
| 729 | } |
||
| 730 | 34 | ||
| 731 | // Compile the children blocks. |
||
| 732 | 8 | $blocks = $this->compileComponentBlocks($node, $out); |
|
| 733 | $blocksStr = $blocks->flush(); |
||
| 734 | |||
| 735 | 8 | if (isset($special[self::T_INCLUDE])) { |
|
| 736 | 6 | $name = $this->expr($special[self::T_INCLUDE]->value, $out, $special[self::T_INCLUDE]); |
|
| 737 | } else { |
||
| 738 | 2 | $name = var_export($node->tagName, true); |
|
| 739 | } |
||
| 740 | |||
| 741 | 8 | $out->appendCode("\$this->write($name, $propsStr, $blocksStr);\n"); |
|
| 742 | } |
||
| 743 | |||
| 744 | 34 | /** |
|
| 745 | 34 | * @param DOMElement $parent |
|
| 746 | * @return CompilerBuffer |
||
| 747 | 1 | */ |
|
| 748 | protected function compileComponentBlocks(DOMElement $parent, CompilerBuffer $out) { |
||
| 749 | 34 | $blocksOut = new CompilerBuffer(CompilerBuffer::STYLE_ARRAY, [ |
|
| 750 | 'baseIndent' => $out->getIndent(), |
||
| 751 | 11 | 'indent' => $out->getIndent() + 1, |
|
| 752 | 11 | 'depth' => $out->getDepth(), |
|
| 753 | 'scopes' => $out->getAllScopes(), |
||
| 754 | 'nodeProps' => $out->getNodePropArray() |
||
| 755 | 35 | ]); |
|
| 756 | 35 | $blocksOut->setSource($out->getSource()); |
|
| 757 | 34 | ||
| 758 | if ($this->isEmptyNode($parent)) { |
||
| 759 | 35 | return $blocksOut; |
|
| 760 | } |
||
| 761 | 3 | ||
| 762 | 3 | $use = '$'.implode(', $', $blocksOut->getScopeVariables()).', $children'; |
|
| 763 | |||
| 764 | $blocksOut->appendCode("function () use ($use) {\n"); |
||
| 765 | 9 | $blocksOut->indent(+1); |
|
| 766 | 9 | ||
| 767 | 7 | try { |
|
| 768 | foreach ($parent->childNodes as $node) { |
||
| 769 | $this->compileNode($node, $blocksOut); |
||
| 770 | 2 | } |
|
| 771 | 2 | } finally { |
|
| 772 | 1 | $blocksOut->indent(-1); |
|
| 773 | $blocksOut->appendCode("}"); |
||
| 774 | 1 | } |
|
| 775 | 1 | ||
| 776 | return $blocksOut; |
||
| 777 | } |
||
| 778 | |||
| 779 | protected function compileTagComment(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
||
| 796 | |||
| 797 | 7 | /** |
|
| 798 | 2 | * @param DOMElement $node |
|
| 799 | 2 | * @param DOMAttr[] $attributes |
|
| 800 | * @param DOMAttr[] $special |
||
| 801 | 2 | * @param CompilerBuffer $out |
|
| 802 | * @param bool $force |
||
| 803 | 2 | */ |
|
| 804 | 2 | protected function compileOpenTag(DOMElement $node, $attributes, $special, CompilerBuffer $out, $force = false) { |
|
| 846 | 1 | ||
| 847 | private function isExpression($value) { |
||
| 850 | 1 | ||
| 851 | protected function compileCloseTag(DOMElement $node, $special, CompilerBuffer $out, $force = false) { |
||
| 852 | 1 | if (!$force && !$node->hasChildNodes()) { |
|
| 853 | return; |
||
| 854 | 1 | } |
|
| 855 | |||
| 856 | 1 | $tagNameExpr = !empty($special[self::T_TAG]) ? $special[self::T_TAG]->value : ''; |
|
| 857 | 1 | if (!empty($tagNameExpr)) { |
|
| 858 | $tagNameExpr = $this->expr($tagNameExpr, $out, $special[self::T_TAG]); |
||
| 859 | 1 | $out->echoLiteral('</'); |
|
| 860 | $out->echoCode($tagNameExpr); |
||
| 861 | 1 | $out->echoLiteral('>'); |
|
| 862 | 1 | } elseif ($node->tagName !== self::T_X) { |
|
| 863 | 1 | $out->echoLiteral("</{$node->tagName}>"); |
|
| 864 | } |
||
| 865 | 2 | } |
|
| 866 | 2 | ||
| 867 | 2 | protected function isEmptyText(DOMNode $node) { |
|
| 870 | |||
| 871 | 2 | protected function isEmptyNode(DOMNode $node) { |
|
| 872 | 2 | if (!$node->hasChildNodes()) { |
|
| 873 | 2 | return true; |
|
| 874 | } |
||
| 875 | |||
| 876 | 2 | foreach ($node->childNodes as $childNode) { |
|
| 877 | 2 | if ($childNode instanceof DOMElement) { |
|
| 878 | return false; |
||
| 879 | 18 | } |
|
| 880 | 18 | if ($childNode instanceof \DOMText && !$this->isEmptyText($childNode)) { |
|
| 881 | return false; |
||
| 887 | 18 | ||
| 888 | protected function compileIf(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 916 | 2 | ||
| 917 | protected function compileEach(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 949 | 12 | ||
| 950 | 12 | protected function compileWith(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 978 | |||
| 979 | 35 | protected function compileLiteral(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 992 | |||
| 993 | protected function compileElement(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 1002 | 35 | ||
| 1003 | 35 | /** |
|
| 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 | 35 | * @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 | 36 | */ |
|
| 1013 | 36 | 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 | private function compileEachLoop(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
||
| 1087 | |||
| 1088 | 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 | private function canSkip(\DOMNode $node, CompilerBuffer $out) { |
||
| 1145 | |||
| 1146 | protected function rtrim($text, \DOMNode $node, CompilerBuffer $out) { |
||
| 1168 | |||
| 1169 | protected function inPre(\DOMNode $node) { |
||
| 1177 | |||
| 1178 | 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 | 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 | 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 | protected function compileBasicElement(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
||
| 1284 | |||
| 1285 | 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.