Complex classes like Runtime 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 Runtime, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Runtime |
||
27 | { |
||
28 | const DEBUG_ERROR_LOG = 1; |
||
29 | const DEBUG_ERROR_EXCEPTION = 2; |
||
30 | const DEBUG_TAGS = 4; |
||
31 | const DEBUG_TAGS_ANSI = 12; |
||
32 | const DEBUG_TAGS_HTML = 20; |
||
33 | |||
34 | /** |
||
35 | * LightnCandy runtime method for output debug info. |
||
36 | * |
||
37 | * @param string $v expression |
||
38 | * @param string $f runtime function name |
||
39 | * @param array<string,array|string|integer> $cx render time context |
||
40 | * |
||
41 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
42 | * @expect '<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->' when input '123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML), 'runtime' => 'LightnCandy\\Runtime'), false, null, false, function () {return 'A';} |
||
43 | */ |
||
44 | 9 | public static function debug($v, $f, $cx) { |
|
74 | |||
75 | /** |
||
76 | * LightnCandy runtime method for error |
||
77 | * |
||
78 | * @param array<string,array|string|integer> $cx render time context |
||
79 | * @param string $err error message |
||
80 | * |
||
81 | * @throws \Exception |
||
82 | */ |
||
83 | 11 | public static function err($cx, $err) { |
|
92 | |||
93 | /** |
||
94 | * LightnCandy runtime method for missing data error. |
||
95 | * |
||
96 | * @param array<string,array|string|integer> $cx render time context |
||
97 | * @param string $v expression |
||
98 | */ |
||
99 | 5 | public static function miss($cx, $v) { |
|
102 | |||
103 | /** |
||
104 | * LightnCandy runtime method for log. |
||
105 | * |
||
106 | * @param array<string,array|string|integer> $cx render time context |
||
107 | * @param string $v expression |
||
108 | */ |
||
109 | public static function lo($cx, $v) { |
||
113 | |||
114 | /** |
||
115 | * LightnCandy runtime method for variable lookup. It is slower and only be used for instance property or method detection or lambdas. |
||
116 | * |
||
117 | * @param array<string,array|string|integer> $cx render time context |
||
118 | * @param array|string|boolean|integer|double|null $in current context |
||
119 | * @param array<array|string|integer> $base current variable context |
||
120 | * @param array<string|integer> $path array of names for path |
||
121 | * @param array|null $args extra arguments for lambda |
||
122 | * |
||
123 | * @return null|string Return the value or null when not found |
||
124 | * |
||
125 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
126 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
127 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b') |
||
128 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 1, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b') |
||
129 | */ |
||
130 | 385 | public static function v($cx, $in, $base, $path, $args = null) { |
|
184 | |||
185 | /** |
||
186 | * LightnCandy runtime method for {{#if var}}. |
||
187 | * |
||
188 | * @param array<string,array|string|integer> $cx render time context |
||
189 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
190 | * @param boolean $zero include zero as true |
||
191 | * |
||
192 | * @return boolean Return true when the value is not null nor false. |
||
193 | * |
||
194 | * @expect false when input array(), null, false |
||
195 | * @expect false when input array(), 0, false |
||
196 | * @expect true when input array(), 0, true |
||
197 | * @expect false when input array(), false, false |
||
198 | * @expect true when input array(), true, false |
||
199 | * @expect true when input array(), 1, false |
||
200 | * @expect false when input array(), '', false |
||
201 | * @expect false when input array(), array(), false |
||
202 | * @expect true when input array(), array(''), false |
||
203 | * @expect true when input array(), array(0), false |
||
204 | */ |
||
205 | 64 | public static function ifvar($cx, $v, $zero) { |
|
208 | |||
209 | /** |
||
210 | * LightnCandy runtime method for {{^var}} inverted section. |
||
211 | * |
||
212 | * @param array<string,array|string|integer> $cx render time context |
||
213 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
214 | * |
||
215 | * @return boolean Return true when the value is not null nor false. |
||
216 | * |
||
217 | * @expect true when input array(), null |
||
218 | * @expect false when input array(), 0 |
||
219 | * @expect true when input array(), false |
||
220 | * @expect false when input array(), 'false' |
||
221 | * @expect true when input array(), array() |
||
222 | * @expect false when input array(), array('1') |
||
223 | */ |
||
224 | 37 | public static function isec($cx, $v) { |
|
227 | |||
228 | /** |
||
229 | * LightnCandy runtime method for {{{var}}} . |
||
230 | * |
||
231 | * @param array<string,array|string|integer> $cx render time context |
||
232 | * @param array<array|string|integer>|string|integer|null $v value to be output |
||
233 | * |
||
234 | * @return string The raw value of the specified variable |
||
235 | * |
||
236 | * @expect true when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), true |
||
237 | * @expect 'true' when input array('flags' => array('jstrue' => 1)), true |
||
238 | * @expect '' when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), false |
||
239 | * @expect 'false' when input array('flags' => array('jstrue' => 1)), false |
||
240 | * @expect 'false' when input array('flags' => array('jstrue' => 1)), false, true |
||
241 | * @expect 'Array' when input array('flags' => array('jstrue' => 1, 'jsobj' => 0)), array('a', 'b') |
||
242 | * @expect 'a,b' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', 'b') |
||
243 | * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('a', 'c' => 'b') |
||
244 | * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('c' => 'b') |
||
245 | * @expect 'a,true' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', true) |
||
246 | * @expect 'a,1' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',true) |
||
247 | * @expect 'a,' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false) |
||
248 | * @expect 'a,false' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false) |
||
249 | */ |
||
250 | 313 | public static function raw($cx, $v) { |
|
281 | |||
282 | /** |
||
283 | * LightnCandy runtime method for {{var}} . |
||
284 | * |
||
285 | * @param array<string,array|string|integer> $cx render time context |
||
286 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
287 | * |
||
288 | * @return string The htmlencoded value of the specified variable |
||
289 | * |
||
290 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
291 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
292 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
293 | */ |
||
294 | 44 | public static function enc($cx, $var) { |
|
297 | |||
298 | /** |
||
299 | * LightnCandy runtime method for {{var}} , and deal with single quote to same as handlebars.js . |
||
300 | * |
||
301 | * @param array<string,array|string|integer> $cx render time context |
||
302 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
303 | * |
||
304 | * @return string The htmlencoded value of the specified variable |
||
305 | * |
||
306 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
307 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
308 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
309 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
310 | */ |
||
311 | 240 | public static function encq($cx, $var) { |
|
314 | |||
315 | /** |
||
316 | * LightnCandy runtime method for {{#var}} section. |
||
317 | * |
||
318 | * @param array<string,array|string|integer> $cx render time context |
||
319 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
320 | * @param array<string>|null $bp block parameters |
||
321 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
322 | * @param boolean $each true when rendering #each |
||
323 | * @param Closure $cb callback function to render child context |
||
324 | * @param Closure|null $else callback function to render child context when {{else}} |
||
325 | * |
||
326 | * @return string The rendered string of the section |
||
327 | * |
||
328 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
329 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
330 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
331 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
332 | * @expect '-a=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a'), null, array('a'), false, function ($c, $i) {return "-$i=";} |
||
333 | * @expect '-a=-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a','b'), null, array('a','b'), false, function ($c, $i) {return "-$i=";} |
||
334 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
335 | * @expect '-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a' => 'b'), null, array('a' => 'b'), true, function ($c, $i) {return "-$i=";} |
||
336 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
337 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 1, null, 1, false, function ($c, $i) {return print_r($i, true);} |
||
338 | * @expect '0' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return print_r($i, true);} |
||
339 | * @expect '{"b":"c"}' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('b' => 'c'), null, array('b' => 'c'), false, function ($c, $i) {return json_encode($i);} |
||
340 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
341 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
342 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
343 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
344 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), '', null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
345 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), '', null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
346 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
347 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
348 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), new stdClass, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
349 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), new stdClass, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
350 | * @expect '268' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,4), null, 0, false, function ($c, $i) {return $i * 2;} |
||
351 | * @expect '038' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,'a'=>4), null, 0, true, function ($c, $i) {return $i * $c['sp_vars']['index'];} |
||
352 | */ |
||
353 | 160 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
465 | |||
466 | /** |
||
467 | * LightnCandy runtime method for {{#with var}} . |
||
468 | * |
||
469 | * @param array<string,array|string|integer> $cx render time context |
||
470 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
471 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
472 | * @param array<string>|null $bp block parameters |
||
473 | * @param Closure $cb callback function to render child context |
||
474 | * @param Closure|null $else callback function to render child context when {{else}} |
||
475 | * |
||
476 | * @return string The rendered string of the token |
||
477 | * |
||
478 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
479 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
480 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
481 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
482 | */ |
||
483 | 25 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
495 | |||
496 | /** |
||
497 | * LightnCandy runtime method to get merged context |
||
498 | * |
||
499 | * @param array<string,array|string|integer> $cx render time context |
||
500 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
501 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
502 | * |
||
503 | * @return array<array|string|integer>|string|integer the merged context object |
||
504 | * |
||
505 | */ |
||
506 | 82 | public static function m($cx, $a, $b) { |
|
520 | |||
521 | /** |
||
522 | * LightnCandy runtime method for {{> partial}} . |
||
523 | * |
||
524 | * @param array<string,array|string|integer> $cx render time context |
||
525 | * @param string $p partial name |
||
526 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
527 | * |
||
528 | * @return string The rendered string of the partial |
||
529 | * |
||
530 | */ |
||
531 | 77 | public static function p($cx, $p, $v, $sp = '') { |
|
539 | |||
540 | /** |
||
541 | * LightnCandy runtime method for inline partial. |
||
542 | * |
||
543 | * @param array<string,array|string|integer> $cx render time context |
||
544 | * @param string $p partial name |
||
545 | * @param Closure $code the compiled partial code |
||
546 | * |
||
547 | */ |
||
548 | 7 | public static function in(&$cx, $p, $code) { |
|
551 | |||
552 | /** |
||
553 | * LightnCandy runtime method for custom helpers. |
||
554 | * |
||
555 | * @param array<string,array|string|integer> $cx render time context |
||
556 | * @param string $ch the name of custom helper to be executed |
||
557 | * @param array<array> $vars variables for the helper |
||
558 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
559 | * |
||
560 | * @return string The rendered string of the token |
||
561 | * |
||
562 | * @expect '---' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('-'),array()), 'raw' |
||
563 | * @expect '-&-' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('&'),array()), 'enc' |
||
564 | * @expect '-'-' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('\''),array()), 'encq' |
||
565 | * @expect '-b-' when input array('helpers' => array('a' => function ($i,$j) {return "-{$j['a']}-";})), 'a', array(array(),array('a' => 'b')), 'raw' |
||
566 | */ |
||
567 | 22 | public static function ch($cx, $ch, $vars, $op) { |
|
570 | |||
571 | /** |
||
572 | * LightnCandy runtime method to handle response of custom helpers. |
||
573 | * |
||
574 | * @param string|array<string,array|string|integer> $ret return value from custom helper |
||
575 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
576 | * |
||
577 | * @return string The rendered string of the token |
||
578 | * |
||
579 | * @expect '-&-' when input '-&-', 'raw' |
||
580 | * @expect '-&'-' when input '-&\'-', 'enc' |
||
581 | * @expect '-&'-' when input '-&\'-', 'encq' |
||
582 | * @expect '-&'-' when input array('-&\'-'), 'enc' |
||
583 | * @expect '-&'-' when input array('-&\'-'), 'encq' |
||
584 | * @expect '-&-' when input array('-&-', false), 'enc' |
||
585 | * @expect '-&-' when input array('-&-', false), 'raw' |
||
586 | * @expect '-&-' when input array('-&-', 'raw'), 'enc' |
||
587 | * @expect '-&'-' when input array('-&\'-', 'encq'), 'raw' |
||
588 | */ |
||
589 | 165 | public static function chret($ret, $op) { |
|
605 | |||
606 | /** |
||
607 | * LightnCandy runtime method for Handlebars.js style custom helpers. |
||
608 | * |
||
609 | * @param array<string,array|string|integer> $cx render time context |
||
610 | * @param string $ch the name of custom helper to be executed |
||
611 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
612 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
613 | * @param boolean $inverted the logic will be inverted |
||
614 | * @param Closure|null $cb callback function to render child context |
||
615 | * @param Closure|null $else callback function to render child context when {{else}} |
||
616 | * |
||
617 | * @return string The rendered string of the token |
||
618 | */ |
||
619 | 145 | public static function hbch($cx, $ch, $vars, $op, $inverted, $cb = null, $else = null) { |
|
717 | |||
718 | /** |
||
719 | * LightnCandy runtime method for block custom helpers. |
||
720 | * |
||
721 | * @param array<string,array|string|integer> $cx render time context |
||
722 | * @param string $ch the name of custom helper to be executed |
||
723 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
724 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
725 | * @param boolean $inverted the logic will be inverted |
||
726 | * @param Closure $cb callback function to render child context |
||
727 | * @param Closure|null $else callback function to render child context when {{else}} |
||
728 | * |
||
729 | * @return string The rendered string of the token |
||
730 | * |
||
731 | * @expect '4.2.3' when input array('blockhelpers' => array('a' => function ($cx) {return array($cx,2,3);})), 'a', array(0, 0), 4, false, function($cx, $i) {return implode('.', $i);} |
||
732 | * @expect '2.6.5' when input array('blockhelpers' => array('a' => function ($cx,$in) {return array($cx,$in[0],5);})), 'a', array('6', 0), 2, false, function($cx, $i) {return implode('.', $i);} |
||
733 | * @expect '' when input array('blockhelpers' => array('a' => function ($cx,$in) {})), 'a', array('6', 0), 2, false, function($cx, $i) {return implode('.', $i);} |
||
734 | */ |
||
735 | 4 | public static function bch($cx, $ch, $vars, $in, $inverted, $cb, $else = null) { |
|
762 | } |
||
763 | |||
764 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.