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 |
||
| 28 | class Runtime extends Encoder |
||
| 29 | { |
||
| 30 | const DEBUG_ERROR_LOG = 1; |
||
| 31 | const DEBUG_ERROR_EXCEPTION = 2; |
||
| 32 | const DEBUG_TAGS = 4; |
||
| 33 | const DEBUG_TAGS_ANSI = 12; |
||
| 34 | const DEBUG_TAGS_HTML = 20; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Output debug info. |
||
| 38 | * |
||
| 39 | * @param string $v expression |
||
| 40 | * @param string $f runtime function name |
||
| 41 | * @param array<string,array|string|integer> $cx render time context |
||
| 42 | * |
||
| 43 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
| 44 | * @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';} |
||
| 45 | */ |
||
| 46 | 9 | public static function debug($v, $f, $cx) { |
|
| 47 | 9 | $params = array_slice(func_get_args(), 2); |
|
| 48 | 9 | $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params); |
|
| 49 | |||
| 50 | 8 | if ($cx['flags']['debug'] & static::DEBUG_TAGS) { |
|
| 51 | 5 | $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS); |
|
| 52 | 5 | $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS); |
|
| 53 | 5 | $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '') |
|
| 54 | 5 | . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : ''); |
|
| 55 | 5 | $ce = ($html ? '<!--))-->' : '') |
|
| 56 | 5 | . ($ansi ? "\033[0m" : ''); |
|
| 57 | switch ($f) { |
||
| 58 | 5 | case 'sec': |
|
| 59 | 3 | case 'wi': |
|
| 60 | 3 | if ($r == '') { |
|
| 61 | 3 | if ($ansi) { |
|
| 62 | 1 | $r = "\033[0;33mSKIPPED\033[0m"; |
|
| 63 | } |
||
| 64 | 3 | if ($html) { |
|
| 65 | 2 | $r = '<!--SKIPPED-->'; |
|
| 66 | } |
||
| 67 | } |
||
| 68 | 3 | return "$cs{{#{$v}}}$ce{$r}$cs{{/{$v}}}$ce"; |
|
| 69 | default: |
||
| 70 | 3 | return "$cs{{{$v}}}$ce"; |
|
| 71 | } |
||
| 72 | } else { |
||
| 73 | 3 | return $r; |
|
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Handle error by error_log or throw exception. |
||
| 79 | * |
||
| 80 | * @param array<string,array|string|integer> $cx render time context |
||
| 81 | * @param string $err error message |
||
| 82 | * |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | 12 | public static function err($cx, $err) { |
|
| 86 | 12 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_LOG) { |
|
| 87 | 2 | error_log($err); |
|
| 88 | 2 | return; |
|
| 89 | } |
||
| 90 | 10 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_EXCEPTION) { |
|
| 91 | 6 | throw new \Exception($err); |
|
| 92 | } |
||
| 93 | 4 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Handle missing data error. |
||
| 97 | * |
||
| 98 | * @param array<string,array|string|integer> $cx render time context |
||
| 99 | * @param string $v expression |
||
| 100 | */ |
||
| 101 | 5 | public static function miss($cx, $v) { |
|
| 104 | |||
| 105 | /** |
||
| 106 | * For {{log}} . |
||
| 107 | * |
||
| 108 | * @param array<string,array|string|integer> $cx render time context |
||
| 109 | * @param string $v expression |
||
| 110 | */ |
||
| 111 | public static function lo($cx, $v) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
| 118 | * |
||
| 119 | * @param array<string,array|string|integer> $cx render time context |
||
| 120 | * @param array|string|boolean|integer|double|null $in current context |
||
| 121 | * @param array<array|string|integer> $base current variable context |
||
| 122 | * @param array<string|integer> $path array of names for path |
||
| 123 | * @param array|null $args extra arguments for lambda |
||
| 124 | * |
||
| 125 | * @return null|string Return the value or null when not found |
||
| 126 | * |
||
| 127 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
| 128 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
| 129 | * @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') |
||
| 130 | * @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') |
||
| 131 | */ |
||
| 132 | 385 | public static function v($cx, $in, $base, $path, $args = null) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * For {{#if}} . |
||
| 189 | * |
||
| 190 | * @param array<string,array|string|integer> $cx render time context |
||
| 191 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 192 | * @param boolean $zero include zero as true |
||
| 193 | * |
||
| 194 | * @return boolean Return true when the value is not null nor false. |
||
| 195 | * |
||
| 196 | * @expect false when input array(), null, false |
||
| 197 | * @expect false when input array(), 0, false |
||
| 198 | * @expect true when input array(), 0, true |
||
| 199 | * @expect false when input array(), false, false |
||
| 200 | * @expect true when input array(), true, false |
||
| 201 | * @expect true when input array(), 1, false |
||
| 202 | * @expect false when input array(), '', false |
||
| 203 | * @expect false when input array(), array(), false |
||
| 204 | * @expect true when input array(), array(''), false |
||
| 205 | * @expect true when input array(), array(0), false |
||
| 206 | */ |
||
| 207 | 64 | public static function ifvar($cx, $v, $zero) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * For {{^var}} . |
||
| 213 | * |
||
| 214 | * @param array<string,array|string|integer> $cx render time context |
||
| 215 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 216 | * |
||
| 217 | * @return boolean Return true when the value is not null nor false. |
||
| 218 | * |
||
| 219 | * @expect true when input array(), null |
||
| 220 | * @expect false when input array(), 0 |
||
| 221 | * @expect true when input array(), false |
||
| 222 | * @expect false when input array(), 'false' |
||
| 223 | * @expect true when input array(), array() |
||
| 224 | * @expect false when input array(), array('1') |
||
| 225 | */ |
||
| 226 | 37 | public static function isec($cx, $v) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * For {{var}} . |
||
| 232 | * |
||
| 233 | * @param array<string,array|string|integer> $cx render time context |
||
| 234 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 235 | * |
||
| 236 | * @return string The htmlencoded value of the specified variable |
||
| 237 | * |
||
| 238 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 239 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 240 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 241 | * @expect 'a&b' when input null, new SafeString('a&b') |
||
| 242 | */ |
||
| 243 | 45 | public static function enc($cx, $var) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * For {{var}} , do html encode just like handlebars.js . |
||
| 253 | * |
||
| 254 | * @param array<string,array|string|integer> $cx render time context |
||
| 255 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 256 | * |
||
| 257 | * @return string The htmlencoded value of the specified variable |
||
| 258 | * |
||
| 259 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 260 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 261 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 262 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 263 | */ |
||
| 264 | 316 | public static function encq($cx, $var) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * For {{#var}} or {{#each}} . |
||
| 274 | * |
||
| 275 | * @param array<string,array|string|integer> $cx render time context |
||
| 276 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 277 | * @param array<string>|null $bp block parameters |
||
| 278 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 279 | * @param boolean $each true when rendering #each |
||
| 280 | * @param Closure $cb callback function to render child context |
||
| 281 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 282 | * |
||
| 283 | * @return string The rendered string of the section |
||
| 284 | * |
||
| 285 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 286 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 287 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 288 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 289 | * @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=";} |
||
| 290 | * @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=";} |
||
| 291 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 292 | * @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=";} |
||
| 293 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
| 294 | * @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);} |
||
| 295 | * @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);} |
||
| 296 | * @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);} |
||
| 297 | * @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';} |
||
| 298 | * @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';} |
||
| 299 | * @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';} |
||
| 300 | * @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';} |
||
| 301 | * @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';} |
||
| 302 | * @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';} |
||
| 303 | * @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';} |
||
| 304 | * @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';} |
||
| 305 | * @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';} |
||
| 306 | * @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';} |
||
| 307 | * @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;} |
||
| 308 | * @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'];} |
||
| 309 | */ |
||
| 310 | 158 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * For {{#with}} . |
||
| 421 | * |
||
| 422 | * @param array<string,array|string|integer> $cx render time context |
||
| 423 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 424 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 425 | * @param array<string>|null $bp block parameters |
||
| 426 | * @param Closure $cb callback function to render child context |
||
| 427 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 428 | * |
||
| 429 | * @return string The rendered string of the token |
||
| 430 | * |
||
| 431 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 432 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 433 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 434 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 435 | */ |
||
| 436 | 22 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Get merged context. |
||
| 451 | * |
||
| 452 | * @param array<string,array|string|integer> $cx render time context |
||
| 453 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 454 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 455 | * |
||
| 456 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 457 | * |
||
| 458 | */ |
||
| 459 | 82 | public static function m($cx, $a, $b) { |
|
| 473 | |||
| 474 | /** |
||
| 475 | * For {{> partial}} . |
||
| 476 | * |
||
| 477 | * @param array<string,array|string|integer> $cx render time context |
||
| 478 | * @param string $p partial name |
||
| 479 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 480 | * |
||
| 481 | * @return string The rendered string of the partial |
||
| 482 | * |
||
| 483 | */ |
||
| 484 | 78 | public static function p($cx, $p, $v, $sp = '') { |
|
| 492 | |||
| 493 | /** |
||
| 494 | * For {{#* inlinepartial}} . |
||
| 495 | * |
||
| 496 | * @param array<string,array|string|integer> $cx render time context |
||
| 497 | * @param string $p partial name |
||
| 498 | * @param Closure $code the compiled partial code |
||
| 499 | * |
||
| 500 | */ |
||
| 501 | 7 | public static function in(&$cx, $p, $code) { |
|
| 504 | |||
| 505 | /** |
||
| 506 | * For custom helpers. |
||
| 507 | * |
||
| 508 | * @param array<string,array|string|integer> $cx render time context |
||
| 509 | * @param string $ch the name of custom helper to be executed |
||
| 510 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 511 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 512 | * @param boolean $inverted the logic will be inverted |
||
| 513 | * @param Closure|null $cb callback function to render child context |
||
| 514 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 515 | * |
||
| 516 | * @return string The rendered string of the token |
||
| 517 | */ |
||
| 518 | 150 | public static function hbch($cx, $ch, $vars, $op, $inverted, $cb = null, $else = null) { |
|
| 616 | } |
||
| 617 | |||
| 618 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.