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 |
||
25 | class Compiler extends Validator |
||
26 | { |
||
27 | public static $lastParsed; |
||
28 | |||
29 | /** |
||
30 | * Compile template into PHP code |
||
31 | * |
||
32 | * @param array<string,array|string|integer> $context Current context |
||
33 | * @param string $template handlebars template |
||
34 | * |
||
35 | * @return string|null generated PHP code |
||
36 | */ |
||
37 | 795 | public static function compileTemplate(&$context, $template) |
|
69 | |||
70 | /** |
||
71 | * Compose LightnCandy render codes for include() |
||
72 | * |
||
73 | * @param array<string,array|string|integer> $context Current context |
||
74 | * @param string $code generated PHP code |
||
75 | * |
||
76 | * @return string Composed PHP code |
||
77 | */ |
||
78 | 717 | public static function composePHPRender($context, $code) |
|
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', 'runtimealias' => 'LR'), 'test', '' |
||
151 | * @expect 'LL::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', '' |
||
152 | * @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 0, 'funcprefix' => 'lala_abc'), 'test3', '' |
||
153 | * @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc' |
||
154 | */ |
||
155 | 649 | 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 | 297 | protected static function getVariableNames(&$context, $vn, $blockParams = null) |
|
199 | |||
200 | /** |
||
201 | * Get string presentation of a sub expression |
||
202 | * |
||
203 | * @param array<string,array|string|integer> $context current compile context |
||
204 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
205 | * |
||
206 | * @return array<string> code representing passed expression |
||
207 | */ |
||
208 | 46 | public static function compileSubExpression(&$context, $vars) |
|
218 | |||
219 | /** |
||
220 | * Get string presentation of a subexpression or a variable |
||
221 | * |
||
222 | * @param array<array|string|integer> $context current compile context |
||
223 | * @param array<array|string|integer> $var variable parsed path |
||
224 | * |
||
225 | * @return array<string> variable names |
||
226 | */ |
||
227 | 418 | protected static function getVariableNameOrSubExpression(&$context, $var) |
|
231 | |||
232 | /** |
||
233 | * Get string presentation of a variable |
||
234 | * |
||
235 | * @param array<array|string|integer> $var variable parsed path |
||
236 | * @param array<array|string|integer> $context current compile context |
||
237 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
238 | * |
||
239 | * @return array<string> variable names |
||
240 | * |
||
241 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
242 | * @expect array('(($inary && 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') |
||
243 | * @expect array('(($inary && 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') |
||
244 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
245 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
246 | * @expect array('(($inary && 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') |
||
247 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
248 | * @expect array('(($inary && 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') |
||
249 | * @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') |
||
250 | * @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') |
||
251 | * @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') |
||
252 | * @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') |
||
253 | * @expect array('(($inary && 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"') |
||
254 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
255 | * @expect array('(($inary && 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') |
||
256 | * @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') |
||
257 | * @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') |
||
258 | * @expect array('(($inary && 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') |
||
259 | * @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') |
||
260 | */ |
||
261 | 642 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) |
|
335 | |||
336 | /** |
||
337 | * Return compiled PHP code for a handlebars token |
||
338 | * |
||
339 | * @param array<string,array|string|integer> $context current compile context |
||
340 | * @param array<string,array|boolean> $info parsed information |
||
341 | * |
||
342 | * @return string Return compiled code segment for the token |
||
343 | */ |
||
344 | 677 | protected static function compileToken(&$context, $info) |
|
372 | |||
373 | /** |
||
374 | * handle partial |
||
375 | * |
||
376 | * @param array<string,array|string|integer> $context current compile context |
||
377 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
378 | * |
||
379 | * @return string Return compiled code segment for the partial |
||
380 | */ |
||
381 | 110 | public static function partial(&$context, $vars) |
|
402 | |||
403 | /** |
||
404 | * handle inline partial |
||
405 | * |
||
406 | * @param array<string,array|string|integer> $context current compile context |
||
407 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
408 | * |
||
409 | * @return string Return compiled code segment for the partial |
||
410 | */ |
||
411 | 13 | public static function inline(&$context, $vars) |
|
423 | |||
424 | /** |
||
425 | * Return compiled PHP code for a handlebars inverted section 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 | * |
||
430 | * @return string Return compiled code segment for the token |
||
431 | */ |
||
432 | 38 | protected static function invertedSection(&$context, $vars) |
|
437 | |||
438 | /** |
||
439 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
440 | * |
||
441 | * @param array<string,array|string|integer> $context current compile context |
||
442 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
443 | * @param boolean $inverted the logic will be inverted |
||
444 | * |
||
445 | * @return string Return compiled code segment for the token |
||
446 | */ |
||
447 | 64 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) |
|
457 | |||
458 | /** |
||
459 | * Return compiled PHP code for a handlebars block end token |
||
460 | * |
||
461 | * @param array<string,array|string|integer> $context current compile context |
||
462 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
463 | * @param string|null $matchop should also match to this operator |
||
464 | * |
||
465 | * @return string Return compiled code segment for the token |
||
466 | */ |
||
467 | 346 | protected static function blockEnd(&$context, &$vars, $matchop = null) |
|
500 | |||
501 | /** |
||
502 | * Return compiled PHP code for a handlebars block begin token |
||
503 | * |
||
504 | * @param array<string,array|string|integer> $context current compile context |
||
505 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
506 | * |
||
507 | * @return string Return compiled code segment for the token |
||
508 | */ |
||
509 | 261 | protected static function blockBegin(&$context, $vars) |
|
530 | |||
531 | /** |
||
532 | * compile {{#foo}} token |
||
533 | * |
||
534 | * @param array<string,array|string|integer> $context current compile context |
||
535 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
536 | * @param boolean $isEach the section is #each |
||
537 | * |
||
538 | * @return string|null Return compiled code segment for the token |
||
539 | */ |
||
540 | 175 | protected static function section(&$context, $vars, $isEach = false) |
|
559 | |||
560 | /** |
||
561 | * compile {{with}} token |
||
562 | * |
||
563 | * @param array<string,array|string|integer> $context current compile context |
||
564 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
565 | * |
||
566 | * @return string|null Return compiled code segment for the token |
||
567 | */ |
||
568 | 32 | protected static function with(&$context, $vars) |
|
576 | |||
577 | /** |
||
578 | * Return compiled PHP code for a handlebars custom helper token |
||
579 | * |
||
580 | * @param array<string,array|string|integer> $context current compile context |
||
581 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
582 | * @param boolean $raw is this {{{ token or not |
||
583 | * @param boolean $nosep true to compile without seperator |
||
584 | * @param boolean $subExp true when compile for subexpression |
||
585 | * |
||
586 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
587 | */ |
||
588 | 494 | protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) |
|
611 | |||
612 | /** |
||
613 | * Return compiled PHP code for a handlebars else token |
||
614 | * |
||
615 | * @param array<string,array|string|integer> $context current compile context |
||
616 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
617 | * |
||
618 | * @return string Return compiled code segment for the token when the token is else |
||
619 | */ |
||
620 | 59 | protected static function doElse(&$context, $vars) |
|
632 | |||
633 | /** |
||
634 | * Return compiled PHP code for a handlebars log token |
||
635 | * |
||
636 | * @param array<string,array|string|integer> $context current compile context |
||
637 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
638 | * @param boolean $raw is this {{{ token or not |
||
639 | * |
||
640 | * @return string Return compiled code segment for the token |
||
641 | */ |
||
642 | 2 | protected static function compileLog(&$context, &$vars, $raw) |
|
648 | |||
649 | /** |
||
650 | * Return compiled PHP code for a handlebars lookup token |
||
651 | * |
||
652 | * @param array<string,array|string|integer> $context current compile context |
||
653 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
654 | * @param boolean $raw is this {{{ token or not |
||
655 | * @param boolean $nosep true to compile without seperator |
||
656 | * |
||
657 | * @return string Return compiled code segment for the token |
||
658 | */ |
||
659 | 8 | protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) |
|
672 | |||
673 | /** |
||
674 | * Return compiled PHP code for template output |
||
675 | * |
||
676 | * @param array<string,array|string|integer> $context current compile context |
||
677 | * @param string $variable PHP code for the variable |
||
678 | * @param string $expression normalized handlebars expression |
||
679 | * @param boolean $raw is this {{{ token or not |
||
680 | * @param boolean $nosep true to compile without seperator |
||
681 | * |
||
682 | * @return string Return compiled code segment for the token |
||
683 | */ |
||
684 | 491 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) |
|
693 | |||
694 | /** |
||
695 | * Return compiled PHP code for a handlebars variable token |
||
696 | * |
||
697 | * @param array<string,array|string|integer> $context current compile context |
||
698 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
699 | * @param boolean $raw is this {{{ token or not |
||
700 | * @param boolean $nosep true to compile without seperator |
||
701 | * |
||
702 | * @return string Return compiled code segment for the token |
||
703 | */ |
||
704 | 382 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) |
|
714 | |||
715 | /** |
||
716 | * Add usage count to context |
||
717 | * |
||
718 | * @param array<string,array|string|integer> $context current context |
||
719 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
720 | * @param string $name used name |
||
721 | * @param integer $count increment |
||
722 | * |
||
723 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
724 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
725 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
726 | */ |
||
727 | 649 | protected static function addUsageCount(&$context, $category, $name, $count = 1) |
|
734 | } |
||
735 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.