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 |
||
| 14 | class Compiler { |
||
| 15 | const T_IF = 'x-if'; |
||
|
|
|||
| 16 | const T_EACH = 'x-each'; |
||
| 17 | const T_WITH = 'x-with'; |
||
| 18 | const T_LITERAL = 'x-literal'; |
||
| 19 | const T_AS = 'x-as'; |
||
| 20 | const T_COMPONENT = 'x-component'; |
||
| 21 | const T_CHILDREN = 'x-children'; |
||
| 22 | const T_BLOCK = 'x-block'; |
||
| 23 | const T_ELSE = 'x-else'; |
||
| 24 | const T_EMPTY = 'x-empty'; |
||
| 25 | const T_X = 'x'; |
||
| 26 | const T_INCLUDE = 'x-include'; |
||
| 27 | |||
| 28 | protected static $special = [ |
||
| 29 | self::T_COMPONENT => 1, |
||
| 30 | self::T_IF => 2, |
||
| 31 | self::T_ELSE => 3, |
||
| 32 | self::T_EACH => 4, |
||
| 33 | self::T_EMPTY => 5, |
||
| 34 | self::T_CHILDREN => 6, |
||
| 35 | self::T_INCLUDE => 7, |
||
| 36 | self::T_WITH => 8, |
||
| 37 | self::T_BLOCK => 9, |
||
| 38 | self::T_LITERAL => 10, |
||
| 39 | self::T_AS => 11, |
||
| 40 | ]; |
||
| 41 | |||
| 42 | protected static $htmlTags = [ |
||
| 43 | 'a' => 'i', |
||
| 44 | 'abbr' => 'i', |
||
| 45 | 'acronym' => 'i', // deprecated |
||
| 46 | 'address' => 'b', |
||
| 47 | // 'applet' => 'i', // deprecated |
||
| 48 | 'area' => 'i', |
||
| 49 | 'article' => 'b', |
||
| 50 | 'aside' => 'b', |
||
| 51 | 'audio' => 'i', |
||
| 52 | 'b' => 'i', |
||
| 53 | 'base' => 'i', |
||
| 54 | // 'basefont' => 'i', |
||
| 55 | 'bdi' => 'i', |
||
| 56 | 'bdo' => 'i', |
||
| 57 | // 'bgsound' => 'i', |
||
| 58 | // 'big' => 'i', |
||
| 59 | 'x' => 'i', |
||
| 60 | // 'blink' => 'i', |
||
| 61 | 'blockquote' => 'b', |
||
| 62 | 'body' => 'b', |
||
| 63 | 'br' => 'i', |
||
| 64 | 'button' => 'i', |
||
| 65 | 'canvas' => 'b', |
||
| 66 | 'caption' => 'i', |
||
| 67 | // 'center' => 'b', |
||
| 68 | 'cite' => 'i', |
||
| 69 | 'code' => 'i', |
||
| 70 | 'col' => 'i', |
||
| 71 | 'colgroup' => 'i', |
||
| 72 | // 'command' => 'i', |
||
| 73 | 'content' => 'i', |
||
| 74 | 'data' => 'i', |
||
| 75 | 'datalist' => 'i', |
||
| 76 | 'dd' => 'b', |
||
| 77 | 'del' => 'i', |
||
| 78 | 'details' => 'i', |
||
| 79 | 'dfn' => 'i', |
||
| 80 | 'dialog' => 'i', |
||
| 81 | // 'dir' => 'i', |
||
| 82 | 'div' => 'i', |
||
| 83 | 'dl' => 'b', |
||
| 84 | 'dt' => 'b', |
||
| 85 | // 'element' => 'i', |
||
| 86 | 'em' => 'i', |
||
| 87 | 'embed' => 'i', |
||
| 88 | 'fieldset' => 'b', |
||
| 89 | 'figcaption' => 'b', |
||
| 90 | 'figure' => 'b', |
||
| 91 | // 'font' => 'i', |
||
| 92 | 'footer' => 'b', |
||
| 93 | 'form' => 'b', |
||
| 94 | 'frame' => 'i', |
||
| 95 | 'frameset' => 'i', |
||
| 96 | 'h1' => 'b', |
||
| 97 | 'h2' => 'b', |
||
| 98 | 'h3' => 'b', |
||
| 99 | 'h4' => 'b', |
||
| 100 | 'h5' => 'b', |
||
| 101 | 'h6' => 'b', |
||
| 102 | 'head' => 'b', |
||
| 103 | 'header' => 'b', |
||
| 104 | 'hgroup' => 'b', |
||
| 105 | 'hr' => 'b', |
||
| 106 | 'html' => 'b', |
||
| 107 | 'i' => 'i', |
||
| 108 | 'iframe' => 'i', |
||
| 109 | 'image' => 'i', |
||
| 110 | 'img' => 'i', |
||
| 111 | 'input' => 'i', |
||
| 112 | 'ins' => 'i', |
||
| 113 | 'isindex' => 'i', |
||
| 114 | 'kbd' => 'i', |
||
| 115 | 'keygen' => 'i', |
||
| 116 | 'label' => 'i', |
||
| 117 | 'legend' => 'i', |
||
| 118 | 'li' => 'i', |
||
| 119 | 'link' => 'i', |
||
| 120 | // 'listing' => 'i', |
||
| 121 | 'main' => 'b', |
||
| 122 | 'map' => 'i', |
||
| 123 | 'mark' => 'i', |
||
| 124 | // 'marquee' => 'i', |
||
| 125 | 'menu' => 'i', |
||
| 126 | 'menuitem' => 'i', |
||
| 127 | 'meta' => 'i', |
||
| 128 | 'meter' => 'i', |
||
| 129 | 'multicol' => 'i', |
||
| 130 | 'nav' => 'b', |
||
| 131 | 'nobr' => 'i', |
||
| 132 | 'noembed' => 'i', |
||
| 133 | 'noframes' => 'i', |
||
| 134 | 'noscript' => 'b', |
||
| 135 | 'object' => 'i', |
||
| 136 | 'ol' => 'b', |
||
| 137 | 'optgroup' => 'i', |
||
| 138 | 'option' => 'b', |
||
| 139 | 'output' => 'i', |
||
| 140 | 'p' => 'b', |
||
| 141 | 'param' => 'i', |
||
| 142 | 'picture' => 'i', |
||
| 143 | // 'plaintext' => 'i', |
||
| 144 | 'pre' => 'b', |
||
| 145 | 'progress' => 'i', |
||
| 146 | 'q' => 'i', |
||
| 147 | 'rp' => 'i', |
||
| 148 | 'rt' => 'i', |
||
| 149 | 'rtc' => 'i', |
||
| 150 | 'ruby' => 'i', |
||
| 151 | 's' => 'i', |
||
| 152 | 'samp' => 'i', |
||
| 153 | 'script' => 'i', |
||
| 154 | 'section' => 'b', |
||
| 155 | 'select' => 'i', |
||
| 156 | // 'shadow' => 'i', |
||
| 157 | 'slot' => 'i', |
||
| 158 | 'small' => 'i', |
||
| 159 | 'source' => 'i', |
||
| 160 | // 'spacer' => 'i', |
||
| 161 | 'span' => 'i', |
||
| 162 | // 'strike' => 'i', |
||
| 163 | 'strong' => 'i', |
||
| 164 | 'style' => 'i', |
||
| 165 | 'sub' => 'i', |
||
| 166 | 'summary' => 'i', |
||
| 167 | 'sup' => 'i', |
||
| 168 | 'table' => 'b', |
||
| 169 | 'tbody' => 'i', |
||
| 170 | 'td' => 'i', |
||
| 171 | 'template' => 'i', |
||
| 172 | 'textarea' => 'i', |
||
| 173 | 'tfoot' => 'b', |
||
| 174 | 'th' => 'i', |
||
| 175 | 'thead' => 'i', |
||
| 176 | 'time' => 'i', |
||
| 177 | 'title' => 'i', |
||
| 178 | 'tr' => 'i', |
||
| 179 | 'track' => 'i', |
||
| 180 | // 'tt' => 'i', |
||
| 181 | 'u' => 'i', |
||
| 182 | 'ul' => 'b', |
||
| 183 | 'var' => 'i', |
||
| 184 | 'video' => 'b', |
||
| 185 | 'wbr' => 'i', |
||
| 186 | |||
| 187 | /// SVG /// |
||
| 188 | 'animate' => 's', |
||
| 189 | 'animateColor' => 's', |
||
| 190 | 'animateMotion' => 's', |
||
| 191 | 'animateTransform' => 's', |
||
| 192 | // 'canvas' => 's', |
||
| 193 | 'circle' => 's', |
||
| 194 | 'desc' => 's', |
||
| 195 | 'defs' => 's', |
||
| 196 | 'discard' => 's', |
||
| 197 | 'ellipse' => 's', |
||
| 198 | 'g' => 's', |
||
| 199 | // 'image' => 's', |
||
| 200 | 'line' => 's', |
||
| 201 | 'marker' => 's', |
||
| 202 | 'mask' => 's', |
||
| 203 | 'missing-glyph' => 's', |
||
| 204 | 'mpath' => 's', |
||
| 205 | 'metadata' => 's', |
||
| 206 | 'path' => 's', |
||
| 207 | 'pattern' => 's', |
||
| 208 | 'polygon' => 's', |
||
| 209 | 'polyline' => 's', |
||
| 210 | 'rect' => 's', |
||
| 211 | 'set' => 's', |
||
| 212 | 'svg' => 's', |
||
| 213 | 'switch' => 's', |
||
| 214 | 'symbol' => 's', |
||
| 215 | 'text' => 's', |
||
| 216 | // 'unknown' => 's', |
||
| 217 | 'use' => 's', |
||
| 218 | ]; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var ExpressionLanguage |
||
| 222 | */ |
||
| 223 | protected $expressions; |
||
| 224 | |||
| 225 | 46 | public function __construct() { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Register a runtime function. |
||
| 232 | * |
||
| 233 | * @param string $name The name of the function. |
||
| 234 | * @param callable $function The function callback. |
||
| 235 | */ |
||
| 236 | 44 | public function defineFunction($name, $function = null) { |
|
| 237 | 44 | if ($function === null) { |
|
| 238 | 1 | $function = $name; |
|
| 239 | 1 | } |
|
| 240 | |||
| 241 | 44 | $this->expressions->register( |
|
| 242 | 44 | $name, |
|
| 243 | 44 | $this->getFunctionCompiler($name, $function), |
|
| 244 | 44 | $this->getFunctionEvaluator($function) |
|
| 245 | 44 | ); |
|
| 246 | 44 | } |
|
| 247 | |||
| 248 | 44 | private function getFunctionEvaluator($function) { |
|
| 249 | 44 | if ($function === 'empty') { |
|
| 250 | return function ($expr) { |
||
| 251 | return empty($expr); |
||
| 252 | 44 | }; |
|
| 253 | 43 | } elseif ($function === 'isset') { |
|
| 254 | return function ($expr) { |
||
| 255 | return isset($expr); |
||
| 256 | }; |
||
| 257 | } |
||
| 258 | |||
| 259 | 43 | return $function; |
|
| 260 | } |
||
| 261 | |||
| 262 | 44 | private function getFunctionCompiler($name, $function) { |
|
| 263 | 44 | $var = var_export(strtolower($name), true); |
|
| 264 | $fn = function ($expr) use ($var) { |
||
| 265 | return "\$this->call($var, $expr)"; |
||
| 266 | 44 | }; |
|
| 267 | |||
| 268 | 44 | if (is_string($function)) { |
|
| 269 | $fn = function ($expr) use ($function) { |
||
| 270 | 7 | return "$function($expr)"; |
|
| 271 | 44 | }; |
|
| 272 | 44 | } elseif (is_array($function)) { |
|
| 273 | 43 | if (is_string($function[0])) { |
|
| 274 | $fn = function ($expr) use ($function) { |
||
| 275 | return "$function[0]::$function[1]($expr)"; |
||
| 276 | }; |
||
| 277 | 43 | } elseif ($function[0] instanceof Ebi) { |
|
| 278 | $fn = function ($expr) use ($function) { |
||
| 279 | 5 | return "\$this->$function[1]($expr)"; |
|
| 280 | 43 | }; |
|
| 281 | 43 | } |
|
| 282 | 43 | } |
|
| 283 | |||
| 284 | 44 | return $fn; |
|
| 285 | } |
||
| 286 | |||
| 287 | 38 | public function compile($src, array $options = []) { |
|
| 288 | 38 | $options += ['basename' => '', 'runtime' => true]; |
|
| 289 | |||
| 290 | 38 | $src = trim($src); |
|
| 291 | |||
| 292 | 38 | $dom = new \DOMDocument(); |
|
| 293 | 38 | libxml_use_internal_errors(true); |
|
| 294 | |||
| 295 | 38 | $fragment = false; |
|
| 296 | 38 | if (strpos($src, '<html') === false) { |
|
| 297 | 37 | $src = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><body>$src</body></html>"; |
|
| 298 | 37 | $fragment = true; |
|
| 299 | 37 | } |
|
| 300 | |||
| 301 | 38 | $dom->loadHTML($src, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOCDATA | LIBXML_NOXMLDECL); |
|
| 302 | // $arr = $this->domToArray($dom); |
||
| 303 | |||
| 304 | 38 | $out = new CompilerBuffer(); |
|
| 305 | |||
| 306 | 38 | $out->setBasename($options['basename']); |
|
| 307 | |||
| 308 | 38 | if ($options['runtime']) { |
|
| 309 | 37 | $name = var_export($options['basename'], true); |
|
| 310 | 37 | $out->appendCode("\$this->defineComponent($name, function (\$props = [], \$children = []) {\n"); |
|
| 311 | 37 | } else { |
|
| 312 | 1 | $out->appendCode("function (\$props = [], \$children = []) {\n"); |
|
| 313 | } |
||
| 314 | |||
| 315 | 38 | $out->pushScope(['this' => 'props']); |
|
| 316 | 38 | $out->indent(+1); |
|
| 317 | |||
| 318 | 38 | $parent = $fragment ? $dom->firstChild->nextSibling->firstChild : $dom; |
|
| 319 | |||
| 320 | 38 | foreach ($parent->childNodes as $node) { |
|
| 321 | 38 | $this->compileNode($node, $out); |
|
| 322 | 38 | } |
|
| 323 | |||
| 324 | 38 | $out->indent(-1); |
|
| 325 | 38 | $out->popScope(); |
|
| 326 | |||
| 327 | 38 | if ($options['runtime']) { |
|
| 328 | 37 | $out->appendCode("});"); |
|
| 329 | 37 | } else { |
|
| 330 | 1 | $out->appendCode("};"); |
|
| 331 | } |
||
| 332 | |||
| 333 | 38 | $r = $out->flush(); |
|
| 334 | 38 | return $r; |
|
| 335 | } |
||
| 336 | |||
| 337 | 33 | protected function isComponent($tag) { |
|
| 340 | |||
| 341 | 38 | protected function compileNode(DOMNode $node, CompilerBuffer $out) { |
|
| 342 | 38 | if ($out->getNodeProp($node, 'skip')) { |
|
| 343 | 4 | return; |
|
| 344 | } |
||
| 345 | |||
| 346 | 38 | switch ($node->nodeType) { |
|
| 347 | 38 | case XML_TEXT_NODE: |
|
| 348 | 36 | $this->compileTextNode($node, $out); |
|
| 349 | 36 | break; |
|
| 350 | 35 | case XML_ELEMENT_NODE: |
|
| 351 | /* @var \DOMElement $node */ |
||
| 352 | 35 | $this->compileElementNode($node, $out); |
|
| 353 | 35 | break; |
|
| 354 | 2 | case XML_COMMENT_NODE: |
|
| 355 | /* @var \DOMComment $node */ |
||
| 356 | 1 | $this->compileCommentNode($node, $out); |
|
| 357 | 1 | break; |
|
| 358 | 1 | case XML_DOCUMENT_TYPE_NODE: |
|
| 359 | 1 | $out->echoLiteral("<!DOCTYPE {$node->name}>\n"); |
|
| 360 | 1 | break; |
|
| 361 | default: |
||
| 362 | $r = "// Unknown node\n". |
||
| 363 | '// '.str_replace("\n", "\n// ", $node->ownerDocument->saveHTML($node)); |
||
| 364 | 38 | } |
|
| 365 | 38 | } |
|
| 366 | |||
| 367 | protected function domToArray(DOMNode $root) { |
||
| 368 | $result = []; |
||
| 369 | |||
| 370 | if ($root->hasAttributes()) { |
||
| 371 | $attrs = $root->attributes; |
||
| 372 | foreach ($attrs as $attr) { |
||
| 373 | $result['@attributes'][$attr->name] = $attr->value; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | if ($root->hasChildNodes()) { |
||
| 378 | $children = $root->childNodes; |
||
| 379 | if ($children->length == 1) { |
||
| 380 | $child = $children->item(0); |
||
| 381 | if ($child->nodeType == XML_TEXT_NODE) { |
||
| 382 | $result['_value'] = $child->nodeValue; |
||
| 383 | return count($result) == 1 |
||
| 384 | ? $result['_value'] |
||
| 385 | : $result; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | $groups = []; |
||
| 389 | foreach ($children as $child) { |
||
| 390 | if (!isset($result[$child->nodeName])) { |
||
| 391 | $result[$child->nodeName] = $this->domToArray($child); |
||
| 392 | } else { |
||
| 393 | if (!isset($groups[$child->nodeName])) { |
||
| 394 | $result[$child->nodeName] = [$result[$child->nodeName]]; |
||
| 395 | $groups[$child->nodeName] = 1; |
||
| 396 | } |
||
| 397 | $result[$child->nodeName][] = $this->domToArray($child); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | return $result; |
||
| 403 | } |
||
| 404 | |||
| 405 | 1 | protected function newline(DOMNode $node, CompilerBuffer $out) { |
|
| 406 | 1 | if ($node->previousSibling && $node->previousSibling->nodeType !== XML_COMMENT_NODE) { |
|
| 407 | $out->appendCode("\n"); |
||
| 408 | } |
||
| 409 | 1 | } |
|
| 410 | |||
| 411 | 1 | protected function compileCommentNode(\DOMComment $node, CompilerBuffer $out) { |
|
| 412 | 1 | $comments = explode("\n", trim($node->nodeValue)); |
|
| 413 | |||
| 414 | 1 | $this->newline($node, $out); |
|
| 415 | 1 | foreach ($comments as $comment) { |
|
| 416 | 1 | $out->appendCode("// $comment\n"); |
|
| 417 | 1 | } |
|
| 418 | 1 | } |
|
| 419 | |||
| 420 | 36 | protected function compileTextNode(DOMNode $node, CompilerBuffer $out) { |
|
| 421 | 36 | $text = $this->ltrim($this->rtrim($node->nodeValue, $node, $out), $node, $out); |
|
| 422 | |||
| 423 | 36 | $items = $this->splitExpressions($text); |
|
| 424 | |||
| 425 | 36 | foreach ($items as $i => list($text, $offset)) { |
|
| 426 | 36 | if (preg_match('`^{\S`', $text)) { |
|
| 427 | 22 | if (preg_match('`^{\s*unescape\((.+)\)\s*}$`', $text, $m)) { |
|
| 428 | 1 | $out->echoCode($this->expr($m[1], $out)); |
|
| 429 | 1 | } else { |
|
| 430 | 21 | $out->echoCode('htmlspecialchars('.$this->expr(substr($text, 1, -1), $out).')'); |
|
| 431 | } |
||
| 432 | 22 | } else { |
|
| 433 | // if ($i === 0) { |
||
| 434 | // $text = $this->ltrim($text, $node, $out); |
||
| 435 | // } |
||
| 436 | // if ($i === count($items) - 1) { |
||
| 437 | // $text = $this->rtrim($text, $node, $out); |
||
| 438 | // } |
||
| 439 | |||
| 440 | 36 | $out->echoLiteral($text); |
|
| 441 | } |
||
| 442 | 36 | } |
|
| 443 | 36 | } |
|
| 444 | |||
| 445 | 35 | protected function compileElementNode(DOMElement $node, CompilerBuffer $out) { |
|
| 446 | 35 | list($attributes, $special) = $this->splitAttributes($node); |
|
| 447 | |||
| 448 | 35 | if (!empty($special) || $this->isComponent($node->tagName)) { |
|
| 449 | 30 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 450 | 30 | } else { |
|
| 451 | 17 | $this->compileOpenTag($node, $node->attributes, $out); |
|
| 452 | |||
| 453 | 17 | foreach ($node->childNodes as $childNode) { |
|
| 454 | 17 | $this->compileNode($childNode, $out); |
|
| 455 | 17 | } |
|
| 456 | |||
| 457 | 17 | $this->compileCloseTag($node, $out); |
|
| 458 | } |
||
| 459 | 35 | } |
|
| 460 | |||
| 461 | 36 | protected function splitExpressions($value) { |
|
| 465 | |||
| 466 | 34 | protected function expr($expr, CompilerBuffer $output, DOMAttr $attr = null) { |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get the compiler function to wrap an attribute. |
||
| 488 | * |
||
| 489 | * Attribute functions are regular expression functions, but with a special naming convention. The following naming |
||
| 490 | * conventions are supported: |
||
| 491 | * |
||
| 492 | * - **@tag:attribute**: Applies to an attribute only on a specific tag. |
||
| 493 | * - **@attribute**: Applies to all attributes with a given name. |
||
| 494 | * |
||
| 495 | * @param DOMAttr $attr The attribute to look at. |
||
| 496 | * @return callable|null A function or **null** if the attribute doesn't have a function. |
||
| 497 | */ |
||
| 498 | 10 | private function getAttributeFunction(DOMAttr $attr) { |
|
| 499 | 10 | $keys = ['@'.$attr->ownerElement->tagName.':'.$attr->name, '@'.$attr->name]; |
|
| 500 | |||
| 501 | 10 | foreach ($keys as $key) { |
|
| 502 | 10 | if (null !== $fn = $this->expressions->getFunctionCompiler($key)) { |
|
| 503 | 4 | return $fn; |
|
| 504 | } |
||
| 505 | 10 | } |
|
| 506 | 6 | } |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @param DOMElement $node |
||
| 510 | */ |
||
| 511 | 35 | protected function splitAttributes(DOMElement $node) { |
|
| 512 | 35 | $attributes = []; |
|
| 513 | 35 | $special = []; |
|
| 514 | |||
| 515 | 35 | foreach ($node->attributes as $name => $attribute) { |
|
| 516 | 34 | if (isset(static::$special[$name])) { |
|
| 517 | 29 | $special[$name] = $attribute; |
|
| 518 | 29 | } else { |
|
| 519 | 11 | $attributes[$name] = $attribute; |
|
| 520 | } |
||
| 521 | 35 | } |
|
| 522 | |||
| 523 | 35 | uksort($special, function ($a, $b) { |
|
| 524 | 7 | return strnatcmp(static::$special[$a], static::$special[$b]); |
|
| 525 | 35 | }); |
|
| 526 | |||
| 527 | 35 | return [$attributes, $special]; |
|
| 528 | } |
||
| 529 | |||
| 530 | 30 | protected function compileSpecialNode(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 531 | 30 | $specialName = key($special); |
|
| 532 | |||
| 533 | switch ($specialName) { |
||
| 534 | 30 | case self::T_COMPONENT: |
|
| 535 | 8 | $this->compileComponentRegister($node, $attributes, $special, $out); |
|
| 536 | 8 | break; |
|
| 537 | 30 | case self::T_IF: |
|
| 538 | 7 | $this->compileIf($node, $attributes, $special, $out); |
|
| 539 | 7 | break; |
|
| 540 | 30 | case self::T_EACH: |
|
| 541 | 12 | $this->compileEach($node, $attributes, $special, $out); |
|
| 542 | 12 | break; |
|
| 543 | 21 | case self::T_BLOCK: |
|
| 544 | 1 | $this->compileBlock($node, $attributes, $special, $out); |
|
| 545 | 1 | break; |
|
| 546 | 21 | case self::T_CHILDREN: |
|
| 547 | 2 | $this->compileChildBlock($node, $attributes, $special, $out); |
|
| 548 | 2 | break; |
|
| 549 | 21 | case self::T_INCLUDE: |
|
| 550 | 1 | $this->compileComponentInclude($node, $attributes, $special, $out); |
|
| 551 | 1 | break; |
|
| 552 | 21 | case self::T_WITH: |
|
| 553 | 3 | if ($this->isComponent($node->tagName)) { |
|
| 554 | // With has a special meaning in components. |
||
| 555 | 2 | $this->compileComponentInclude($node, $attributes, $special, $out); |
|
| 556 | 2 | } else { |
|
| 557 | 1 | $this->compileWith($node, $attributes, $special, $out); |
|
| 558 | } |
||
| 559 | 3 | break; |
|
| 560 | 21 | case self::T_LITERAL: |
|
| 561 | 2 | $this->compileLiteral($node, $attributes, $special, $out); |
|
| 562 | 2 | break; |
|
| 563 | 19 | case '': |
|
| 564 | 19 | if ($this->isComponent($node->tagName)) { |
|
| 565 | 6 | $this->compileComponentInclude($node, $attributes, $special, $out); |
|
| 566 | 6 | } else { |
|
| 567 | 18 | $this->compileElement($node, $attributes, $out); |
|
| 568 | } |
||
| 569 | 19 | break; |
|
| 570 | } |
||
| 571 | 30 | } |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Compile component registering. |
||
| 575 | * |
||
| 576 | * @param DOMElement $node |
||
| 577 | * @param $attributes |
||
| 578 | * @param $special |
||
| 579 | * @param CompilerBuffer $out |
||
| 580 | */ |
||
| 581 | 8 | public function compileComponentRegister(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
| 582 | 8 | $name = strtolower($special[self::T_COMPONENT]->value); |
|
| 583 | 8 | unset($special[self::T_COMPONENT]); |
|
| 584 | |||
| 585 | 8 | $prev = $out->select($name); |
|
| 586 | |||
| 587 | 8 | $varName = var_export($name, true); |
|
| 588 | 8 | $out->appendCode("\$this->defineComponent($varName, function (\$props = [], \$children = []) {\n"); |
|
| 589 | 8 | $out->pushScope(['this' => 'props']); |
|
| 590 | 8 | $out->indent(+1); |
|
| 591 | |||
| 592 | try { |
||
| 593 | 8 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 594 | 8 | } finally { |
|
| 595 | 8 | $out->popScope(); |
|
| 596 | 8 | $out->indent(-1); |
|
| 597 | 8 | $out->appendCode("});"); |
|
| 598 | 8 | $out->select($prev); |
|
| 599 | 8 | } |
|
| 600 | 8 | } |
|
| 601 | |||
| 602 | 1 | private function compileBlock(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 603 | 1 | $name = strtolower($special[self::T_BLOCK]->value); |
|
| 604 | 1 | unset($special[self::T_BLOCK]); |
|
| 605 | |||
| 606 | 1 | $prev = $out->select($name); |
|
| 607 | |||
| 608 | 1 | $use = '$'.implode(', $', $out->getScopeVariables()).', $children'; |
|
| 609 | |||
| 610 | 1 | $out->appendCode("function () use ($use) {\n"); |
|
| 611 | 1 | $out->pushScope(['this' => 'props']); |
|
| 612 | 1 | $out->indent(+1); |
|
| 613 | |||
| 614 | try { |
||
| 615 | 1 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 616 | 1 | } finally { |
|
| 617 | 1 | $out->indent(-1); |
|
| 618 | 1 | $out->popScope(); |
|
| 619 | 1 | $out->appendCode("}"); |
|
| 620 | 1 | $out->select($prev); |
|
| 621 | 1 | } |
|
| 622 | |||
| 623 | 1 | return $out; |
|
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Compile component inclusion and rendering. |
||
| 628 | * |
||
| 629 | * @param DOMElement $node |
||
| 630 | * @param DOMAttr[] $attributes |
||
| 631 | * @param DOMAttr[] $special |
||
| 632 | * @param CompilerBuffer $out |
||
| 633 | */ |
||
| 634 | 9 | protected function compileComponentInclude(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 635 | // Generate the attributes into a property array. |
||
| 636 | 9 | $props = []; |
|
| 637 | 9 | foreach ($attributes as $name => $attribute) { |
|
| 638 | /* @var DOMAttr $attr */ |
||
| 639 | 5 | if ($this->isExpression($attribute->value)) { |
|
| 640 | 4 | $expr = $this->expr(substr($attribute->value, 1, -1), $out, $attribute); |
|
| 641 | 4 | } else { |
|
| 642 | 1 | $expr = var_export($attribute->value, true); |
|
| 643 | } |
||
| 644 | |||
| 645 | 5 | $props[] = var_export($name, true).' => '.$expr; |
|
| 646 | 9 | } |
|
| 647 | 9 | $propsStr = '['.implode(', ', $props).']'; |
|
| 648 | |||
| 649 | 9 | if (isset($special[self::T_WITH])) { |
|
| 650 | 2 | $withExpr = $this->expr($special[self::T_WITH]->value, $out, $special[self::T_WITH]); |
|
| 651 | 2 | unset($special[self::T_WITH]); |
|
| 652 | |||
| 653 | 2 | $propsStr = empty($props) ? $withExpr : $propsStr.' + (array)'.$withExpr; |
|
| 654 | 9 | } elseif (empty($props)) { |
|
| 655 | // By default the current context is passed to components. |
||
| 656 | 3 | $propsStr = $this->expr('this', $out); |
|
| 657 | 3 | } |
|
| 658 | |||
| 659 | // Compile the children blocks. |
||
| 660 | 9 | $blocks = $this->compileComponentBlocks($node, $out); |
|
| 661 | 9 | $blocksStr = $blocks->flush(); |
|
| 662 | |||
| 663 | 9 | if (isset($special[self::T_INCLUDE])) { |
|
| 664 | 1 | $name = $this->expr($special[self::T_INCLUDE]->value, $out, $special[self::T_INCLUDE]); |
|
| 665 | 1 | } else { |
|
| 666 | 8 | $name = var_export($node->tagName, true); |
|
| 667 | } |
||
| 668 | |||
| 669 | 9 | $out->appendCode("\$this->write($name, $propsStr, $blocksStr);\n"); |
|
| 670 | 9 | } |
|
| 671 | |||
| 672 | /** |
||
| 673 | * @param DOMElement $parent |
||
| 674 | * @return CompilerBuffer |
||
| 675 | */ |
||
| 676 | 9 | protected function compileComponentBlocks(DOMElement $parent, CompilerBuffer $out) { |
|
| 677 | 9 | $blocksOut = new CompilerBuffer(CompilerBuffer::STYLE_ARRAY, [ |
|
| 678 | 9 | 'baseIndent' => $out->getIndent(), |
|
| 679 | 9 | 'indent' => $out->getIndent() + 1, |
|
| 680 | 9 | 'depth' => $out->getDepth(), |
|
| 681 | 9 | 'scopes' => $out->getAllScopes() |
|
| 682 | 9 | ]); |
|
| 683 | |||
| 684 | 9 | if ($this->isEmptyNode($parent)) { |
|
| 685 | 7 | return $blocksOut; |
|
| 686 | } |
||
| 687 | |||
| 688 | 2 | $use = '$'.implode(', $', $blocksOut->getScopeVariables()).', $children'; |
|
| 689 | |||
| 690 | 2 | $blocksOut->appendCode("function () use ($use) {\n"); |
|
| 691 | 2 | $blocksOut->indent(+1); |
|
| 692 | |||
| 693 | try { |
||
| 694 | 2 | foreach ($parent->childNodes as $node) { |
|
| 695 | 2 | $this->compileNode($node, $blocksOut); |
|
| 696 | 2 | } |
|
| 697 | 2 | } finally { |
|
| 698 | 2 | $blocksOut->indent(-1); |
|
| 699 | 2 | $blocksOut->appendCode("}"); |
|
| 700 | 2 | } |
|
| 701 | |||
| 702 | 2 | return $blocksOut; |
|
| 703 | } |
||
| 704 | |||
| 705 | 22 | protected function compileTagComment(DOMElement $node, $attributes, $special, CompilerBuffer $out) { |
|
| 706 | // Don't double up comments. |
||
| 707 | 22 | if ($node->previousSibling && $node->previousSibling->nodeType === XML_COMMENT_NODE) { |
|
| 708 | return; |
||
| 709 | } |
||
| 710 | |||
| 711 | 22 | $str = '<'.$node->tagName; |
|
| 712 | 22 | foreach ($special as $attr) { |
|
| 713 | /* @var DOMAttr $attr */ |
||
| 714 | 22 | $str .= ' '.$attr->name.(empty($attr->value) ? '' : '="'.htmlspecialchars($attr->value).'"'); |
|
| 715 | 22 | } |
|
| 716 | 22 | $str .= '>'; |
|
| 717 | 22 | $comments = explode("\n", $str); |
|
| 718 | 22 | foreach ($comments as $comment) { |
|
| 719 | 22 | $out->appendCode("// $comment\n"); |
|
| 720 | 22 | } |
|
| 721 | 22 | } |
|
| 722 | |||
| 723 | 35 | protected function compileOpenTag(DOMElement $node, $attributes, CompilerBuffer $out, $force = false) { |
|
| 724 | 35 | if ($node->tagName === self::T_X) { |
|
| 725 | 3 | return; |
|
| 726 | } |
||
| 727 | |||
| 728 | 34 | $out->echoLiteral('<'.$node->tagName); |
|
| 729 | |||
| 730 | 34 | foreach ($attributes as $name => $attribute) { |
|
| 731 | /* @var DOMAttr $attribute */ |
||
| 732 | 8 | $out->echoLiteral(' '.$name.'="'); |
|
| 733 | |||
| 734 | // Check for an attribute expression. |
||
| 735 | 8 | if ($this->isExpression($attribute->value)) { |
|
| 736 | 6 | $out->echoCode('htmlspecialchars('.$this->expr(substr($attribute->value, 1, -1), $out, $attribute).')'); |
|
| 737 | 6 | } else { |
|
| 738 | 2 | $out->echoLiteral(htmlspecialchars($attribute->value)); |
|
| 739 | } |
||
| 740 | |||
| 741 | 8 | $out->echoLiteral('"'); |
|
| 742 | 34 | } |
|
| 743 | |||
| 744 | 34 | if ($node->hasChildNodes() || $force) { |
|
| 745 | 34 | $out->echoLiteral('>'); |
|
| 746 | 34 | } else { |
|
| 747 | 1 | $out->echoLiteral(" />"); |
|
| 748 | } |
||
| 749 | 34 | } |
|
| 750 | |||
| 751 | 11 | private function isExpression($value) { |
|
| 754 | |||
| 755 | 35 | protected function compileCloseTag(DOMElement $node, CompilerBuffer $out, $force = false) { |
|
| 756 | 35 | if (($force || $node->hasChildNodes()) && $node->tagName !== self::T_X) { |
|
| 757 | 34 | $out->echoLiteral("</{$node->tagName}>"); |
|
| 758 | 34 | } |
|
| 759 | 35 | } |
|
| 760 | |||
| 761 | 3 | protected function isEmptyText(DOMNode $node) { |
|
| 764 | |||
| 765 | 9 | protected function isEmptyNode(DOMNode $node) { |
|
| 766 | 9 | if (!$node->hasChildNodes()) { |
|
| 767 | 7 | return true; |
|
| 768 | } |
||
| 769 | |||
| 770 | 2 | foreach ($node->childNodes as $childNode) { |
|
| 771 | 2 | if ($childNode instanceof DOMElement) { |
|
| 772 | 1 | return false; |
|
| 773 | } |
||
| 774 | 1 | if ($childNode instanceof \DOMText && !$this->isEmptyText($childNode)) { |
|
| 775 | 1 | return false; |
|
| 776 | } |
||
| 777 | } |
||
| 778 | |||
| 779 | return true; |
||
| 780 | } |
||
| 781 | |||
| 782 | 7 | protected function compileIf(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 783 | 7 | $this->compileTagComment($node, $attributes, $special, $out); |
|
| 784 | 7 | $expr = $this->expr($special[self::T_IF]->value, $out); |
|
| 785 | 7 | unset($special[self::T_IF]); |
|
| 786 | |||
| 787 | 7 | $elseNode = $this->findSpecialNode($node, self::T_ELSE, self::T_IF); |
|
| 788 | 7 | $out->setNodeProp($elseNode, 'skip', true); |
|
| 789 | |||
| 790 | 7 | $out->appendCode('if ('.$expr.") {\n"); |
|
| 791 | 7 | $out->indent(+1); |
|
| 792 | |||
| 793 | 7 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 794 | |||
| 795 | 7 | $out->indent(-1); |
|
| 796 | |||
| 797 | 7 | if ($elseNode) { |
|
| 798 | 2 | list($attributes, $special) = $this->splitAttributes($elseNode); |
|
| 799 | 2 | unset($special[self::T_ELSE]); |
|
| 800 | |||
| 801 | 2 | $out->appendCode("} else {\n"); |
|
| 802 | |||
| 803 | 2 | $out->indent(+1); |
|
| 804 | 2 | $this->compileSpecialNode($elseNode, $attributes, $special, $out); |
|
| 805 | 2 | $out->indent(-1); |
|
| 806 | 2 | } |
|
| 807 | |||
| 808 | 7 | $out->appendCode("}\n"); |
|
| 809 | 7 | } |
|
| 810 | |||
| 811 | 12 | protected function compileEach(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 812 | 12 | $this->compileTagComment($node, $attributes, $special, $out); |
|
| 813 | 12 | $this->compileOpenTag($node, $attributes, $out); |
|
| 814 | |||
| 815 | 12 | $emptyNode = $this->findSpecialNode($node, self::T_EMPTY, self::T_ELSE); |
|
| 816 | 12 | $out->setNodeProp($emptyNode, 'skip', true); |
|
| 817 | |||
| 818 | 12 | if ($emptyNode === null) { |
|
| 819 | 10 | $this->compileEachLoop($node, $attributes, $special, $out); |
|
| 820 | 10 | } else { |
|
| 821 | 2 | $expr = $this->expr("empty({$special[self::T_EACH]->value})", $out); |
|
| 822 | |||
| 823 | 2 | list ($emptyAttributes, $emptySpecial) = $this->splitAttributes($emptyNode); |
|
| 824 | 2 | unset($emptySpecial[self::T_EMPTY]); |
|
| 825 | |||
| 826 | 2 | $out->appendCode('if ('.$expr.") {\n"); |
|
| 827 | |||
| 828 | 2 | $out->indent(+1); |
|
| 829 | 2 | $this->compileSpecialNode($emptyNode, $emptyAttributes, $emptySpecial, $out); |
|
| 830 | 2 | $out->indent(-1); |
|
| 831 | |||
| 832 | 2 | $out->appendCode("} else {\n"); |
|
| 833 | |||
| 834 | 2 | $out->indent(+1); |
|
| 835 | 2 | $this->compileEachLoop($node, $attributes, $special, $out); |
|
| 836 | 2 | $out->indent(-1); |
|
| 837 | |||
| 838 | 2 | $out->appendCode("}\n"); |
|
| 839 | } |
||
| 840 | |||
| 841 | 12 | $this->compileCloseTag($node, $out); |
|
| 842 | 12 | } |
|
| 843 | |||
| 844 | 1 | protected function compileWith(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 845 | 1 | $this->compileTagComment($node, $attributes, $special, $out); |
|
| 846 | 1 | $with = $this->expr($special[self::T_WITH]->value, $out); |
|
| 847 | |||
| 848 | 1 | $out->depth(+1); |
|
| 849 | 1 | $scope = ['this' => $out->depthName('props')]; |
|
| 850 | 1 | if (!empty($special[self::T_AS]) && preg_match('`^([a-z0-9]+)$`', $special[self::T_AS]->value, $m)) { |
|
| 851 | // The template specified an x-as attribute to alias the with expression. |
||
| 852 | 1 | $scope = [$m[1] => $out->depthName('props')]; |
|
| 853 | 1 | } |
|
| 854 | 1 | unset($special[self::T_WITH], $special[self::T_AS]); |
|
| 855 | |||
| 856 | 1 | $out->pushScope($scope); |
|
| 857 | 1 | $out->appendCode('$'.$out->depthName('props')." = $with;\n"); |
|
| 858 | |||
| 859 | 1 | $this->compileSpecialNode($node, $attributes, $special, $out); |
|
| 860 | |||
| 861 | 1 | $out->depth(-1); |
|
| 862 | 1 | $out->popScope(); |
|
| 863 | 1 | } |
|
| 864 | |||
| 865 | 2 | protected function compileLiteral(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 866 | 2 | $this->compileTagComment($node, $attributes, $special, $out); |
|
| 867 | 2 | unset($special[self::T_LITERAL]); |
|
| 868 | |||
| 869 | 2 | $this->compileOpenTag($node, $attributes, $out); |
|
| 870 | |||
| 871 | 2 | foreach ($node->childNodes as $childNode) { |
|
| 872 | 2 | $html = $childNode->ownerDocument->saveHTML($childNode); |
|
| 873 | 2 | $out->echoLiteral($html); |
|
| 874 | 2 | } |
|
| 875 | |||
| 876 | 2 | $this->compileCloseTag($node, $out); |
|
| 877 | 2 | } |
|
| 878 | |||
| 879 | 18 | protected function compileElement(DOMElement $node, array $attributes, CompilerBuffer $out) { |
|
| 880 | 18 | $this->compileOpenTag($node, $attributes, $out); |
|
| 881 | |||
| 882 | 18 | foreach ($node->childNodes as $childNode) { |
|
| 883 | 18 | $this->compileNode($childNode, $out); |
|
| 884 | 18 | } |
|
| 885 | |||
| 886 | 18 | $this->compileCloseTag($node, $out); |
|
| 887 | 18 | } |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Find a special node in relation to another node. |
||
| 891 | * |
||
| 892 | * This method is used to find things such as x-empty and x-else elements. |
||
| 893 | * |
||
| 894 | * @param DOMElement $node The node to search in relation to. |
||
| 895 | * @param string $attribute The name of the attribute to search for. |
||
| 896 | * @param string $parentAttribute The name of the parent attribute to resolve conflicts. |
||
| 897 | * @return DOMElement|null Returns the found element node or **null** if not found. |
||
| 898 | */ |
||
| 899 | 19 | protected function findSpecialNode(DOMElement $node, $attribute, $parentAttribute) { |
|
| 900 | // First look for a sibling after the node. |
||
| 901 | 19 | for ($sibNode = $node->nextSibling; $sibNode !== null; $sibNode = $sibNode->nextSibling) { |
|
| 902 | 2 | if ($sibNode instanceof DOMElement && $sibNode->hasAttribute($attribute)) { |
|
| 903 | 2 | return $sibNode; |
|
| 904 | } |
||
| 905 | |||
| 906 | // Stop searching if we encounter another node. |
||
| 907 | 2 | if (!$this->isEmptyText($sibNode)) { |
|
| 908 | break; |
||
| 909 | } |
||
| 910 | 2 | } |
|
| 911 | |||
| 912 | // Next look inside the node. |
||
| 913 | 17 | $parentFound = false; |
|
| 914 | 17 | foreach ($node->childNodes as $childNode) { |
|
| 915 | 17 | if (!$parentFound && $childNode instanceof DOMElement && $childNode->hasAttribute($attribute)) { |
|
| 916 | 2 | return $childNode; |
|
| 917 | } |
||
| 918 | |||
| 919 | 17 | if ($childNode instanceof DOMElement) { |
|
| 920 | 12 | $parentFound = $childNode->hasAttribute($parentAttribute); |
|
| 921 | 17 | } elseif ($childNode instanceof \DOMText && !empty(trim($childNode->data))) { |
|
| 922 | 5 | $parentFound = false; |
|
| 923 | 5 | } |
|
| 924 | 17 | } |
|
| 925 | |||
| 926 | 15 | return null; |
|
| 927 | } |
||
| 928 | |||
| 929 | /** |
||
| 930 | * @param DOMElement $node |
||
| 931 | * @param array $attributes |
||
| 932 | * @param array $special |
||
| 933 | * @param CompilerBuffer $out |
||
| 934 | */ |
||
| 935 | 12 | private function compileEachLoop(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 936 | 12 | $each = $this->expr($special[self::T_EACH]->value, $out); |
|
| 937 | 12 | unset($special[self::T_EACH]); |
|
| 938 | |||
| 939 | 12 | $as = ['', $out->depthName('props', 1)]; |
|
| 940 | 12 | $scope = ['this' => $as[1]]; |
|
| 941 | 12 | if (!empty($special[self::T_AS])) { |
|
| 942 | 6 | if (preg_match('`(?:([a-z0-9]+)\s+)?([a-z0-9]+)`', $special[self::T_AS]->value, $m)) { |
|
| 943 | 6 | $scope = [$m[2] => $as[1]]; |
|
| 944 | 6 | if (!empty($m[1])) { |
|
| 945 | 3 | $scope[$m[1]] = $as[0] = $out->depthName('i', 1); |
|
| 946 | 3 | } |
|
| 947 | 6 | } |
|
| 948 | 6 | } |
|
| 949 | 12 | unset($special[self::T_AS]); |
|
| 950 | 12 | if (empty($as[0])) { |
|
| 951 | 9 | $out->appendCode("foreach ($each as \${$as[1]}) {\n"); |
|
| 952 | 9 | } else { |
|
| 953 | 3 | $out->appendCode("foreach ($each as \${$as[0]} => \${$as[1]}) {\n"); |
|
| 954 | } |
||
| 955 | 12 | $out->depth(+1); |
|
| 956 | 12 | $out->indent(+1); |
|
| 957 | 12 | $out->pushScope($scope); |
|
| 958 | |||
| 959 | 12 | foreach ($node->childNodes as $childNode) { |
|
| 960 | 12 | $this->compileNode($childNode, $out); |
|
| 961 | 12 | } |
|
| 962 | |||
| 963 | 12 | $out->indent(-1); |
|
| 964 | 12 | $out->depth(-1); |
|
| 965 | 12 | $out->popScope(); |
|
| 966 | 12 | $out->appendCode("}\n"); |
|
| 967 | 12 | } |
|
| 968 | |||
| 969 | 36 | protected function ltrim($text, \DOMNode $node, CompilerBuffer $out) { |
|
| 970 | 36 | if ($this->inPre($node)) { |
|
| 971 | return $text; |
||
| 972 | } |
||
| 973 | |||
| 974 | 36 | $sib = $node->previousSibling ?: $node->parentNode; |
|
| 975 | 36 | if ($sib === null || !$sib instanceof \DOMElement || $out->getNodeProp($sib, 'skip') || $sib->tagName === self::T_X) { |
|
| 976 | 6 | return ltrim($text); |
|
| 977 | } |
||
| 978 | |||
| 979 | 35 | $text = preg_replace('`^\s*\n\s*`', "\n", $text, -1, $count); |
|
| 980 | 35 | if ($count === 0) { |
|
| 981 | 35 | $text = preg_replace('`^\s+`', ' ', $text); |
|
| 982 | 35 | } |
|
| 983 | |||
| 984 | // if ($sib !== null && ($sib->nodeType === XML_COMMENT_NODE || in_array($sib->tagName, static::$blocks))) { |
||
| 985 | // return ltrim($text); |
||
| 986 | // } |
||
| 987 | 35 | return $text; |
|
| 988 | } |
||
| 989 | |||
| 990 | 36 | protected function rtrim($text, \DOMNode $node, CompilerBuffer $out) { |
|
| 991 | 36 | if ($this->inPre($node)) { |
|
| 992 | return $text; |
||
| 993 | } |
||
| 994 | |||
| 995 | 36 | $sib = $node->nextSibling ?: $node->parentNode; |
|
| 996 | |||
| 997 | 36 | if ($sib === null || !$sib instanceof \DOMElement || $out->getNodeProp($sib, 'skip') || $sib->tagName === self::T_X) { |
|
| 998 | 5 | return rtrim($text); |
|
| 999 | } |
||
| 1000 | |||
| 1001 | 35 | $text = preg_replace('`\s*\n\s*$`', "\n", $text, -1, $count); |
|
| 1002 | 35 | if ($count === 0) { |
|
| 1003 | 35 | $text = preg_replace('`\s+$`', ' ', $text); |
|
| 1004 | 35 | } |
|
| 1005 | |||
| 1006 | // if ($sib !== null && ($sib->nodeType === XML_COMMENT_NODE || in_array($sib->tagName, static::$blocks))) { |
||
| 1007 | // return rtrim($text); |
||
| 1008 | // } |
||
| 1009 | 35 | return $text; |
|
| 1010 | } |
||
| 1011 | |||
| 1012 | 36 | protected function inPre(\DOMNode $node) { |
|
| 1013 | 36 | for ($node = $node->parentNode; $node !== null; $node = $node->parentNode) { |
|
| 1014 | 36 | if (in_array($node->nodeType, ['code', 'pre'], true)) { |
|
| 1015 | return true; |
||
| 1016 | } |
||
| 1017 | 36 | } |
|
| 1018 | 36 | return false; |
|
| 1019 | } |
||
| 1020 | |||
| 1021 | 2 | private function compileChildBlock(DOMElement $node, array $attributes, array $special, CompilerBuffer $out) { |
|
| 1039 | } |
||
| 1040 |
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.