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 | 635 | 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 | 570 | public static function composePHPRender($context, $code) { |
|
136 | |||
137 | /** |
||
138 | * Get function name for standalone or none standalone template. |
||
139 | * |
||
140 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
141 | * @param string $name base function name |
||
142 | * @param string $tag original handlabars tag for debug |
||
143 | * |
||
144 | * @return string compiled Function name |
||
145 | * |
||
146 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
147 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
148 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
149 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
150 | */ |
||
151 | 506 | protected static function getFuncName(&$context, $name, $tag) { |
|
164 | |||
165 | /** |
||
166 | * Get string presentation of variables |
||
167 | * |
||
168 | * @param array<array> $vn variable name array. |
||
169 | * @param array<string,array|string|integer> $context current compile context |
||
170 | * |
||
171 | * @return array<string|array> variable names |
||
172 | * |
||
173 | * @expect array('array(array($in),array())', array('this')) when input array(null), array('flags'=>array('spvar'=>true)) |
||
174 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array(null, null), array('flags'=>array('spvar'=>true)) |
||
175 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('a' => null), array('flags'=>array('spvar'=>true)) |
||
176 | */ |
||
177 | 205 | protected static function getVariableNames($vn, &$context) { |
|
191 | |||
192 | /** |
||
193 | * Get string presentation of a sub expression |
||
194 | * |
||
195 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
196 | * @param array<string,array|string|integer> $context current compile context |
||
197 | * |
||
198 | * @return array<string> code representing passed expression |
||
199 | */ |
||
200 | 35 | public static function compileSubExpression($vars, &$context) { |
|
214 | |||
215 | /** |
||
216 | * Get string presentation of a subexpression or a variable |
||
217 | * |
||
218 | * @param array<array|string|integer> $var variable parsed path |
||
219 | * @param array<array|string|integer> $context current compile context |
||
220 | * |
||
221 | * @return array<string> variable names |
||
222 | */ |
||
223 | 341 | protected static function getVariableNameOrSubExpression($var, &$context) { |
|
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> $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(null), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
237 | * @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)) |
||
238 | * @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)) |
||
239 | * @expect array('true', 'true') when input array(0, 'true'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
240 | * @expect array('false', 'false') when input array(0, 'false'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
241 | * @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)) |
||
242 | * @expect array('2', '2') when input array(0, '2'), array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)) |
||
243 | * @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)) |
||
244 | * @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)) |
||
245 | * @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)) |
||
246 | * @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)) |
||
247 | * @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)) |
||
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('"a"', '"a"') when input array(0, '"a"'), array('flags'=>array('spvar'=>true,'debug'=>0)) |
||
250 | * @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)) |
||
251 | * @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)) |
||
252 | * @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)) |
||
253 | * @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)) |
||
254 | * @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') |
||
255 | */ |
||
256 | 503 | protected static function getVariableName($var, &$context, $lookup = null) { |
|
298 | |||
299 | /** |
||
300 | * Return compiled PHP code for a handlebars token |
||
301 | * |
||
302 | * @param array<string,array|boolean> $info parsed information |
||
303 | * @param array<string,array|string|integer> $context current compile context |
||
304 | * |
||
305 | * @return string Return compiled code segment for the token |
||
306 | */ |
||
307 | 530 | protected static function compileToken($info, &$context) { |
|
336 | |||
337 | /** |
||
338 | * handle partial |
||
339 | * |
||
340 | * @param array<string,array|string|integer> $context current compile context |
||
341 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
342 | * |
||
343 | * @return string Return compiled code segment for the partial |
||
344 | */ |
||
345 | 64 | public static function partial(&$context, $vars) { |
|
367 | |||
368 | /** |
||
369 | * Return compiled PHP code for a handlebars inverted section begin token |
||
370 | * |
||
371 | * @param array<string,array|string|integer> $context current compile context |
||
372 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
373 | * |
||
374 | * @return string Return compiled code segment for the token |
||
375 | */ |
||
376 | 37 | protected static function invertedSection(&$context, $vars) { |
|
380 | |||
381 | /** |
||
382 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
383 | * |
||
384 | * @param array<string,array|string|integer> $context current compile context |
||
385 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
386 | * @param boolean $inverted the logic will be inverted |
||
387 | * |
||
388 | * @return string Return compiled code segment for the token |
||
389 | */ |
||
390 | 52 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
400 | |||
401 | /** |
||
402 | * Return compiled PHP code for a handlebars block end 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 | 283 | protected static function blockEnd(&$context, $vars) { |
|
440 | |||
441 | /** |
||
442 | * Return compiled PHP code for a handlebars block begin 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 | * |
||
447 | * @return string Return compiled code segment for the token |
||
448 | */ |
||
449 | 209 | protected static function blockBegin(&$context, $vars) { |
|
469 | |||
470 | /** |
||
471 | * compile {{#foo}} token |
||
472 | * |
||
473 | * @param array<string,array|string|integer> $context current compile context |
||
474 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
475 | * @param boolean $isEach the section is #each |
||
476 | * |
||
477 | * @return string|null Return compiled code segment for the token |
||
478 | */ |
||
479 | 152 | protected static function section(&$context, $vars, $isEach = false) { |
|
490 | |||
491 | /** |
||
492 | * compile {{with}} token |
||
493 | * |
||
494 | * @param array<string,array|string|integer> $context current compile context |
||
495 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
496 | * |
||
497 | * @return string|null Return compiled code segment for the token |
||
498 | */ |
||
499 | 15 | protected static function with(&$context, $vars) { |
|
503 | |||
504 | /** |
||
505 | * Return compiled PHP code for a handlebars custom helper token |
||
506 | * |
||
507 | * @param array<string,array|string|integer> $context current compile context |
||
508 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
509 | * @param boolean $raw is this {{{ token or not |
||
510 | * |
||
511 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
512 | */ |
||
513 | 392 | protected static function customHelper(&$context, $vars, $raw) { |
|
525 | |||
526 | /** |
||
527 | * Return compiled PHP code for a handlebars else token |
||
528 | * |
||
529 | * @param array<string,array|string|integer> $context current compile context |
||
530 | * |
||
531 | * @return string Return compiled code segment for the token when the token is else |
||
532 | */ |
||
533 | 46 | protected static function doElse(&$context) { |
|
543 | |||
544 | /** |
||
545 | * Return compiled PHP code for a handlebars lookup token |
||
546 | * |
||
547 | * @param array<string,array|string|integer> $context current compile context |
||
548 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
549 | * @param boolean $raw is this {{{ token or not |
||
550 | * |
||
551 | * @return string Return compiled code segment for the token |
||
552 | */ |
||
553 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
562 | |||
563 | /** |
||
564 | * Return compiled PHP code for a handlebars variable token |
||
565 | * |
||
566 | * @param array<string,array|string|integer> $context current compile context |
||
567 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
568 | * @param boolean $raw is this {{{ token or not |
||
569 | * |
||
570 | * @return string Return compiled code segment for the token |
||
571 | */ |
||
572 | 317 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
580 | |||
581 | /** |
||
582 | * Add usage count to context |
||
583 | * |
||
584 | * @param array<string,array|string|integer> $context current context |
||
585 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
586 | * @param string $name used name |
||
587 | * @param integer $count increment |
||
588 | * |
||
589 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
590 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
591 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
592 | */ |
||
593 | 506 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
599 | } |
||
600 | |||
601 |
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.