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 |
||
30 | class Compiler extends Validator |
||
31 | { |
||
32 | public static $lastParsed; |
||
33 | |||
34 | /** |
||
35 | * Compile template into PHP code |
||
36 | * |
||
37 | * @param array<string,array|string|integer> $context Current context |
||
38 | * @param string $template handlebars template |
||
39 | * |
||
40 | * @return string|null generated PHP code |
||
41 | */ |
||
42 | 630 | public static function compileTemplate(&$context, $template) { |
|
76 | |||
77 | /** |
||
78 | * Compose LightnCandy render codes for include() |
||
79 | * |
||
80 | * @param array<string,array|string|integer> $context Current context |
||
81 | * @param string $code generated PHP code |
||
82 | * |
||
83 | * @return string Composed PHP code |
||
84 | */ |
||
85 | 566 | public static function composePHPRender($context, $code) { |
|
86 | 566 | $flagJStrue = Expression::boolString($context['flags']['jstrue']); |
|
87 | 566 | $flagJSObj = Expression::boolString($context['flags']['jsobj']); |
|
88 | 566 | $flagSPVar = Expression::boolString($context['flags']['spvar']); |
|
89 | 566 | $flagProp = Expression::boolString($context['flags']['prop']); |
|
90 | 566 | $flagMethod = Expression::boolString($context['flags']['method']); |
|
91 | 566 | $flagLambda = Expression::boolString($context['flags']['lambda']); |
|
92 | 566 | $flagMustlok = Expression::boolString($context['flags']['mustlok']); |
|
93 | 566 | $flagMustlam = Expression::boolString($context['flags']['mustlam']); |
|
94 | 566 | $flagEcho = Expression::boolString($context['flags']['echo']); |
|
95 | |||
96 | 566 | $libstr = Exporter::runtime($context); |
|
97 | 566 | $constants = Exporter::constants($context); |
|
98 | 566 | $helpers = Exporter::helpers($context); |
|
99 | 566 | $bhelpers = Exporter::helpers($context, 'blockhelpers'); |
|
100 | 566 | $hbhelpers = Exporter::helpers($context, 'hbhelpers'); |
|
101 | 566 | $debug = Runtime::DEBUG_ERROR_LOG; |
|
102 | 566 | $phpstart = $context['flags']['bare'] ? '' : "<?php use {$context['runtime']} as LR;\n"; |
|
103 | 566 | $phpend = $context['flags']['bare'] ? ';' : "\n?>"; |
|
104 | |||
105 | // Return generated PHP code string. |
||
106 | 566 | return "{$phpstart}return function (\$in, \$options = null) { |
|
107 | \$cx = array( |
||
108 | 'flags' => array( |
||
109 | 566 | 'jstrue' => $flagJStrue, |
|
110 | 566 | 'jsobj' => $flagJSObj, |
|
111 | 566 | 'spvar' => $flagSPVar, |
|
112 | 566 | 'prop' => $flagProp, |
|
113 | 566 | 'method' => $flagMethod, |
|
114 | 566 | 'lambda' => $flagLambda, |
|
115 | 566 | 'mustlok' => $flagMustlok, |
|
116 | 566 | 'mustlam' => $flagMustlam, |
|
117 | 566 | 'echo' => $flagEcho, |
|
118 | 566 | 'debug' => isset(\$options['debug']) ? \$options['debug'] : $debug, |
|
119 | ), |
||
120 | 566 | 'constants' => $constants, |
|
121 | 566 | 'helpers' => $helpers, |
|
122 | 566 | 'blockhelpers' => $bhelpers, |
|
123 | 566 | 'hbhelpers' => isset(\$options['helpers']) ? array_merge($hbhelpers, \$options['helpers']) : $hbhelpers, |
|
124 | 566 | 'partials' => array({$context['partialCode']}), |
|
125 | 'scopes' => array(), |
||
126 | 'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in), |
||
127 | 566 | 'runtime' => '{$context['runtime']}', |
|
128 | 566 | $libstr |
|
129 | ); |
||
130 | 566 | {$context['renderex']} |
|
131 | 566 | {$context['ops']['op_start']}'$code'{$context['ops']['op_end']} |
|
132 | 566 | }$phpend"; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get function name for standalone or none standalone template. |
||
137 | * |
||
138 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
139 | * @param string $name base function name |
||
140 | * @param string $tag original handlabars tag for debug |
||
141 | * |
||
142 | * @return string compiled Function name |
||
143 | * |
||
144 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
145 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
146 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
147 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
148 | */ |
||
149 | 502 | protected static function getFuncName(&$context, $name, $tag) { |
|
162 | |||
163 | /** |
||
164 | * Get string presentation of variables |
||
165 | * |
||
166 | * @param array<array> $vn variable name array. |
||
167 | * @param array<string,array|string|integer> $context current compile context |
||
168 | * |
||
169 | * @return array<string|array> variable names |
||
170 | * |
||
171 | * @expect array('array(array($in),array())', array('this')) when input array(null), array('flags'=>array('spvar'=>true)) |
||
172 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array(null, null), array('flags'=>array('spvar'=>true)) |
||
173 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('a' => null), array('flags'=>array('spvar'=>true)) |
||
174 | */ |
||
175 | 210 | protected static function getVariableNames($vn, &$context) { |
|
189 | |||
190 | /** |
||
191 | * Get string presentation of a sub expression |
||
192 | * |
||
193 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
194 | * @param array<string,array|string|integer> $context current compile context |
||
195 | * |
||
196 | * @return array<string> code representing passed expression |
||
197 | */ |
||
198 | 35 | public static function compileSubExpression($vars, &$context) { |
|
212 | |||
213 | /** |
||
214 | * Get string presentation of a subexpression or a variable |
||
215 | * |
||
216 | * @param array<array|string|integer> $var variable parsed path |
||
217 | * @param array<array|string|integer> $context current compile context |
||
218 | * |
||
219 | * @return array<string> variable names |
||
220 | */ |
||
221 | 341 | protected static function getVariableNameOrSubExpression($var, &$context) { |
|
224 | |||
225 | /** |
||
226 | * Get string presentation of a variable |
||
227 | * |
||
228 | * @param array<array|string|integer> $var variable parsed path |
||
229 | * @param array<array|string|integer> $context current compile context |
||
230 | * @param array<string> $lookup extra lookup string as valid PHP variable name |
||
|
|||
231 | * |
||
232 | * @return array<string> variable names |
||
233 | * |
||
234 | * @expect array('$in', 'this') when input array(null), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
235 | * @expect array('((isset($in[\'true\']) && is_array($in)) ? $in[\'true\'] : null)', '[true]') when input array('true'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
236 | * @expect array('((isset($in[\'false\']) && is_array($in)) ? $in[\'false\'] : null)', '[false]') when input array('false'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
237 | * @expect array('true', 'true') when input array(0, 'true'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
238 | * @expect array('false', 'false') when input array(0, 'false'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
239 | * @expect array('((isset($in[\'2\']) && is_array($in)) ? $in[\'2\'] : null)', '[2]') when input array('2'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
240 | * @expect array('2', '2') when input array(0, '2'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)) |
||
241 | * @expect array('((isset($in[\'@index\']) && is_array($in)) ? $in[\'@index\'] : null)', '[@index]') when input array('@index'), array('flags'=>array('spvar'=>false,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
242 | * @expect array("((isset(\$cx['sp_vars']['index']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['index'] : null)", '@[index]') when input array('@index'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
243 | * @expect array("((isset(\$cx['sp_vars']['key']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['key'] : null)", '@[key]') when input array('@key'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
244 | * @expect array("((isset(\$cx['sp_vars']['first']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['first'] : null)", '@[first]') when input array('@first'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
245 | * @expect array("((isset(\$cx['sp_vars']['last']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['last'] : null)", '@[last]') when input array('@last'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
246 | * @expect array('((isset($in[\'"a"\']) && is_array($in)) ? $in[\'"a"\'] : null)', '["a"]') when input array('"a"'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
247 | * @expect array('"a"', '"a"') when input array(0, '"a"'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
248 | * @expect array('((isset($in[\'a\']) && is_array($in)) ? $in[\'a\'] : null)', '[a]') when input array('a'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
249 | * @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(1,'a'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
250 | * @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(3,'a'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
251 | * @expect array('((isset($in[\'id\']) && is_array($in)) ? $in[\'id\'] : null)', 'this.[id]') when input array(null, 'id'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)) |
||
252 | * @expect array('LR::v($cx, isset($in) ? $in : null, array(\'id\'))', 'this.[id]') when input array(null, 'id'), array('flags'=>array('prop'=>true,'spvar'=>true,'debug'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0,'standalone'=>0), 'runtime' => 'Runtime') |
||
253 | */ |
||
254 | 502 | protected static function getVariableName($var, &$context, $lookup = null) { |
|
296 | |||
297 | /** |
||
298 | * Return compiled PHP code for a handlebars token |
||
299 | * |
||
300 | * @param array<string,array|boolean> $info parsed information |
||
301 | * @param array<string,array|string|integer> $context current compile context |
||
302 | * |
||
303 | * @return string Return compiled code segment for the token |
||
304 | */ |
||
305 | 526 | protected static function compileToken($info, &$context) { |
|
334 | |||
335 | /** |
||
336 | * handle partial |
||
337 | * |
||
338 | * @param array<string,array|string|integer> $context current compile context |
||
339 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
340 | * |
||
341 | * @return string Return compiled code segment for the partial |
||
342 | */ |
||
343 | 62 | public static function partial(&$context, $vars) { |
|
365 | |||
366 | /** |
||
367 | * Return compiled PHP code for a handlebars inverted section begin token |
||
368 | * |
||
369 | * @param array<string,array|string|integer> $context current compile context |
||
370 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
371 | * |
||
372 | * @return string Return compiled code segment for the token |
||
373 | */ |
||
374 | 37 | protected static function invertedSection(&$context, $vars) { |
|
378 | |||
379 | /** |
||
380 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
381 | * |
||
382 | * @param array<string,array|string|integer> $context current compile context |
||
383 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
384 | * @param boolean $inverted the logic will be inverted |
||
385 | * |
||
386 | * @return string Return compiled code segment for the token |
||
387 | */ |
||
388 | 52 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
398 | |||
399 | /** |
||
400 | * Return compiled PHP code for a handlebars block end token |
||
401 | * |
||
402 | * @param array<string,array|string|integer> $context current compile context |
||
403 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
404 | * |
||
405 | * @return string Return compiled code segment for the token |
||
406 | */ |
||
407 | 279 | protected static function blockEnd(&$context, $vars) { |
|
438 | |||
439 | /** |
||
440 | * Return compiled PHP code for a handlebars block begin token |
||
441 | * |
||
442 | * @param array<string,array|string|integer> $context current compile context |
||
443 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
444 | * |
||
445 | * @return string Return compiled code segment for the token |
||
446 | */ |
||
447 | 205 | protected static function blockBegin(&$context, $vars) { |
|
467 | |||
468 | /** |
||
469 | * compile {{#foo}} token |
||
470 | * |
||
471 | * @param array<string,array|string|integer> $context current compile context |
||
472 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
473 | * @param boolean $isEach the section is #each |
||
474 | * |
||
475 | * @return string|null Return compiled code segment for the token |
||
476 | */ |
||
477 | 148 | protected static function section(&$context, $vars, $isEach = false) { |
|
488 | |||
489 | /** |
||
490 | * compile {{with}} token |
||
491 | * |
||
492 | * @param array<string,array|string|integer> $context current compile context |
||
493 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
494 | * |
||
495 | * @return string|null Return compiled code segment for the token |
||
496 | */ |
||
497 | 15 | protected static function with(&$context, $vars) { |
|
501 | |||
502 | /** |
||
503 | * Return compiled PHP code for a handlebars custom helper token |
||
504 | * |
||
505 | * @param array<string,array|string|integer> $context current compile context |
||
506 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
507 | * @param boolean $raw is this {{{ token or not |
||
508 | * |
||
509 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
510 | */ |
||
511 | 389 | protected static function customHelper(&$context, $vars, $raw) { |
|
523 | |||
524 | /** |
||
525 | * Return compiled PHP code for a handlebars else token |
||
526 | * |
||
527 | * @param array<string,array|string|integer> $context current compile context |
||
528 | * |
||
529 | * @return string Return compiled code segment for the token when the token is else |
||
530 | */ |
||
531 | 44 | protected static function doElse(&$context) { |
|
541 | |||
542 | /** |
||
543 | * Return compiled PHP code for a handlebars lookup 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 | * @param boolean $raw is this {{{ token or not |
||
548 | * |
||
549 | * @return string Return compiled code segment for the token |
||
550 | */ |
||
551 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
560 | |||
561 | /** |
||
562 | * Return compiled PHP code for a handlebars variable token |
||
563 | * |
||
564 | * @param array<string,array|string|integer> $context current compile context |
||
565 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
566 | * @param boolean $raw is this {{{ token or not |
||
567 | * |
||
568 | * @return string Return compiled code segment for the token |
||
569 | */ |
||
570 | 313 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
578 | |||
579 | /** |
||
580 | * Add usage count to context |
||
581 | * |
||
582 | * @param array<string,array|string|integer> $context current context |
||
583 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
584 | * @param string $name used name |
||
585 | * @param integer $count increment |
||
586 | * |
||
587 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
588 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
589 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
590 | */ |
||
591 | 502 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
597 | } |
||
598 | |||
599 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.