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 | 715 | public static function compileTemplate(&$context, $template) { |
|
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 | 645 | 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'), 'test', '' |
||
152 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
153 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
154 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
155 | */ |
||
156 | 581 | 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 | 258 | 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 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
221 | |||
222 | /** |
||
223 | * Get string presentation of a subexpression or a variable |
||
224 | * |
||
225 | * @param array<array|string|integer> $context current compile context |
||
226 | * @param array<array|string|integer> $var variable parsed path |
||
227 | * |
||
228 | * @return array<string> variable names |
||
|
|||
229 | */ |
||
230 | 360 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
233 | |||
234 | /** |
||
235 | * Get string presentation of a variable |
||
236 | * |
||
237 | * @param array<array|string|integer> $var variable parsed path |
||
238 | * @param array<array|string|integer> $context current compile context |
||
239 | * @param array<string> $lookup extra lookup string as valid PHP variable name |
||
240 | * |
||
241 | * @return array<string> variable names |
||
242 | * |
||
243 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
244 | * @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') |
||
245 | * @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') |
||
246 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
247 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
248 | * @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') |
||
249 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
250 | * @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') |
||
251 | * @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') |
||
252 | * @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') |
||
253 | * @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') |
||
254 | * @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') |
||
255 | * @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"') |
||
256 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
257 | * @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') |
||
258 | * @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') |
||
259 | * @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') |
||
260 | * @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') |
||
261 | * @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') |
||
262 | */ |
||
263 | 576 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
308 | |||
309 | /** |
||
310 | * Return compiled PHP code for a handlebars token |
||
311 | * |
||
312 | * @param array<string,array|string|integer> $context current compile context |
||
313 | * @param array<string,array|boolean> $info parsed information |
||
314 | * |
||
315 | * @return string Return compiled code segment for the token |
||
316 | */ |
||
317 | 605 | protected static function compileToken(&$context, $info) { |
|
346 | |||
347 | /** |
||
348 | * handle partial |
||
349 | * |
||
350 | * @param array<string,array|string|integer> $context current compile context |
||
351 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
352 | * |
||
353 | * @return string Return compiled code segment for the partial |
||
354 | */ |
||
355 | 85 | public static function partial(&$context, $vars) { |
|
378 | |||
379 | /** |
||
380 | * handle inline partial |
||
381 | * |
||
382 | * @param array<string,array|string|integer> $context current compile context |
||
383 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
384 | * |
||
385 | * @return string Return compiled code segment for the partial |
||
386 | */ |
||
387 | 10 | public static function inline(&$context, $vars) { |
|
398 | |||
399 | /** |
||
400 | * Return compiled PHP code for a handlebars inverted section begin 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 | 38 | protected static function invertedSection(&$context, $vars) { |
|
411 | |||
412 | /** |
||
413 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
414 | * |
||
415 | * @param array<string,array|string|integer> $context current compile context |
||
416 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
417 | * @param boolean $inverted the logic will be inverted |
||
418 | * |
||
419 | * @return string Return compiled code segment for the token |
||
420 | */ |
||
421 | 61 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
432 | |||
433 | /** |
||
434 | * Return compiled PHP code for a handlebars block end token |
||
435 | * |
||
436 | * @param array<string,array|string|integer> $context current compile context |
||
437 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
438 | * @param string|null $matchop should also match to this operator |
||
439 | * |
||
440 | * @return string Return compiled code segment for the token |
||
441 | */ |
||
442 | 316 | protected static function blockEnd(&$context, $vars, $matchop = NULL) { |
|
473 | |||
474 | /** |
||
475 | * Return compiled PHP code for a handlebars block begin token |
||
476 | * |
||
477 | * @param array<string,array|string|integer> $context current compile context |
||
478 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
479 | * |
||
480 | * @return string Return compiled code segment for the token |
||
481 | */ |
||
482 | 232 | protected static function blockBegin(&$context, $vars) { |
|
502 | |||
503 | /** |
||
504 | * compile {{#foo}} token |
||
505 | * |
||
506 | * @param array<string,array|string|integer> $context current compile context |
||
507 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
508 | * @param boolean $isEach the section is #each |
||
509 | * |
||
510 | * @return string|null Return compiled code segment for the token |
||
511 | */ |
||
512 | 159 | protected static function section(&$context, $vars, $isEach = false) { |
|
533 | |||
534 | /** |
||
535 | * compile {{with}} token |
||
536 | * |
||
537 | * @param array<string,array|string|integer> $context current compile context |
||
538 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
539 | * |
||
540 | * @return string|null Return compiled code segment for the token |
||
541 | */ |
||
542 | 27 | protected static function with(&$context, $vars) { |
|
549 | |||
550 | /** |
||
551 | * Return compiled PHP code for a handlebars custom helper token |
||
552 | * |
||
553 | * @param array<string,array|string|integer> $context current compile context |
||
554 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
555 | * @param boolean $raw is this {{{ token or not |
||
556 | * |
||
557 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
558 | */ |
||
559 | 445 | protected static function customHelper(&$context, $vars, $raw) { |
|
571 | |||
572 | /** |
||
573 | * Return compiled PHP code for a handlebars else token |
||
574 | * |
||
575 | * @param array<string,array|string|integer> $context current compile context |
||
576 | * |
||
577 | * @return string Return compiled code segment for the token when the token is else |
||
578 | */ |
||
579 | 46 | protected static function doElse(&$context) { |
|
589 | |||
590 | /** |
||
591 | * Return compiled PHP code for a handlebars lookup token |
||
592 | * |
||
593 | * @param array<string,array|string|integer> $context current compile context |
||
594 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
595 | * @param boolean $raw is this {{{ token or not |
||
596 | * |
||
597 | * @return string Return compiled code segment for the token |
||
598 | */ |
||
599 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
608 | |||
609 | /** |
||
610 | * Return compiled PHP code for a handlebars variable token |
||
611 | * |
||
612 | * @param array<string,array|string|integer> $context current compile context |
||
613 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
614 | * @param boolean $raw is this {{{ token or not |
||
615 | * |
||
616 | * @return string Return compiled code segment for the token |
||
617 | */ |
||
618 | 357 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
631 | |||
632 | /** |
||
633 | * Add usage count to context |
||
634 | * |
||
635 | * @param array<string,array|string|integer> $context current context |
||
636 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
637 | * @param string $name used name |
||
638 | * @param integer $count increment |
||
639 | * |
||
640 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
641 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
642 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
643 | */ |
||
644 | 581 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
650 | } |
||
651 | |||
652 |
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.