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 | 761 | 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 | 684 | public static function composePHPRender($context, $code) { |
|
141 | |||
142 | /** |
||
143 | * Get function name for standalone or none standalone template. |
||
144 | * |
||
145 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
146 | * @param string $name base function name |
||
147 | * @param string $tag original handlabars tag for debug |
||
148 | * |
||
149 | * @return string compiled Function name |
||
150 | * |
||
151 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), 'test', '' |
||
152 | * @expect 'LL::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', '' |
||
153 | * @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 0, 'funcprefix' => 'lala_abc'), 'test3', '' |
||
154 | * @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc' |
||
155 | */ |
||
156 | 619 | protected static function getFuncName(&$context, $name, $tag) { |
|
169 | |||
170 | /** |
||
171 | * Get string presentation of variables |
||
172 | * |
||
173 | * @param array<string,array|string|integer> $context current compile context |
||
174 | * @param array<array> $vn variable name array. |
||
175 | * @param array<string>|null $blockParams block param list |
||
176 | * |
||
177 | * @return array<string|array> variable names |
||
178 | * |
||
179 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
180 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
181 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
182 | */ |
||
183 | 278 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
198 | |||
199 | /** |
||
200 | * Get string presentation of a sub expression |
||
201 | * |
||
202 | * @param array<string,array|string|integer> $context current compile context |
||
203 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
204 | * |
||
205 | * @return array<string> code representing passed expression |
||
206 | */ |
||
207 | 43 | public static function compileSubExpression(&$context, $vars) { |
|
216 | |||
217 | /** |
||
218 | * Get string presentation of a subexpression or a variable |
||
219 | * |
||
220 | * @param array<array|string|integer> $context current compile context |
||
221 | * @param array<array|string|integer> $var variable parsed path |
||
222 | * |
||
223 | * @return array<string> variable names |
||
|
|||
224 | */ |
||
225 | 391 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
228 | |||
229 | /** |
||
230 | * Get string presentation of a variable |
||
231 | * |
||
232 | * @param array<array|string|integer> $var variable parsed path |
||
233 | * @param array<array|string|integer> $context current compile context |
||
234 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
235 | * |
||
236 | * @return array<string> variable names |
||
237 | * |
||
238 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
239 | * @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') |
||
240 | * @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') |
||
241 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
242 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
243 | * @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') |
||
244 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
245 | * @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') |
||
246 | * @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') |
||
247 | * @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') |
||
248 | * @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') |
||
249 | * @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') |
||
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('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
252 | * @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') |
||
253 | * @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') |
||
254 | * @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') |
||
255 | * @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') |
||
256 | * @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') |
||
257 | */ |
||
258 | 612 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
332 | |||
333 | /** |
||
334 | * Return compiled PHP code for a handlebars token |
||
335 | * |
||
336 | * @param array<string,array|string|integer> $context current compile context |
||
337 | * @param array<string,array|boolean> $info parsed information |
||
338 | * |
||
339 | * @return string Return compiled code segment for the token |
||
340 | */ |
||
341 | 644 | protected static function compileToken(&$context, $info) { |
|
368 | |||
369 | /** |
||
370 | * handle partial |
||
371 | * |
||
372 | * @param array<string,array|string|integer> $context current compile context |
||
373 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
374 | * |
||
375 | * @return string Return compiled code segment for the partial |
||
376 | */ |
||
377 | 97 | public static function partial(&$context, $vars) { |
|
397 | |||
398 | /** |
||
399 | * handle inline partial |
||
400 | * |
||
401 | * @param array<string,array|string|integer> $context current compile context |
||
402 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
403 | * |
||
404 | * @return string Return compiled code segment for the partial |
||
405 | */ |
||
406 | 11 | public static function inline(&$context, $vars) { |
|
417 | |||
418 | /** |
||
419 | * Return compiled PHP code for a handlebars inverted section begin token |
||
420 | * |
||
421 | * @param array<string,array|string|integer> $context current compile context |
||
422 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
423 | * |
||
424 | * @return string Return compiled code segment for the token |
||
425 | */ |
||
426 | 38 | protected static function invertedSection(&$context, $vars) { |
|
430 | |||
431 | /** |
||
432 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
433 | * |
||
434 | * @param array<string,array|string|integer> $context current compile context |
||
435 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
436 | * @param boolean $inverted the logic will be inverted |
||
437 | * |
||
438 | * @return string Return compiled code segment for the token |
||
439 | */ |
||
440 | 63 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
449 | |||
450 | /** |
||
451 | * Return compiled PHP code for a handlebars block end 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 | * @param string|null $matchop should also match to this operator |
||
456 | * |
||
457 | * @return string Return compiled code segment for the token |
||
458 | */ |
||
459 | 332 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
491 | |||
492 | /** |
||
493 | * Return compiled PHP code for a handlebars block begin token |
||
494 | * |
||
495 | * @param array<string,array|string|integer> $context current compile context |
||
496 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
497 | * |
||
498 | * @return string Return compiled code segment for the token |
||
499 | */ |
||
500 | 248 | protected static function blockBegin(&$context, $vars) { |
|
520 | |||
521 | /** |
||
522 | * compile {{#foo}} token |
||
523 | * |
||
524 | * @param array<string,array|string|integer> $context current compile context |
||
525 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
526 | * @param boolean $isEach the section is #each |
||
527 | * |
||
528 | * @return string|null Return compiled code segment for the token |
||
529 | */ |
||
530 | 166 | protected static function section(&$context, $vars, $isEach = false) { |
|
548 | |||
549 | /** |
||
550 | * compile {{with}} token |
||
551 | * |
||
552 | * @param array<string,array|string|integer> $context current compile context |
||
553 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
554 | * |
||
555 | * @return string|null Return compiled code segment for the token |
||
556 | */ |
||
557 | 30 | protected static function with(&$context, $vars) { |
|
564 | |||
565 | /** |
||
566 | * Return compiled PHP code for a handlebars custom helper token |
||
567 | * |
||
568 | * @param array<string,array|string|integer> $context current compile context |
||
569 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
570 | * @param boolean $raw is this {{{ token or not |
||
571 | * @param boolean $nosep true to compile without seperator |
||
572 | * @param boolean $subExp true when compile for subexpression |
||
573 | * |
||
574 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
575 | */ |
||
576 | 477 | protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) { |
|
598 | |||
599 | /** |
||
600 | * Return compiled PHP code for a handlebars else token |
||
601 | * |
||
602 | * @param array<string,array|string|integer> $context current compile context |
||
603 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
604 | * |
||
605 | * @return string Return compiled code segment for the token when the token is else |
||
606 | */ |
||
607 | 57 | protected static function doElse(&$context, $vars) { |
|
618 | |||
619 | /** |
||
620 | * Return compiled PHP code for a handlebars log token |
||
621 | * |
||
622 | * @param array<string,array|string|integer> $context current compile context |
||
623 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
624 | * @param boolean $raw is this {{{ token or not |
||
625 | * |
||
626 | * @return string Return compiled code segment for the token |
||
627 | */ |
||
628 | 2 | protected static function compileLog(&$context, &$vars, $raw) { |
|
633 | |||
634 | /** |
||
635 | * Return compiled PHP code for a handlebars lookup token |
||
636 | * |
||
637 | * @param array<string,array|string|integer> $context current compile context |
||
638 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
639 | * @param boolean $raw is this {{{ token or not |
||
640 | * @param boolean $nosep true to compile without seperator |
||
641 | * |
||
642 | * @return string Return compiled code segment for the token |
||
643 | */ |
||
644 | 7 | protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) { |
|
656 | |||
657 | /** |
||
658 | * Return compiled PHP code for template output |
||
659 | * |
||
660 | * @param array<string,array|string|integer> $context current compile context |
||
661 | * @param string $variable PHP code for the variable |
||
662 | * @param string $expression normalized handlebars expression |
||
663 | * @param boolean $raw is this {{{ token or not |
||
664 | * @param boolean $nosep true to compile without seperator |
||
665 | * |
||
666 | * @return string Return compiled code segment for the token |
||
667 | */ |
||
668 | 477 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) { |
|
676 | |||
677 | /** |
||
678 | * Return compiled PHP code for a handlebars variable token |
||
679 | * |
||
680 | * @param array<string,array|string|integer> $context current compile context |
||
681 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
682 | * @param boolean $raw is this {{{ token or not |
||
683 | * @param boolean $nosep true to compile without seperator |
||
684 | * |
||
685 | * @return string Return compiled code segment for the token |
||
686 | */ |
||
687 | 371 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) { |
|
696 | |||
697 | /** |
||
698 | * Add usage count to context |
||
699 | * |
||
700 | * @param array<string,array|string|integer> $context current context |
||
701 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
702 | * @param string $name used name |
||
703 | * @param integer $count increment |
||
704 | * |
||
705 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
706 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
707 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
708 | */ |
||
709 | 619 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
715 | } |
||
716 | |||
717 |
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.