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 |
||
42 | class Runtime extends Encoder |
||
43 | { |
||
44 | const DEBUG_ERROR_LOG = 1; |
||
45 | const DEBUG_ERROR_EXCEPTION = 2; |
||
46 | const DEBUG_TAGS = 4; |
||
47 | const DEBUG_TAGS_ANSI = 12; |
||
48 | const DEBUG_TAGS_HTML = 20; |
||
49 | |||
50 | /** |
||
51 | * Output debug info. |
||
52 | * |
||
53 | * @param string $v expression |
||
54 | * @param string $f runtime function name |
||
55 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
56 | * |
||
57 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
58 | * @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';} |
||
59 | */ |
||
60 | 9 | public static function debug($v, $f, $cx) { |
|
95 | |||
96 | /** |
||
97 | * Handle error by error_log or throw exception. |
||
98 | * |
||
99 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
100 | * @param string $err error message |
||
101 | * |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | 18 | public static function err($cx, $err) { |
|
113 | |||
114 | /** |
||
115 | * Handle missing data error. |
||
116 | * |
||
117 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
118 | * @param string $v expression |
||
119 | */ |
||
120 | 5 | public static function miss($cx, $v) { |
|
123 | |||
124 | /** |
||
125 | * For {{log}} . |
||
126 | * |
||
127 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
128 | * @param string $v expression |
||
129 | */ |
||
130 | public static function lo($cx, $v) { |
||
134 | |||
135 | /** |
||
136 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
137 | * |
||
138 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
139 | * @param array|string|boolean|integer|double|null $in current context |
||
140 | * @param array<array|string|integer> $base current variable context |
||
141 | * @param array<string|integer> $path array of names for path |
||
142 | * @param array|null $args extra arguments for lambda |
||
143 | * |
||
144 | * @return null|string Return the value or null when not found |
||
145 | * |
||
146 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
147 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
148 | * @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') |
||
149 | * @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') |
||
150 | */ |
||
151 | 388 | public static function v($cx, $in, $base, $path, $args = null) { |
|
211 | |||
212 | /** |
||
213 | * For {{#if}} . |
||
214 | * |
||
215 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
216 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
217 | * @param boolean $zero include zero as true |
||
218 | * |
||
219 | * @return boolean Return true when the value is not null nor false. |
||
220 | * |
||
221 | * @expect false when input array(), null, false |
||
222 | * @expect false when input array(), 0, false |
||
223 | * @expect true when input array(), 0, true |
||
224 | * @expect false when input array(), false, false |
||
225 | * @expect true when input array(), true, false |
||
226 | * @expect true when input array(), 1, false |
||
227 | * @expect false when input array(), '', false |
||
228 | * @expect false when input array(), array(), false |
||
229 | * @expect true when input array(), array(''), false |
||
230 | * @expect true when input array(), array(0), false |
||
231 | */ |
||
232 | 74 | public static function ifvar($cx, $v, $zero) { |
|
235 | |||
236 | /** |
||
237 | * For {{^var}} . |
||
238 | * |
||
239 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
240 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
241 | * |
||
242 | * @return boolean Return true when the value is not null nor false. |
||
243 | * |
||
244 | * @expect true when input array(), null |
||
245 | * @expect false when input array(), 0 |
||
246 | * @expect true when input array(), false |
||
247 | * @expect false when input array(), 'false' |
||
248 | * @expect true when input array(), array() |
||
249 | * @expect false when input array(), array('1') |
||
250 | */ |
||
251 | 37 | public static function isec($cx, $v) { |
|
254 | |||
255 | /** |
||
256 | * For {{var}} . |
||
257 | * |
||
258 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
259 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
260 | * |
||
261 | * @return string The htmlencoded value of the specified variable |
||
262 | * |
||
263 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
264 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
265 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
266 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
267 | */ |
||
268 | 46 | public static function enc($cx, $var) { |
|
275 | |||
276 | /** |
||
277 | * For {{var}} , do html encode just like handlebars.js . |
||
278 | * |
||
279 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
280 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
281 | * |
||
282 | * @return string The htmlencoded value of the specified variable |
||
283 | * |
||
284 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
285 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
286 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
287 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
288 | */ |
||
289 | 340 | public static function encq($cx, $var) { |
|
296 | |||
297 | /** |
||
298 | * For {{#var}} or {{#each}} . |
||
299 | * |
||
300 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
301 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
302 | * @param array<string>|null $bp block parameters |
||
303 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
304 | * @param boolean $each true when rendering #each |
||
305 | * @param Closure $cb callback function to render child context |
||
306 | * @param Closure|null $else callback function to render child context when {{else}} |
||
307 | * |
||
308 | * @return string The rendered string of the section |
||
309 | * |
||
310 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
311 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
312 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
313 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
314 | * @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=";} |
||
315 | * @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=";} |
||
316 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
317 | * @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=";} |
||
318 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
319 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 1, null, 1, false, function ($c, $i) {return print_r($i, true);} |
||
320 | * @expect '0' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return print_r($i, true);} |
||
321 | * @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);} |
||
322 | * @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';} |
||
323 | * @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';} |
||
324 | * @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';} |
||
325 | * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
326 | * @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';} |
||
327 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), '', null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
328 | * @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';} |
||
329 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
330 | * @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';} |
||
331 | * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), new stdClass, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} |
||
332 | * @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;} |
||
333 | * @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'];} |
||
334 | */ |
||
335 | 170 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
447 | |||
448 | /** |
||
449 | * For {{#with}} . |
||
450 | * |
||
451 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
452 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
453 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
454 | * @param array<string>|null $bp block parameters |
||
455 | * @param Closure $cb callback function to render child context |
||
456 | * @param Closure|null $else callback function to render child context when {{else}} |
||
457 | * |
||
458 | * @return string The rendered string of the token |
||
459 | * |
||
460 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
461 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
462 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
463 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
464 | */ |
||
465 | 30 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
481 | |||
482 | /** |
||
483 | * Get merged context. |
||
484 | * |
||
485 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
486 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
487 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
488 | * |
||
489 | * @return array<array|string|integer>|string|integer the merged context object |
||
490 | * |
||
491 | */ |
||
492 | 102 | public static function m($cx, $a, $b) { |
|
509 | |||
510 | /** |
||
511 | * For {{> partial}} . |
||
512 | * |
||
513 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
514 | * @param string $p partial name |
||
515 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
516 | * |
||
517 | * @return string The rendered string of the partial |
||
518 | * |
||
519 | */ |
||
520 | 99 | public static function p($cx, $p, $v, $pid, $sp = '') { |
|
532 | |||
533 | /** |
||
534 | * For {{#* inlinepartial}} . |
||
535 | * |
||
536 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
537 | * @param string $p partial name |
||
538 | * @param Closure $code the compiled partial code |
||
539 | * |
||
540 | */ |
||
541 | 7 | public static function in(&$cx, $p, $code) { |
|
544 | |||
545 | /* For single custom helpers. |
||
546 | * |
||
547 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
548 | * @param string $ch the name of custom helper to be executed |
||
549 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
550 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
551 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
552 | * |
||
553 | * @return string The rendered string of the token |
||
554 | */ |
||
555 | 120 | public static function hbch($cx, $ch, $vars, $op, &$_this) { |
|
574 | |||
575 | /** |
||
576 | * For block custom helpers. |
||
577 | * |
||
578 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
579 | * @param string $ch the name of custom helper to be executed |
||
580 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
581 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
582 | * @param boolean $inverted the logic will be inverted |
||
583 | * @param Closure|null $cb callback function to render child context |
||
584 | * @param Closure|null $else callback function to render child context when {{else}} |
||
585 | * |
||
586 | * @return string The rendered string of the token |
||
587 | */ |
||
588 | 57 | public static function hbbch($cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) { |
|
662 | |||
663 | /** |
||
664 | * Execute custom helper with prepared options |
||
665 | * |
||
666 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
667 | * @param string $ch the name of custom helper to be executed |
||
668 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
669 | * @param array<string,array|string|integer> $options the options object |
||
670 | * |
||
671 | * @return string The rendered string of the token |
||
672 | */ |
||
673 | 167 | public static function exch($cx, $ch, $vars, &$options) { |
|
691 | } |
||
692 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: