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 |
||
| 27 | class Runtime extends Encoder |
||
| 28 | { |
||
| 29 | const DEBUG_ERROR_LOG = 1; |
||
| 30 | const DEBUG_ERROR_EXCEPTION = 2; |
||
| 31 | const DEBUG_TAGS = 4; |
||
| 32 | const DEBUG_TAGS_ANSI = 12; |
||
| 33 | const DEBUG_TAGS_HTML = 20; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Output debug info. |
||
| 37 | * |
||
| 38 | * @param string $v expression |
||
| 39 | * @param string $f runtime function name |
||
| 40 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 41 | * |
||
| 42 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
| 43 | * @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';} |
||
| 44 | */ |
||
| 45 | 9 | public static function debug($v, $f, $cx) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Handle error by error_log or throw exception. |
||
| 83 | * |
||
| 84 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 85 | * @param string $err error message |
||
| 86 | * |
||
| 87 | * @throws \Exception |
||
| 88 | */ |
||
| 89 | 12 | public static function err($cx, $err) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Handle missing data error. |
||
| 101 | * |
||
| 102 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 103 | * @param string $v expression |
||
| 104 | */ |
||
| 105 | 5 | public static function miss($cx, $v) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * For {{log}} . |
||
| 111 | * |
||
| 112 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 113 | * @param string $v expression |
||
| 114 | */ |
||
| 115 | public static function lo($cx, $v) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
| 122 | * |
||
| 123 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 124 | * @param array|string|boolean|integer|double|null $in current context |
||
| 125 | * @param array<array|string|integer> $base current variable context |
||
| 126 | * @param array<string|integer> $path array of names for path |
||
| 127 | * @param array|null $args extra arguments for lambda |
||
| 128 | * |
||
| 129 | * @return null|string Return the value or null when not found |
||
| 130 | * |
||
| 131 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
| 132 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
| 133 | * @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') |
||
| 134 | * @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') |
||
| 135 | */ |
||
| 136 | 386 | public static function v($cx, $in, $base, $path, $args = null) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * For {{#if}} . |
||
| 199 | * |
||
| 200 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 201 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 202 | * @param boolean $zero include zero as true |
||
| 203 | * |
||
| 204 | * @return boolean Return true when the value is not null nor false. |
||
| 205 | * |
||
| 206 | * @expect false when input array(), null, false |
||
| 207 | * @expect false when input array(), 0, false |
||
| 208 | * @expect true when input array(), 0, true |
||
| 209 | * @expect false when input array(), false, false |
||
| 210 | * @expect true when input array(), true, false |
||
| 211 | * @expect true when input array(), 1, false |
||
| 212 | * @expect false when input array(), '', false |
||
| 213 | * @expect false when input array(), array(), false |
||
| 214 | * @expect true when input array(), array(''), false |
||
| 215 | * @expect true when input array(), array(0), false |
||
| 216 | */ |
||
| 217 | 74 | public static function ifvar($cx, $v, $zero) { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * For {{^var}} . |
||
| 223 | * |
||
| 224 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 225 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 226 | * |
||
| 227 | * @return boolean Return true when the value is not null nor false. |
||
| 228 | * |
||
| 229 | * @expect true when input array(), null |
||
| 230 | * @expect false when input array(), 0 |
||
| 231 | * @expect true when input array(), false |
||
| 232 | * @expect false when input array(), 'false' |
||
| 233 | * @expect true when input array(), array() |
||
| 234 | * @expect false when input array(), array('1') |
||
| 235 | */ |
||
| 236 | 37 | public static function isec($cx, $v) { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * For {{var}} . |
||
| 242 | * |
||
| 243 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 244 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 245 | * |
||
| 246 | * @return string The htmlencoded value of the specified variable |
||
| 247 | * |
||
| 248 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 249 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 250 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 251 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
| 252 | */ |
||
| 253 | 45 | public static function enc($cx, $var) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * For {{var}} , do html encode just like handlebars.js . |
||
| 263 | * |
||
| 264 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 265 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 266 | * |
||
| 267 | * @return string The htmlencoded value of the specified variable |
||
| 268 | * |
||
| 269 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 270 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 271 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 272 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 273 | */ |
||
| 274 | 334 | public static function encq($cx, $var) { |
|
| 281 | |||
| 282 | /** |
||
| 283 | * For {{#var}} or {{#each}} . |
||
| 284 | * |
||
| 285 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 286 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 287 | * @param array<string>|null $bp block parameters |
||
| 288 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 289 | * @param boolean $each true when rendering #each |
||
| 290 | * @param Closure $cb callback function to render child context |
||
| 291 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 292 | * |
||
| 293 | * @return string The rendered string of the section |
||
| 294 | * |
||
| 295 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 296 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 297 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 298 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 299 | * @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=";} |
||
| 300 | * @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=";} |
||
| 301 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 302 | * @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=";} |
||
| 303 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
| 304 | * @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);} |
||
| 305 | * @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);} |
||
| 306 | * @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);} |
||
| 307 | * @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';} |
||
| 308 | * @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';} |
||
| 309 | * @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';} |
||
| 310 | * @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';} |
||
| 311 | * @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';} |
||
| 312 | * @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';} |
||
| 313 | * @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';} |
||
| 314 | * @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';} |
||
| 315 | * @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';} |
||
| 316 | * @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';} |
||
| 317 | * @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;} |
||
| 318 | * @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'];} |
||
| 319 | */ |
||
| 320 | 163 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
| 428 | |||
| 429 | /** |
||
| 430 | * For {{#with}} . |
||
| 431 | * |
||
| 432 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 433 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 434 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 435 | * @param array<string>|null $bp block parameters |
||
| 436 | * @param Closure $cb callback function to render child context |
||
| 437 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 438 | * |
||
| 439 | * @return string The rendered string of the token |
||
| 440 | * |
||
| 441 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 442 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 443 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 444 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 445 | */ |
||
| 446 | 28 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get merged context. |
||
| 465 | * |
||
| 466 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 467 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 468 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 469 | * |
||
| 470 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 471 | * |
||
| 472 | */ |
||
| 473 | 93 | public static function m($cx, $a, $b) { |
|
| 487 | |||
| 488 | /** |
||
| 489 | * For {{> partial}} . |
||
| 490 | * |
||
| 491 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 492 | * @param string $p partial name |
||
| 493 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 494 | * |
||
| 495 | * @return string The rendered string of the partial |
||
| 496 | * |
||
| 497 | */ |
||
| 498 | 88 | public static function p($cx, $p, $v, $pid, $sp = '') { |
|
| 510 | |||
| 511 | /** |
||
| 512 | * For {{#* inlinepartial}} . |
||
| 513 | * |
||
| 514 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 515 | * @param string $p partial name |
||
| 516 | * @param Closure $code the compiled partial code |
||
| 517 | * |
||
| 518 | */ |
||
| 519 | 7 | public static function in(&$cx, $p, $code) { |
|
| 522 | |||
| 523 | /* For single custom helpers. |
||
| 524 | * |
||
| 525 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 526 | * @param string $ch the name of custom helper to be executed |
||
| 527 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 528 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 529 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 530 | * |
||
| 531 | * @return string The rendered string of the token |
||
| 532 | */ |
||
| 533 | 119 | public static function hbch($cx, $ch, $vars, $op, &$_this) { |
|
| 568 | |||
| 569 | /** |
||
| 570 | * For block custom helpers. |
||
| 571 | * |
||
| 572 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 573 | * @param string $ch the name of custom helper to be executed |
||
| 574 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 575 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 576 | * @param boolean $inverted the logic will be inverted |
||
| 577 | * @param Closure|null $cb callback function to render child context |
||
| 578 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 579 | * |
||
| 580 | * @return string The rendered string of the token |
||
| 581 | */ |
||
| 582 | 56 | public static function hbbch($cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) { |
|
| 671 | } |
||
| 672 | |||
| 673 |
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: