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 |
||
| 31 | class Compiler extends Validator |
||
| 32 | { |
||
| 33 | public static $lastParsed; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Compile template into PHP code |
||
| 37 | * |
||
| 38 | * @param array<string,array|string|integer> $context Current context |
||
| 39 | * @param string $template handlebars template |
||
| 40 | * |
||
| 41 | * @return string|null generated PHP code |
||
| 42 | */ |
||
| 43 | 717 | public static function compileTemplate(&$context, $template) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Compose LightnCandy render codes for include() |
||
| 80 | * |
||
| 81 | * @param array<string,array|string|integer> $context Current context |
||
| 82 | * @param string $code generated PHP code |
||
| 83 | * |
||
| 84 | * @return string Composed PHP code |
||
| 85 | */ |
||
| 86 | 647 | public static function composePHPRender($context, $code) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get function name for standalone or none standalone template. |
||
| 137 | * |
||
| 138 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
| 139 | * @param string $name base function name |
||
| 140 | * @param string $tag original handlabars tag for debug |
||
| 141 | * |
||
| 142 | * @return string compiled Function name |
||
| 143 | * |
||
| 144 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
| 145 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
| 146 | * @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'funcprefix' => 'lala_abc'), 'test3', '' |
||
| 147 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'funcprefix' => 'haha456'), 'test', 'abc' |
||
| 148 | */ |
||
| 149 | 583 | protected static function getFuncName(&$context, $name, $tag) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get string presentation of variables |
||
| 165 | * |
||
| 166 | * @param array<string,array|string|integer> $context current compile context |
||
| 167 | * @param array<array> $vn variable name array. |
||
| 168 | * @param array<string>|null $blockParams block param list |
||
| 169 | * |
||
| 170 | * @return array<string|array> variable names |
||
| 171 | * |
||
| 172 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
| 173 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
| 174 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
| 175 | */ |
||
| 176 | 256 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get string presentation of a sub expression |
||
| 194 | * |
||
| 195 | * @param array<string,array|string|integer> $context current compile context |
||
| 196 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 197 | * |
||
| 198 | * @return array<string> code representing passed expression |
||
| 199 | */ |
||
| 200 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get string presentation of a subexpression or a variable |
||
| 212 | * |
||
| 213 | * @param array<array|string|integer> $context current compile context |
||
| 214 | * @param array<array|string|integer> $var variable parsed path |
||
| 215 | * |
||
| 216 | * @return array<string> variable names |
||
|
|
|||
| 217 | */ |
||
| 218 | 364 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get string presentation of a variable |
||
| 224 | * |
||
| 225 | * @param array<array|string|integer> $var variable parsed path |
||
| 226 | * @param array<array|string|integer> $context current compile context |
||
| 227 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
| 228 | * |
||
| 229 | * @return array<string> variable names |
||
| 230 | * |
||
| 231 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
| 232 | * @expect array('((isset($in[\'true\']) && is_array($in)) ? $in[\'true\'] : null)', '[true]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('true') |
||
| 233 | * @expect array('((isset($in[\'false\']) && is_array($in)) ? $in[\'false\'] : null)', '[false]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('false') |
||
| 234 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
| 235 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
| 236 | * @expect array('((isset($in[\'2\']) && is_array($in)) ? $in[\'2\'] : null)', '[2]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('2') |
||
| 237 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
| 238 | * @expect array('((isset($in[\'@index\']) && is_array($in)) ? $in[\'@index\'] : null)', '[@index]') when input array('flags'=>array('spvar'=>false,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@index') |
||
| 239 | * @expect array("((isset(\$cx['sp_vars']['index']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['index'] : null)", '@[index]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@index') |
||
| 240 | * @expect array("((isset(\$cx['sp_vars']['key']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['key'] : null)", '@[key]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@key') |
||
| 241 | * @expect array("((isset(\$cx['sp_vars']['first']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['first'] : null)", '@[first]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@first') |
||
| 242 | * @expect array("((isset(\$cx['sp_vars']['last']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['last'] : null)", '@[last]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@last') |
||
| 243 | * @expect array('((isset($in[\'"a"\']) && is_array($in)) ? $in[\'"a"\'] : null)', '["a"]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('"a"') |
||
| 244 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
| 245 | * @expect array('((isset($in[\'a\']) && is_array($in)) ? $in[\'a\'] : null)', '[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('a') |
||
| 246 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\']) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-1])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'] : null)', '../[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(1,'a') |
||
| 247 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\']) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-3])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'] : null)', '../../../[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(3,'a') |
||
| 248 | * @expect array('((isset($in[\'id\']) && is_array($in)) ? $in[\'id\'] : null)', 'this.[id]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(null, 'id') |
||
| 249 | * @expect array('LR::v($cx, $in, isset($in) ? $in : null, array(\'id\'))', 'this.[id]') when input array('flags'=>array('prop'=>true,'spvar'=>true,'debug'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0,'standalone'=>0), 'runtime' => 'Runtime'), array(null, 'id') |
||
| 250 | */ |
||
| 251 | 579 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Return compiled PHP code for a handlebars token |
||
| 299 | * |
||
| 300 | * @param array<string,array|string|integer> $context current compile context |
||
| 301 | * @param array<string,array|boolean> $info parsed information |
||
| 302 | * |
||
| 303 | * @return string Return compiled code segment for the token |
||
| 304 | */ |
||
| 305 | 607 | protected static function compileToken(&$context, $info) { |
|
| 337 | |||
| 338 | /** |
||
| 339 | * handle partial |
||
| 340 | * |
||
| 341 | * @param array<string,array|string|integer> $context current compile context |
||
| 342 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 343 | * |
||
| 344 | * @return string Return compiled code segment for the partial |
||
| 345 | */ |
||
| 346 | 86 | public static function partial(&$context, $vars) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * handle inline partial |
||
| 372 | * |
||
| 373 | * @param array<string,array|string|integer> $context current compile context |
||
| 374 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 375 | * |
||
| 376 | * @return string Return compiled code segment for the partial |
||
| 377 | */ |
||
| 378 | 10 | public static function inline(&$context, $vars) { |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Return compiled PHP code for a handlebars inverted section begin token |
||
| 392 | * |
||
| 393 | * @param array<string,array|string|integer> $context current compile context |
||
| 394 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 395 | * |
||
| 396 | * @return string Return compiled code segment for the token |
||
| 397 | */ |
||
| 398 | 38 | protected static function invertedSection(&$context, $vars) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
| 405 | * |
||
| 406 | * @param array<string,array|string|integer> $context current compile context |
||
| 407 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 408 | * @param boolean $inverted the logic will be inverted |
||
| 409 | * |
||
| 410 | * @return string Return compiled code segment for the token |
||
| 411 | */ |
||
| 412 | 58 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Return compiled PHP code for a handlebars block end token |
||
| 424 | * |
||
| 425 | * @param array<string,array|string|integer> $context current compile context |
||
| 426 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 427 | * @param string|null $matchop should also match to this operator |
||
| 428 | * |
||
| 429 | * @return string Return compiled code segment for the token |
||
| 430 | */ |
||
| 431 | 316 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Return compiled PHP code for a handlebars block begin token |
||
| 470 | * |
||
| 471 | * @param array<string,array|string|integer> $context current compile context |
||
| 472 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 473 | * |
||
| 474 | * @return string Return compiled code segment for the token |
||
| 475 | */ |
||
| 476 | 235 | protected static function blockBegin(&$context, $vars) { |
|
| 496 | |||
| 497 | /** |
||
| 498 | * compile {{#foo}} token |
||
| 499 | * |
||
| 500 | * @param array<string,array|string|integer> $context current compile context |
||
| 501 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 502 | * @param boolean $isEach the section is #each |
||
| 503 | * |
||
| 504 | * @return string|null Return compiled code segment for the token |
||
| 505 | */ |
||
| 506 | 162 | protected static function section(&$context, $vars, $isEach = false) { |
|
| 527 | |||
| 528 | /** |
||
| 529 | * compile {{with}} token |
||
| 530 | * |
||
| 531 | * @param array<string,array|string|integer> $context current compile context |
||
| 532 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 533 | * |
||
| 534 | * @return string|null Return compiled code segment for the token |
||
| 535 | */ |
||
| 536 | 27 | protected static function with(&$context, $vars) { |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Return compiled PHP code for a handlebars custom helper token |
||
| 546 | * |
||
| 547 | * @param array<string,array|string|integer> $context current compile context |
||
| 548 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 549 | * @param boolean $raw is this {{{ token or not |
||
| 550 | * @param boolean $nosep true to compile without seperator |
||
| 551 | * |
||
| 552 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
| 553 | */ |
||
| 554 | 448 | protected static function customHelper(&$context, $vars, $raw, $nosep) { |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Return compiled PHP code for a handlebars else token |
||
| 570 | * |
||
| 571 | * @param array<string,array|string|integer> $context current compile context |
||
| 572 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 573 | * |
||
| 574 | * @return string Return compiled code segment for the token when the token is else |
||
| 575 | */ |
||
| 576 | 47 | protected static function doElse(&$context, $vars) { |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Return compiled PHP code for a handlebars log token |
||
| 589 | * |
||
| 590 | * @param array<string,array|string|integer> $context current compile context |
||
| 591 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 592 | * @param boolean $raw is this {{{ token or not |
||
| 593 | * |
||
| 594 | * @return string Return compiled code segment for the token |
||
| 595 | */ |
||
| 596 | 1 | protected static function compileLog(&$context, &$vars, $raw) { |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Return compiled PHP code for a handlebars lookup token |
||
| 604 | * |
||
| 605 | * @param array<string,array|string|integer> $context current compile context |
||
| 606 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 607 | * @param boolean $raw is this {{{ token or not |
||
| 608 | * |
||
| 609 | * @return string Return compiled code segment for the token |
||
| 610 | */ |
||
| 611 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Return compiled PHP code for template output |
||
| 623 | * |
||
| 624 | * @param array<string,array|string|integer> $context current compile context |
||
| 625 | * @param string $variable PHP code for the variable |
||
| 626 | * @param string $expression normalized handlebars expression |
||
| 627 | * @param boolean $raw is this {{{ token or not |
||
| 628 | * @param boolean $nosep true to compile without seperator |
||
| 629 | * |
||
| 630 | * @return string Return compiled code segment for the token |
||
| 631 | */ |
||
| 632 | 459 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) { |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Return compiled PHP code for a handlebars variable token |
||
| 643 | * |
||
| 644 | * @param array<string,array|string|integer> $context current compile context |
||
| 645 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 646 | * @param boolean $raw is this {{{ token or not |
||
| 647 | * @param boolean $nosep true to compile without seperator |
||
| 648 | * |
||
| 649 | * @return string Return compiled code segment for the token |
||
| 650 | */ |
||
| 651 | 359 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) { |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Add usage count to context |
||
| 663 | * |
||
| 664 | * @param array<string,array|string|integer> $context current context |
||
| 665 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
| 666 | * @param string $name used name |
||
| 667 | * @param integer $count increment |
||
| 668 | * |
||
| 669 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
| 670 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
| 671 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
| 672 | */ |
||
| 673 | 583 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
| 679 | } |
||
| 680 | |||
| 681 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.