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 | 744 | public static function compileTemplate(&$context, $template) { |
|
74 | |||
75 | /** |
||
76 | * Compose LightnCandy render codes for include() |
||
77 | * |
||
78 | * @param array<string,array|string|integer> $context Current context |
||
79 | * @param string $code generated PHP code |
||
80 | * |
||
81 | * @return string Composed PHP code |
||
82 | */ |
||
83 | 667 | public static function composePHPRender($context, $code) { |
|
139 | |||
140 | /** |
||
141 | * Get function name for standalone or none standalone template. |
||
142 | * |
||
143 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
144 | * @param string $name base function name |
||
145 | * @param string $tag original handlabars tag for debug |
||
146 | * |
||
147 | * @return string compiled Function name |
||
148 | * |
||
149 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), 'test', '' |
||
150 | * @expect 'LL::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', '' |
||
151 | * @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 0, 'funcprefix' => 'lala_abc'), 'test3', '' |
||
152 | * @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc' |
||
153 | */ |
||
154 | 602 | protected static function getFuncName(&$context, $name, $tag) { |
|
167 | |||
168 | /** |
||
169 | * Get string presentation of variables |
||
170 | * |
||
171 | * @param array<string,array|string|integer> $context current compile context |
||
172 | * @param array<array> $vn variable name array. |
||
173 | * @param array<string>|null $blockParams block param list |
||
174 | * |
||
175 | * @return array<string|array> variable names |
||
176 | * |
||
177 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
178 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
179 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
180 | */ |
||
181 | 270 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
196 | |||
197 | /** |
||
198 | * Get string presentation of a sub expression |
||
199 | * |
||
200 | * @param array<string,array|string|integer> $context current compile context |
||
201 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
202 | * |
||
203 | * @return array<string> code representing passed expression |
||
204 | */ |
||
205 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
214 | |||
215 | /** |
||
216 | * Get string presentation of a subexpression or a variable |
||
217 | * |
||
218 | * @param array<array|string|integer> $context current compile context |
||
219 | * @param array<array|string|integer> $var variable parsed path |
||
220 | * |
||
221 | * @return array<string> variable names |
||
|
|||
222 | */ |
||
223 | 378 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
226 | |||
227 | /** |
||
228 | * Get string presentation of a variable |
||
229 | * |
||
230 | * @param array<array|string|integer> $var variable parsed path |
||
231 | * @param array<array|string|integer> $context current compile context |
||
232 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
233 | * |
||
234 | * @return array<string> variable names |
||
235 | * |
||
236 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
237 | * @expect array('((is_array($in) && isset($in[\'true\'])) ? $in[\'true\'] : null)', '[true]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('true') |
||
238 | * @expect array('((is_array($in) && isset($in[\'false\'])) ? $in[\'false\'] : null)', '[false]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('false') |
||
239 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
240 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
241 | * @expect array('((is_array($in) && isset($in[\'2\'])) ? $in[\'2\'] : null)', '[2]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('2') |
||
242 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
243 | * @expect array('((is_array($in) && isset($in[\'@index\'])) ? $in[\'@index\'] : null)', '[@index]') when input array('flags'=>array('spvar'=>false,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('@index') |
||
244 | * @expect array("(isset(\$cx['sp_vars']['index']) ? \$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,'jslen'=>0)), array('@index') |
||
245 | * @expect array("(isset(\$cx['sp_vars']['key']) ? \$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,'jslen'=>0)), array('@key') |
||
246 | * @expect array("(isset(\$cx['sp_vars']['first']) ? \$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,'jslen'=>0)), array('@first') |
||
247 | * @expect array("(isset(\$cx['sp_vars']['last']) ? \$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,'jslen'=>0)), array('@last') |
||
248 | * @expect array('((is_array($in) && isset($in[\'"a"\'])) ? $in[\'"a"\'] : null)', '["a"]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('"a"') |
||
249 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
250 | * @expect array('((is_array($in) && isset($in[\'a\'])) ? $in[\'a\'] : null)', '[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('a') |
||
251 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'])) ? $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,'jslen'=>0)), array(1,'a') |
||
252 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'])) ? $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,'jslen'=>0)), array(3,'a') |
||
253 | * @expect array('((is_array($in) && isset($in[\'id\'])) ? $in[\'id\'] : null)', 'this.[id]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array(null, 'id') |
||
254 | * @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,'jslen'=>0,'standalone'=>0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), array(null, 'id') |
||
255 | */ |
||
256 | 595 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
323 | |||
324 | /** |
||
325 | * Return compiled PHP code for a handlebars token |
||
326 | * |
||
327 | * @param array<string,array|string|integer> $context current compile context |
||
328 | * @param array<string,array|boolean> $info parsed information |
||
329 | * |
||
330 | * @return string Return compiled code segment for the token |
||
331 | */ |
||
332 | 627 | protected static function compileToken(&$context, $info) { |
|
359 | |||
360 | /** |
||
361 | * handle partial |
||
362 | * |
||
363 | * @param array<string,array|string|integer> $context current compile context |
||
364 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
365 | * |
||
366 | * @return string Return compiled code segment for the partial |
||
367 | */ |
||
368 | 91 | public static function partial(&$context, $vars) { |
|
388 | |||
389 | /** |
||
390 | * handle inline partial |
||
391 | * |
||
392 | * @param array<string,array|string|integer> $context current compile context |
||
393 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
394 | * |
||
395 | * @return string Return compiled code segment for the partial |
||
396 | */ |
||
397 | 10 | public static function inline(&$context, $vars) { |
|
408 | |||
409 | /** |
||
410 | * Return compiled PHP code for a handlebars inverted section begin token |
||
411 | * |
||
412 | * @param array<string,array|string|integer> $context current compile context |
||
413 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
414 | * |
||
415 | * @return string Return compiled code segment for the token |
||
416 | */ |
||
417 | 38 | protected static function invertedSection(&$context, $vars) { |
|
421 | |||
422 | /** |
||
423 | * Return compiled PHP code for a handlebars block custom helper begin 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 boolean $inverted the logic will be inverted |
||
428 | * |
||
429 | * @return string Return compiled code segment for the token |
||
430 | */ |
||
431 | 62 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
440 | |||
441 | /** |
||
442 | * Return compiled PHP code for a handlebars block end token |
||
443 | * |
||
444 | * @param array<string,array|string|integer> $context current compile context |
||
445 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
446 | * @param string|null $matchop should also match to this operator |
||
447 | * |
||
448 | * @return string Return compiled code segment for the token |
||
449 | */ |
||
450 | 325 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
481 | |||
482 | /** |
||
483 | * Return compiled PHP code for a handlebars block begin token |
||
484 | * |
||
485 | * @param array<string,array|string|integer> $context current compile context |
||
486 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
487 | * |
||
488 | * @return string Return compiled code segment for the token |
||
489 | */ |
||
490 | 242 | protected static function blockBegin(&$context, $vars) { |
|
510 | |||
511 | /** |
||
512 | * compile {{#foo}} token |
||
513 | * |
||
514 | * @param array<string,array|string|integer> $context current compile context |
||
515 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
516 | * @param boolean $isEach the section is #each |
||
517 | * |
||
518 | * @return string|null Return compiled code segment for the token |
||
519 | */ |
||
520 | 163 | protected static function section(&$context, $vars, $isEach = false) { |
|
538 | |||
539 | /** |
||
540 | * compile {{with}} token |
||
541 | * |
||
542 | * @param array<string,array|string|integer> $context current compile context |
||
543 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
544 | * |
||
545 | * @return string|null Return compiled code segment for the token |
||
546 | */ |
||
547 | 28 | protected static function with(&$context, $vars) { |
|
554 | |||
555 | /** |
||
556 | * Return compiled PHP code for a handlebars custom helper token |
||
557 | * |
||
558 | * @param array<string,array|string|integer> $context current compile context |
||
559 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
560 | * @param boolean $raw is this {{{ token or not |
||
561 | * @param boolean $nosep true to compile without seperator |
||
562 | * |
||
563 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
564 | */ |
||
565 | 463 | protected static function customHelper(&$context, $vars, $raw, $nosep) { |
|
578 | |||
579 | /** |
||
580 | * Return compiled PHP code for a handlebars else token |
||
581 | * |
||
582 | * @param array<string,array|string|integer> $context current compile context |
||
583 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
584 | * |
||
585 | * @return string Return compiled code segment for the token when the token is else |
||
586 | */ |
||
587 | 52 | protected static function doElse(&$context, $vars) { |
|
597 | |||
598 | /** |
||
599 | * Return compiled PHP code for a handlebars log token |
||
600 | * |
||
601 | * @param array<string,array|string|integer> $context current compile context |
||
602 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
603 | * @param boolean $raw is this {{{ token or not |
||
604 | * |
||
605 | * @return string Return compiled code segment for the token |
||
606 | */ |
||
607 | 2 | protected static function compileLog(&$context, &$vars, $raw) { |
|
612 | |||
613 | /** |
||
614 | * Return compiled PHP code for a handlebars lookup token |
||
615 | * |
||
616 | * @param array<string,array|string|integer> $context current compile context |
||
617 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
618 | * @param boolean $raw is this {{{ token or not |
||
619 | * |
||
620 | * @return string Return compiled code segment for the token |
||
621 | */ |
||
622 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
631 | |||
632 | /** |
||
633 | * Return compiled PHP code for template output |
||
634 | * |
||
635 | * @param array<string,array|string|integer> $context current compile context |
||
636 | * @param string $variable PHP code for the variable |
||
637 | * @param string $expression normalized handlebars expression |
||
638 | * @param boolean $raw is this {{{ token or not |
||
639 | * @param boolean $nosep true to compile without seperator |
||
640 | * |
||
641 | * @return string Return compiled code segment for the token |
||
642 | */ |
||
643 | 469 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) { |
|
651 | |||
652 | /** |
||
653 | * Return compiled PHP code for a handlebars variable token |
||
654 | * |
||
655 | * @param array<string,array|string|integer> $context current compile context |
||
656 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
657 | * @param boolean $raw is this {{{ token or not |
||
658 | * @param boolean $nosep true to compile without seperator |
||
659 | * |
||
660 | * @return string Return compiled code segment for the token |
||
661 | */ |
||
662 | 365 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) { |
|
671 | |||
672 | /** |
||
673 | * Add usage count to context |
||
674 | * |
||
675 | * @param array<string,array|string|integer> $context current context |
||
676 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
677 | * @param string $name used name |
||
678 | * @param integer $count increment |
||
679 | * |
||
680 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
681 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
682 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
683 | */ |
||
684 | 602 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
690 | } |
||
691 | |||
692 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.