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 | 749 | 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 | 672 | 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 | 607 | 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 | 272 | 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 | 42 | 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 | 383 | 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 | 600 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
325 | |||
326 | /** |
||
327 | * Return compiled PHP code for a handlebars token |
||
328 | * |
||
329 | * @param array<string,array|string|integer> $context current compile context |
||
330 | * @param array<string,array|boolean> $info parsed information |
||
331 | * |
||
332 | * @return string Return compiled code segment for the token |
||
333 | */ |
||
334 | 632 | protected static function compileToken(&$context, $info) { |
|
361 | |||
362 | /** |
||
363 | * handle partial |
||
364 | * |
||
365 | * @param array<string,array|string|integer> $context current compile context |
||
366 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
367 | * |
||
368 | * @return string Return compiled code segment for the partial |
||
369 | */ |
||
370 | 92 | public static function partial(&$context, $vars) { |
|
390 | |||
391 | /** |
||
392 | * handle inline partial |
||
393 | * |
||
394 | * @param array<string,array|string|integer> $context current compile context |
||
395 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
396 | * |
||
397 | * @return string Return compiled code segment for the partial |
||
398 | */ |
||
399 | 10 | public static function inline(&$context, $vars) { |
|
410 | |||
411 | /** |
||
412 | * Return compiled PHP code for a handlebars inverted section 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 | * |
||
417 | * @return string Return compiled code segment for the token |
||
418 | */ |
||
419 | 38 | protected static function invertedSection(&$context, $vars) { |
|
423 | |||
424 | /** |
||
425 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
426 | * |
||
427 | * @param array<string,array|string|integer> $context current compile context |
||
428 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
429 | * @param boolean $inverted the logic will be inverted |
||
430 | * |
||
431 | * @return string Return compiled code segment for the token |
||
432 | */ |
||
433 | 63 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
442 | |||
443 | /** |
||
444 | * Return compiled PHP code for a handlebars block end token |
||
445 | * |
||
446 | * @param array<string,array|string|integer> $context current compile context |
||
447 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
448 | * @param string|null $matchop should also match to this operator |
||
449 | * |
||
450 | * @return string Return compiled code segment for the token |
||
451 | */ |
||
452 | 329 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
484 | |||
485 | /** |
||
486 | * Return compiled PHP code for a handlebars block begin token |
||
487 | * |
||
488 | * @param array<string,array|string|integer> $context current compile context |
||
489 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
490 | * |
||
491 | * @return string Return compiled code segment for the token |
||
492 | */ |
||
493 | 245 | protected static function blockBegin(&$context, $vars) { |
|
513 | |||
514 | /** |
||
515 | * compile {{#foo}} token |
||
516 | * |
||
517 | * @param array<string,array|string|integer> $context current compile context |
||
518 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
519 | * @param boolean $isEach the section is #each |
||
520 | * |
||
521 | * @return string|null Return compiled code segment for the token |
||
522 | */ |
||
523 | 164 | protected static function section(&$context, $vars, $isEach = false) { |
|
541 | |||
542 | /** |
||
543 | * compile {{with}} token |
||
544 | * |
||
545 | * @param array<string,array|string|integer> $context current compile context |
||
546 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
547 | * |
||
548 | * @return string|null Return compiled code segment for the token |
||
549 | */ |
||
550 | 29 | protected static function with(&$context, $vars) { |
|
557 | |||
558 | /** |
||
559 | * Return compiled PHP code for a handlebars custom helper token |
||
560 | * |
||
561 | * @param array<string,array|string|integer> $context current compile context |
||
562 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
563 | * @param boolean $raw is this {{{ token or not |
||
564 | * @param boolean $nosep true to compile without seperator |
||
565 | * @param boolean $subExp true when compile for subexpression |
||
566 | * |
||
567 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
568 | */ |
||
569 | 468 | protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) { |
|
587 | |||
588 | /** |
||
589 | * Return compiled PHP code for a handlebars else 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 | * |
||
594 | * @return string Return compiled code segment for the token when the token is else |
||
595 | */ |
||
596 | 56 | protected static function doElse(&$context, $vars) { |
|
607 | |||
608 | /** |
||
609 | * Return compiled PHP code for a handlebars log token |
||
610 | * |
||
611 | * @param array<string,array|string|integer> $context current compile context |
||
612 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
613 | * @param boolean $raw is this {{{ token or not |
||
614 | * |
||
615 | * @return string Return compiled code segment for the token |
||
616 | */ |
||
617 | 2 | protected static function compileLog(&$context, &$vars, $raw) { |
|
622 | |||
623 | /** |
||
624 | * Return compiled PHP code for a handlebars lookup token |
||
625 | * |
||
626 | * @param array<string,array|string|integer> $context current compile context |
||
627 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
628 | * @param boolean $raw is this {{{ token or not |
||
629 | * @param boolean $nosep true to compile without seperator |
||
630 | * |
||
631 | * @return string Return compiled code segment for the token |
||
632 | */ |
||
633 | 3 | protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) { |
|
644 | |||
645 | /** |
||
646 | * Return compiled PHP code for template output |
||
647 | * |
||
648 | * @param array<string,array|string|integer> $context current compile context |
||
649 | * @param string $variable PHP code for the variable |
||
650 | * @param string $expression normalized handlebars expression |
||
651 | * @param boolean $raw is this {{{ token or not |
||
652 | * @param boolean $nosep true to compile without seperator |
||
653 | * |
||
654 | * @return string Return compiled code segment for the token |
||
655 | */ |
||
656 | 471 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) { |
|
664 | |||
665 | /** |
||
666 | * Return compiled PHP code for a handlebars variable token |
||
667 | * |
||
668 | * @param array<string,array|string|integer> $context current compile context |
||
669 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
670 | * @param boolean $raw is this {{{ token or not |
||
671 | * @param boolean $nosep true to compile without seperator |
||
672 | * |
||
673 | * @return string Return compiled code segment for the token |
||
674 | */ |
||
675 | 367 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) { |
|
684 | |||
685 | /** |
||
686 | * Add usage count to context |
||
687 | * |
||
688 | * @param array<string,array|string|integer> $context current context |
||
689 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
690 | * @param string $name used name |
||
691 | * @param integer $count increment |
||
692 | * |
||
693 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
694 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
695 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
696 | */ |
||
697 | 607 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
703 | } |
||
704 | |||
705 |
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.