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 | 702 | 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 | 633 | public static function composePHPRender($context, $code) { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get function name for standalone or none standalone template. |
||
| 143 | * |
||
| 144 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
| 145 | * @param string $name base function name |
||
| 146 | * @param string $tag original handlabars tag for debug |
||
| 147 | * |
||
| 148 | * @return string compiled Function name |
||
| 149 | * |
||
| 150 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
| 151 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
| 152 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
| 153 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
| 154 | */ |
||
| 155 | 569 | protected static function getFuncName(&$context, $name, $tag) { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get string presentation of variables |
||
| 171 | * |
||
| 172 | * @param array<string,array|string|integer> $context current compile context |
||
| 173 | * @param array<array> $vn variable name array. |
||
| 174 | * @param array<string>|null $blockParams block param list |
||
| 175 | * |
||
| 176 | * @return array<string|array> variable names |
||
| 177 | * |
||
| 178 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
| 179 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
| 180 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
| 181 | */ |
||
| 182 | 246 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Get string presentation of a sub expression |
||
| 200 | * |
||
| 201 | * @param array<string,array|string|integer> $context current compile context |
||
| 202 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 203 | * |
||
| 204 | * @return array<string> code representing passed expression |
||
| 205 | */ |
||
| 206 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
| 207 | 41 | $origSeperator = $context['ops']['seperator']; |
|
| 208 | 41 | $context['ops']['seperator'] = ''; |
|
| 209 | |||
| 210 | 41 | $ret = static::customHelper($context, $vars, true); |
|
| 211 | |||
| 212 | 41 | if (($ret === null) && $context['flags']['lambda']) { |
|
| 213 | 4 | $ret = static::compileVariable($context, $vars, true); |
|
| 214 | } |
||
| 215 | |||
| 216 | 41 | $context['ops']['seperator'] = $origSeperator; |
|
| 217 | |||
| 218 | 41 | return array($ret ? $ret : '', 'FIXME: $subExpression'); |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get string presentation of a subexpression or a variable |
||
| 223 | * |
||
| 224 | * @param array<array|string|integer> $context current compile context |
||
| 225 | * @param array<array|string|integer> $var variable parsed path |
||
| 226 | * |
||
| 227 | * @return array<string> variable names |
||
|
|
|||
| 228 | */ |
||
| 229 | 348 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
| 230 | 348 | return Parser::isSubExp($var) ? static::compileSubExpression($context, $var[1]) : static::getVariableName($context, $var); |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get string presentation of a variable |
||
| 235 | * |
||
| 236 | * @param array<array|string|integer> $var variable parsed path |
||
| 237 | * @param array<array|string|integer> $context current compile context |
||
| 238 | * @param array<string> $lookup extra lookup string as valid PHP variable name |
||
| 239 | * |
||
| 240 | * @return array<string> variable names |
||
| 241 | * |
||
| 242 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
| 243 | * @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') |
||
| 244 | * @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') |
||
| 245 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
| 246 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
| 247 | * @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') |
||
| 248 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
| 249 | * @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') |
||
| 250 | * @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') |
||
| 251 | * @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') |
||
| 252 | * @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') |
||
| 253 | * @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') |
||
| 254 | * @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"') |
||
| 255 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
| 256 | * @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') |
||
| 257 | * @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') |
||
| 258 | * @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') |
||
| 259 | * @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') |
||
| 260 | * @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') |
||
| 261 | */ |
||
| 262 | 564 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Return compiled PHP code for a handlebars token |
||
| 310 | * |
||
| 311 | * @param array<string,array|string|integer> $context current compile context |
||
| 312 | * @param array<string,array|boolean> $info parsed information |
||
| 313 | * |
||
| 314 | * @return string Return compiled code segment for the token |
||
| 315 | */ |
||
| 316 | 593 | protected static function compileToken(&$context, $info) { |
|
| 345 | |||
| 346 | /** |
||
| 347 | * handle partial |
||
| 348 | * |
||
| 349 | * @param array<string,array|string|integer> $context current compile context |
||
| 350 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 351 | * |
||
| 352 | * @return string Return compiled code segment for the partial |
||
| 353 | */ |
||
| 354 | 73 | public static function partial(&$context, $vars) { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * handle inline partial |
||
| 380 | * |
||
| 381 | * @param array<string,array|string|integer> $context current compile context |
||
| 382 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 383 | * |
||
| 384 | * @return string Return compiled code segment for the partial |
||
| 385 | */ |
||
| 386 | 38 | public static function inline(&$context, $vars) { |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Return compiled PHP code for a handlebars inverted section begin token |
||
| 400 | 61 | * |
|
| 401 | 61 | * @param array<string,array|string|integer> $context current compile context |
|
| 402 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 403 | 61 | * |
|
| 404 | 61 | * @return string Return compiled code segment for the token |
|
| 405 | 61 | */ |
|
| 406 | 61 | protected static function invertedSection(&$context, $vars) { |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
| 413 | * |
||
| 414 | * @param array<string,array|string|integer> $context current compile context |
||
| 415 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 416 | * @param boolean $inverted the logic will be inverted |
||
| 417 | * |
||
| 418 | * @return string Return compiled code segment for the token |
||
| 419 | */ |
||
| 420 | 313 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
| 431 | |||
| 432 | 2 | /** |
|
| 433 | 236 | * Return compiled PHP code for a handlebars block end token |
|
| 434 | 29 | * |
|
| 435 | 28 | * @param array<string,array|string|integer> $context current compile context |
|
| 436 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 437 | * |
||
| 438 | * @return string Return compiled code segment for the token |
||
| 439 | 239 | */ |
|
| 440 | protected static function blockEnd(&$context, $vars) { |
||
| 471 | 74 | ||
| 472 | 24 | /** |
|
| 473 | 24 | * Return compiled PHP code for a handlebars block begin token |
|
| 474 | * |
||
| 475 | * @param array<string,array|string|integer> $context current compile context |
||
| 476 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 477 | * |
||
| 478 | 112 | * @return string Return compiled code segment for the token |
|
| 479 | */ |
||
| 480 | protected static function blockBegin(&$context, $vars) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | 159 | * compile {{#foo}} token |
|
| 503 | 66 | * |
|
| 504 | 66 | * @param array<string,array|string|integer> $context current compile context |
|
| 505 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 506 | 93 | * @param boolean $isEach the section is #each |
|
| 507 | * |
||
| 508 | 159 | * @return string|null Return compiled code segment for the token |
|
| 509 | 159 | */ |
|
| 510 | protected static function section(&$context, $vars, $isEach = false) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * compile {{with}} token |
||
| 534 | * |
||
| 535 | * @param array<string,array|string|integer> $context current compile context |
||
| 536 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 537 | 445 | * |
|
| 538 | 445 | * @return string|null Return compiled code segment for the token |
|
| 539 | 445 | */ |
|
| 540 | 335 | protected static function with(&$context, $vars) { |
|
| 547 | 123 | ||
| 548 | /** |
||
| 549 | * Return compiled PHP code for a handlebars custom helper token |
||
| 550 | * |
||
| 551 | * @param array<string,array|string|integer> $context current compile context |
||
| 552 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 553 | * @param boolean $raw is this {{{ token or not |
||
| 554 | * |
||
| 555 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
| 556 | */ |
||
| 557 | 46 | protected static function customHelper(&$context, $vars, $raw) { |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Return compiled PHP code for a handlebars else token |
||
| 572 | * |
||
| 573 | * @param array<string,array|string|integer> $context current compile context |
||
| 574 | * |
||
| 575 | * @return string Return compiled code segment for the token when the token is else |
||
| 576 | */ |
||
| 577 | 2 | protected static function doElse(&$context) { |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Return compiled PHP code for a handlebars lookup token |
||
| 590 | * |
||
| 591 | * @param array<string,array|string|integer> $context current compile context |
||
| 592 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 593 | * @param boolean $raw is this {{{ token or not |
||
| 594 | * |
||
| 595 | * @return string Return compiled code segment for the token |
||
| 596 | 357 | */ |
|
| 597 | 357 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
| 606 | 35 | ||
| 607 | /** |
||
| 608 | * Return compiled PHP code for a handlebars variable token |
||
| 609 | * |
||
| 610 | * @param array<string,array|string|integer> $context current compile context |
||
| 611 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 612 | * @param boolean $raw is this {{{ token or not |
||
| 613 | * |
||
| 614 | * @return string Return compiled code segment for the token |
||
| 615 | */ |
||
| 616 | protected static function compileVariable(&$context, &$vars, $raw) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Add usage count to context |
||
| 632 | * |
||
| 633 | * @param array<string,array|string|integer> $context current context |
||
| 634 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
| 635 | * @param string $name used name |
||
| 636 | * @param integer $count increment |
||
| 637 | * |
||
| 638 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
| 639 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
| 640 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
| 641 | */ |
||
| 642 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
||
| 648 | } |
||
| 649 | |||
| 650 |
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.