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 | 720 | public static function compileTemplate(&$context, $template) { |
|
44 | 720 | array_unshift($context['parsed'], array()); |
|
45 | 720 | Validator::verify($context, $template); |
|
46 | |||
47 | 720 | if (count($context['error'])) { |
|
48 | 70 | return; |
|
49 | } |
||
50 | |||
51 | // Do PHP code generation. |
||
52 | 651 | Parser::setDelimiter($context); |
|
53 | |||
54 | // Handle dynamic partials |
||
55 | 651 | Partial::handleDynamic($context); |
|
56 | |||
57 | 651 | $code = ''; |
|
58 | 651 | foreach ($context['parsed'][0] as $info) { |
|
59 | 651 | if (is_array($info)) { |
|
60 | 610 | $context['tokens']['current']++; |
|
61 | 610 | $tmpl = static::compileToken($context, $info); |
|
62 | 610 | if ($tmpl == $context['ops']['seperator']) { |
|
63 | 1 | $tmpl = ''; |
|
64 | } else { |
||
65 | 609 | $tmpl = "'$tmpl'"; |
|
66 | } |
||
67 | 610 | $code .= $tmpl; |
|
68 | } else { |
||
69 | 651 | $code .= $info; |
|
70 | } |
||
71 | } |
||
72 | |||
73 | 651 | static::$lastParsed = array_shift($context['parsed']); |
|
74 | |||
75 | 651 | 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 | 650 | public static function composePHPRender($context, $code) { |
|
87 | 650 | $flagJStrue = Expression::boolString($context['flags']['jstrue']); |
|
88 | 650 | $flagJSObj = Expression::boolString($context['flags']['jsobj']); |
|
89 | 650 | $flagSPVar = Expression::boolString($context['flags']['spvar']); |
|
90 | 650 | $flagProp = Expression::boolString($context['flags']['prop']); |
|
91 | 650 | $flagMethod = Expression::boolString($context['flags']['method']); |
|
92 | 650 | $flagLambda = Expression::boolString($context['flags']['lambda']); |
|
93 | 650 | $flagMustlok = Expression::boolString($context['flags']['mustlok']); |
|
94 | 650 | $flagMustlam = Expression::boolString($context['flags']['mustlam']); |
|
95 | 650 | $flagEcho = Expression::boolString($context['flags']['echo']); |
|
96 | 650 | $flagPartNC = Expression::boolString($context['flags']['partnc']); |
|
97 | 650 | $flagKnownHlp = Expression::boolString($context['flags']['knohlp']); |
|
98 | |||
99 | 650 | $libstr = Exporter::runtime($context); |
|
100 | 650 | $constants = Exporter::constants($context); |
|
101 | 650 | $helpers = Exporter::helpers($context); |
|
102 | 650 | $bhelpers = Exporter::helpers($context, 'blockhelpers'); |
|
103 | 650 | $hbhelpers = Exporter::helpers($context, 'hbhelpers'); |
|
104 | 650 | $partials = implode(",\n", $context['partialCode']); |
|
105 | 650 | $debug = Runtime::DEBUG_ERROR_LOG; |
|
106 | |||
107 | // Return generated PHP code string. |
||
108 | 650 | return "use {$context['runtime']} as LR; |
|
109 | return function (\$in, \$options = null) { |
||
110 | \$cx = array( |
||
111 | 'flags' => array( |
||
112 | 'jstrue' => $flagJStrue, |
||
113 | 'jsobj' => $flagJSObj, |
||
114 | 'spvar' => $flagSPVar, |
||
115 | 'prop' => $flagProp, |
||
116 | 'method' => $flagMethod, |
||
117 | 'lambda' => $flagLambda, |
||
118 | 'mustlok' => $flagMustlok, |
||
119 | 'mustlam' => $flagMustlam, |
||
120 | 'echo' => $flagEcho, |
||
121 | 'partnc' => $flagPartNC, |
||
122 | 'knohlp' => $flagKnownHlp, |
||
123 | 'debug' => isset(\$options['debug']) ? \$options['debug'] : $debug, |
||
124 | ), |
||
125 | 'constants' => $constants, |
||
126 | 'helpers' => $helpers, |
||
127 | 'blockhelpers' => $bhelpers, |
||
128 | 'hbhelpers' => isset(\$options['helpers']) ? array_merge($hbhelpers, \$options['helpers']) : $hbhelpers, |
||
129 | 'partials' => array($partials), |
||
130 | 'scopes' => array(), |
||
131 | 'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in), |
||
132 | 'blparam' => array(), |
||
133 | 650 | 'runtime' => '{$context['runtime']}', |
|
134 | $libstr |
||
135 | ); |
||
136 | 650 | {$context['renderex']} |
|
137 | 650 | {$context['ops']['op_start']}'$code'{$context['ops']['op_end']} |
|
138 | 650 | };"; |
|
139 | } |
||
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 | 586 | protected static function getFuncName(&$context, $name, $tag) { |
|
156 | 586 | static::addUsageCount($context, 'runtime', $name); |
|
157 | |||
158 | 586 | if ($context['flags']['debug'] && ($name != 'miss')) { |
|
159 | 10 | $dbg = "'$tag', '$name', "; |
|
160 | 10 | $name = 'debug'; |
|
161 | 10 | static::addUsageCount($context, 'runtime', 'debug'); |
|
162 | } else { |
||
163 | 584 | $dbg = ''; |
|
164 | } |
||
165 | |||
166 | 586 | return $context['flags']['standalone'] ? "\$cx['funcs']['$name']($dbg" : "LR::$name($dbg"; |
|
167 | } |
||
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 | 259 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
183 | 259 | $vars = array(array(), array()); |
|
184 | 259 | $exps = array(); |
|
185 | 259 | foreach ($vn as $i => $v) { |
|
186 | 220 | $V = static::getVariableNameOrSubExpression($context, $v); |
|
187 | 220 | if (is_string($i)) { |
|
188 | 36 | $vars[1][] = "'$i'=>{$V[0]}"; |
|
189 | } else { |
||
190 | 206 | $vars[0][] = $V[0]; |
|
191 | } |
||
192 | 220 | $exps[] = $V[1]; |
|
193 | } |
||
194 | 259 | $bp = $blockParams ? (',array(' . Expression::listString($blockParams) . ')') : ''; |
|
195 | 259 | return array('array(array(' . implode(',', $vars[0]) . '),array(' . implode(',', $vars[1]) . ")$bp)", $exps); |
|
196 | } |
||
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) { |
|
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 | 366 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
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>|null $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 | 581 | 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 | 610 | protected static function compileToken(&$context, $info) { |
|
348 | |||
349 | /** |
||
350 | * handle partial |
||
351 | * |
||
352 | * @param array<string,array|string|integer> $context current compile context |
||
353 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
354 | * |
||
355 | * @return string Return compiled code segment for the partial |
||
356 | */ |
||
357 | 86 | public static function partial(&$context, $vars) { |
|
380 | |||
381 | /** |
||
382 | * handle inline partial |
||
383 | * |
||
384 | * @param array<string,array|string|integer> $context current compile context |
||
385 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
386 | * |
||
387 | * @return string Return compiled code segment for the partial |
||
388 | */ |
||
389 | 10 | public static function inline(&$context, $vars) { |
|
400 | |||
401 | /** |
||
402 | * Return compiled PHP code for a handlebars inverted section begin token |
||
403 | * |
||
404 | * @param array<string,array|string|integer> $context current compile context |
||
405 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
406 | * |
||
407 | * @return string Return compiled code segment for the token |
||
408 | */ |
||
409 | 38 | protected static function invertedSection(&$context, $vars) { |
|
413 | |||
414 | /** |
||
415 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
416 | * |
||
417 | * @param array<string,array|string|integer> $context current compile context |
||
418 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
419 | * @param boolean $inverted the logic will be inverted |
||
420 | * |
||
421 | * @return string Return compiled code segment for the token |
||
422 | */ |
||
423 | 61 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
434 | |||
435 | /** |
||
436 | * Return compiled PHP code for a handlebars block end token |
||
437 | * |
||
438 | * @param array<string,array|string|integer> $context current compile context |
||
439 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
440 | * @param string|null $matchop should also match to this operator |
||
441 | * |
||
442 | * @return string Return compiled code segment for the token |
||
443 | */ |
||
444 | 319 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
480 | |||
481 | /** |
||
482 | * Return compiled PHP code for a handlebars block begin token |
||
483 | * |
||
484 | * @param array<string,array|string|integer> $context current compile context |
||
485 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
486 | * |
||
487 | * @return string Return compiled code segment for the token |
||
488 | */ |
||
489 | 235 | protected static function blockBegin(&$context, $vars) { |
|
509 | |||
510 | /** |
||
511 | * compile {{#foo}} 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 | * @param boolean $isEach the section is #each |
||
516 | * |
||
517 | * @return string|null Return compiled code segment for the token |
||
518 | */ |
||
519 | 162 | protected static function section(&$context, $vars, $isEach = false) { |
|
540 | |||
541 | /** |
||
542 | * compile {{with}} token |
||
543 | * |
||
544 | * @param array<string,array|string|integer> $context current compile context |
||
545 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
546 | * |
||
547 | * @return string|null Return compiled code segment for the token |
||
548 | */ |
||
549 | 27 | protected static function with(&$context, $vars) { |
|
556 | |||
557 | /** |
||
558 | * Return compiled PHP code for a handlebars custom helper token |
||
559 | * |
||
560 | * @param array<string,array|string|integer> $context current compile context |
||
561 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
562 | * @param boolean $raw is this {{{ token or not |
||
563 | * |
||
564 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
565 | */ |
||
566 | 450 | protected static function customHelper(&$context, $vars, $raw) { |
|
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 | 49 | 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 | 1 | 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 a handlebars variable token |
||
634 | * |
||
635 | * @param array<string,array|string|integer> $context current compile context |
||
636 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
637 | * @param boolean $raw is this {{{ token or not |
||
638 | * |
||
639 | * @return string Return compiled code segment for the token |
||
640 | */ |
||
641 | 361 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
654 | |||
655 | /** |
||
656 | * Add usage count to context |
||
657 | * |
||
658 | * @param array<string,array|string|integer> $context current context |
||
659 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
660 | * @param string $name used name |
||
661 | * @param integer $count increment |
||
662 | * |
||
663 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
664 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
665 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
666 | */ |
||
667 | 586 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
673 | } |
||
674 | |||
675 |
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.