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 | 679 | public static function compileTemplate(&$context, $template) { |
|
| 44 | 679 | array_unshift($context['parsed'], array()); |
|
| 45 | 679 | Validator::verify($context, $template); |
|
| 46 | |||
| 47 | 679 | if (count($context['error'])) { |
|
| 48 | 68 | return; |
|
| 49 | } |
||
| 50 | |||
| 51 | // Do PHP code generation. |
||
| 52 | 612 | Parser::setDelimiter($context); |
|
| 53 | |||
| 54 | // Handle dynamic partials |
||
| 55 | 612 | Partial::handleDynamicPartial($context); |
|
| 56 | |||
| 57 | 612 | $code = ''; |
|
| 58 | 612 | foreach ($context['parsed'][0] as $info) { |
|
| 59 | 612 | if (is_array($info)) { |
|
| 60 | 571 | $context['tokens']['current']++; |
|
| 61 | 571 | $tmpl = static::compileToken($context, $info); |
|
| 62 | 571 | if ($tmpl == $context['ops']['seperator']) { |
|
| 63 | 1 | $tmpl = ''; |
|
| 64 | } else { |
||
| 65 | 570 | $tmpl = "'$tmpl'"; |
|
| 66 | } |
||
| 67 | 571 | $code .= $tmpl; |
|
| 68 | } else { |
||
| 69 | 612 | $code .= $info; |
|
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | 612 | static::$lastParsed = array_shift($context['parsed']); |
|
| 74 | |||
| 75 | 612 | return $code; |
|
| 76 | } |
||
| 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 | 611 | public static function composePHPRender($context, $code) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get function name for standalone or none standalone template. |
||
| 141 | * |
||
| 142 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
| 143 | * @param string $name base function name |
||
| 144 | * @param string $tag original handlabars tag for debug |
||
| 145 | * |
||
| 146 | * @return string compiled Function name |
||
| 147 | * |
||
| 148 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
| 149 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
| 150 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
| 151 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
| 152 | */ |
||
| 153 | 547 | protected static function getFuncName(&$context, $name, $tag) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get string presentation of variables |
||
| 169 | * |
||
| 170 | * @param array<string,array|string|integer> $context current compile context |
||
| 171 | * @param array<array> $vn variable name array. |
||
| 172 | * @param array<string>|null $blockParams block param list |
||
| 173 | * |
||
| 174 | * @return array<string|array> variable names |
||
| 175 | * |
||
| 176 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
| 177 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
| 178 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
| 179 | */ |
||
| 180 | 231 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Get string presentation of a sub expression |
||
| 198 | * |
||
| 199 | * @param array<string,array|string|integer> $context current compile context |
||
| 200 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 201 | * |
||
| 202 | * @return array<string> code representing passed expression |
||
| 203 | */ |
||
| 204 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
| 205 | 41 | $origSeperator = $context['ops']['seperator']; |
|
| 206 | 41 | $context['ops']['seperator'] = ''; |
|
| 207 | |||
| 208 | 41 | $ret = static::customHelper($context, $vars, true); |
|
| 209 | |||
| 210 | 41 | if (($ret === null) && $context['flags']['lambda']) { |
|
| 211 | 4 | $ret = static::compileVariable($context, $vars, true); |
|
| 212 | } |
||
| 213 | |||
| 214 | 41 | $context['ops']['seperator'] = $origSeperator; |
|
| 215 | |||
| 216 | 41 | return array($ret ? $ret : '', 'FIXME: $subExpression'); |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get string presentation of a subexpression or a variable |
||
| 221 | * |
||
| 222 | * @param array<array|string|integer> $context current compile context |
||
| 223 | * @param array<array|string|integer> $var variable parsed path |
||
| 224 | * |
||
| 225 | * @return array<string> variable names |
||
|
|
|||
| 226 | */ |
||
| 227 | 333 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Get string presentation of a variable |
||
| 233 | * |
||
| 234 | * @param array<array|string|integer> $var variable parsed path |
||
| 235 | * @param array<array|string|integer> $context current compile context |
||
| 236 | * @param array<string> $lookup extra lookup string as valid PHP variable name |
||
| 237 | * |
||
| 238 | * @return array<string> variable names |
||
| 239 | * |
||
| 240 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
| 241 | * @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') |
||
| 242 | * @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') |
||
| 243 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(0, 'true') |
||
| 244 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(0, 'false') |
||
| 245 | * @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') |
||
| 246 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(0, '2') |
||
| 247 | * @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') |
||
| 248 | * @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') |
||
| 249 | * @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') |
||
| 250 | * @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') |
||
| 251 | * @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') |
||
| 252 | * @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"') |
||
| 253 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(0, '"a"') |
||
| 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('((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') |
||
| 256 | * @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') |
||
| 257 | * @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') |
||
| 258 | * @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') |
||
| 259 | */ |
||
| 260 | 543 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Return compiled PHP code for a handlebars token |
||
| 308 | * |
||
| 309 | * @param array<string,array|string|integer> $context current compile context |
||
| 310 | * @param array<string,array|boolean> $info parsed information |
||
| 311 | * |
||
| 312 | * @return string Return compiled code segment for the token |
||
| 313 | */ |
||
| 314 | 571 | protected static function compileToken(&$context, $info) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * handle partial |
||
| 346 | * |
||
| 347 | * @param array<string,array|string|integer> $context current compile context |
||
| 348 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 349 | * |
||
| 350 | * @return string Return compiled code segment for the partial |
||
| 351 | */ |
||
| 352 | 64 | public static function partial(&$context, $vars) { |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Return compiled PHP code for a handlebars inverted section begin token |
||
| 378 | * |
||
| 379 | * @param array<string,array|string|integer> $context current compile context |
||
| 380 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 381 | * |
||
| 382 | * @return string Return compiled code segment for the token |
||
| 383 | */ |
||
| 384 | 37 | protected static function invertedSection(&$context, $vars) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
| 391 | * |
||
| 392 | * @param array<string,array|string|integer> $context current compile context |
||
| 393 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 394 | * @param boolean $inverted the logic will be inverted |
||
| 395 | * |
||
| 396 | * @return string Return compiled code segment for the token |
||
| 397 | */ |
||
| 398 | 60 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Return compiled PHP code for a handlebars block end token |
||
| 412 | * |
||
| 413 | * @param array<string,array|string|integer> $context current compile context |
||
| 414 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 415 | * |
||
| 416 | * @return string Return compiled code segment for the token |
||
| 417 | */ |
||
| 418 | 305 | protected static function blockEnd(&$context, $vars) { |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Return compiled PHP code for a handlebars block begin token |
||
| 452 | * |
||
| 453 | * @param array<string,array|string|integer> $context current compile context |
||
| 454 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 455 | * |
||
| 456 | * @return string Return compiled code segment for the token |
||
| 457 | */ |
||
| 458 | 223 | protected static function blockBegin(&$context, $vars) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * compile {{#foo}} token |
||
| 481 | * |
||
| 482 | * @param array<string,array|string|integer> $context current compile context |
||
| 483 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 484 | * @param boolean $isEach the section is #each |
||
| 485 | * |
||
| 486 | * @return string|null Return compiled code segment for the token |
||
| 487 | */ |
||
| 488 | 158 | protected static function section(&$context, $vars, $isEach = false) { |
|
| 509 | |||
| 510 | /** |
||
| 511 | * compile {{with}} token |
||
| 512 | * |
||
| 513 | * @param array<string,array|string|integer> $context current compile context |
||
| 514 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 515 | * |
||
| 516 | * @return string|null Return compiled code segment for the token |
||
| 517 | */ |
||
| 518 | 20 | protected static function with(&$context, $vars) { |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Return compiled PHP code for a handlebars custom helper token |
||
| 528 | * |
||
| 529 | * @param array<string,array|string|integer> $context current compile context |
||
| 530 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 531 | * @param boolean $raw is this {{{ token or not |
||
| 532 | * |
||
| 533 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
| 534 | */ |
||
| 535 | 429 | protected static function customHelper(&$context, $vars, $raw) { |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Return compiled PHP code for a handlebars else token |
||
| 550 | * |
||
| 551 | * @param array<string,array|string|integer> $context current compile context |
||
| 552 | * |
||
| 553 | * @return string Return compiled code segment for the token when the token is else |
||
| 554 | */ |
||
| 555 | 46 | protected static function doElse(&$context) { |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Return compiled PHP code for a handlebars lookup token |
||
| 568 | * |
||
| 569 | * @param array<string,array|string|integer> $context current compile context |
||
| 570 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 571 | * @param boolean $raw is this {{{ token or not |
||
| 572 | * |
||
| 573 | * @return string Return compiled code segment for the token |
||
| 574 | */ |
||
| 575 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Return compiled PHP code for a handlebars variable token |
||
| 587 | * |
||
| 588 | * @param array<string,array|string|integer> $context current compile context |
||
| 589 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 590 | * @param boolean $raw is this {{{ token or not |
||
| 591 | * |
||
| 592 | * @return string Return compiled code segment for the token |
||
| 593 | */ |
||
| 594 | 343 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Add usage count to context |
||
| 610 | * |
||
| 611 | * @param array<string,array|string|integer> $context current context |
||
| 612 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
| 613 | * @param string $name used name |
||
| 614 | * @param integer $count increment |
||
| 615 | * |
||
| 616 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
| 617 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
| 618 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
| 619 | */ |
||
| 620 | 547 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
| 626 | } |
||
| 627 | |||
| 628 |
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.