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 |
||
| 45 | class Runtime extends Encoder |
||
| 46 | { |
||
| 47 | const DEBUG_ERROR_LOG = 1; |
||
| 48 | const DEBUG_ERROR_EXCEPTION = 2; |
||
| 49 | const DEBUG_TAGS = 4; |
||
| 50 | const DEBUG_TAGS_ANSI = 12; |
||
| 51 | const DEBUG_TAGS_HTML = 20; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Output debug info. |
||
| 55 | * |
||
| 56 | * @param string $v expression |
||
| 57 | * @param string $f runtime function name |
||
| 58 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 59 | * |
||
| 60 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
| 61 | * @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';} |
||
| 62 | */ |
||
| 63 | 9 | public static function debug($v, $f, $cx) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Handle error by error_log or throw exception. |
||
| 102 | * |
||
| 103 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 104 | * @param string $err error message |
||
| 105 | * |
||
| 106 | * @throws \Exception |
||
| 107 | */ |
||
| 108 | 18 | public static function err($cx, $err) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Handle missing data error. |
||
| 121 | * |
||
| 122 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 123 | * @param string $v expression |
||
| 124 | */ |
||
| 125 | 5 | public static function miss($cx, $v) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * For {{log}} . |
||
| 132 | * |
||
| 133 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 134 | * @param string $v expression |
||
| 135 | */ |
||
| 136 | public static function lo($cx, $v) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
| 144 | * |
||
| 145 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 146 | * @param array|string|boolean|integer|double|null $in current context |
||
| 147 | * @param array<array|string|integer> $base current variable context |
||
| 148 | * @param array<string|integer> $path array of names for path |
||
| 149 | * @param array|null $args extra arguments for lambda |
||
| 150 | * |
||
| 151 | * @return null|string Return the value or null when not found |
||
| 152 | * |
||
| 153 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
| 154 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
| 155 | * @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') |
||
| 156 | * @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') |
||
| 157 | */ |
||
| 158 | 388 | public static function v($cx, $in, $base, $path, $args = null) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * For {{#if}} . |
||
| 230 | * |
||
| 231 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 232 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 233 | * @param boolean $zero include zero as true |
||
| 234 | * |
||
| 235 | * @return boolean Return true when the value is not null nor false. |
||
| 236 | * |
||
| 237 | * @expect false when input array(), null, false |
||
| 238 | * @expect false when input array(), 0, false |
||
| 239 | * @expect true when input array(), 0, true |
||
| 240 | * @expect false when input array(), false, false |
||
| 241 | * @expect true when input array(), true, false |
||
| 242 | * @expect true when input array(), 1, false |
||
| 243 | * @expect false when input array(), '', false |
||
| 244 | * @expect false when input array(), array(), false |
||
| 245 | * @expect true when input array(), array(''), false |
||
| 246 | * @expect true when input array(), array(0), false |
||
| 247 | */ |
||
| 248 | 74 | public static function ifvar($cx, $v, $zero) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * For {{^var}} . |
||
| 255 | * |
||
| 256 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 257 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 258 | * |
||
| 259 | * @return boolean Return true when the value is not null nor false. |
||
| 260 | * |
||
| 261 | * @expect true when input array(), null |
||
| 262 | * @expect false when input array(), 0 |
||
| 263 | * @expect true when input array(), false |
||
| 264 | * @expect false when input array(), 'false' |
||
| 265 | * @expect true when input array(), array() |
||
| 266 | * @expect false when input array(), array('1') |
||
| 267 | */ |
||
| 268 | 37 | public static function isec($cx, $v) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * For {{var}} . |
||
| 275 | * |
||
| 276 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 277 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 278 | * |
||
| 279 | * @return string The htmlencoded value of the specified variable |
||
| 280 | * |
||
| 281 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 282 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 283 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 284 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
| 285 | */ |
||
| 286 | 46 | public static function enc($cx, $var) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * For {{var}} , do html encode just like handlebars.js . |
||
| 297 | * |
||
| 298 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 299 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 300 | * |
||
| 301 | * @return string The htmlencoded value of the specified variable |
||
| 302 | * |
||
| 303 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 304 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 305 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 306 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 307 | */ |
||
| 308 | 342 | public static function encq($cx, $var) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * For {{#var}} or {{#each}} . |
||
| 319 | * |
||
| 320 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 321 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 322 | * @param array<string>|null $bp block parameters |
||
| 323 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 324 | * @param boolean $each true when rendering #each |
||
| 325 | * @param Closure $cb callback function to render child context |
||
| 326 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 327 | * |
||
| 328 | * @return string The rendered string of the section |
||
| 329 | * |
||
| 330 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 331 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 332 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 333 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 334 | * @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=";} |
||
| 335 | * @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=";} |
||
| 336 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 337 | * @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=";} |
||
| 338 | * @expect 'b' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return print_r($i, true);} |
||
| 339 | * @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);} |
||
| 340 | * @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);} |
||
| 341 | * @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);} |
||
| 342 | * @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';} |
||
| 343 | * @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';} |
||
| 344 | * @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';} |
||
| 345 | * @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';} |
||
| 346 | * @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';} |
||
| 347 | * @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';} |
||
| 348 | * @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';} |
||
| 349 | * @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';} |
||
| 350 | * @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';} |
||
| 351 | * @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';} |
||
| 352 | * @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;} |
||
| 353 | * @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'];} |
||
| 354 | */ |
||
| 355 | 171 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * For {{#with}} . |
||
| 471 | * |
||
| 472 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 473 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 474 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 475 | * @param array<string>|null $bp block parameters |
||
| 476 | * @param Closure $cb callback function to render child context |
||
| 477 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 478 | * |
||
| 479 | * @return string The rendered string of the token |
||
| 480 | * |
||
| 481 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 482 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 483 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 484 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 485 | */ |
||
| 486 | 30 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Get merged context. |
||
| 506 | * |
||
| 507 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 508 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 509 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 510 | * |
||
| 511 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 512 | * |
||
| 513 | */ |
||
| 514 | 103 | public static function m($cx, $a, $b) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * For {{> partial}} . |
||
| 535 | * |
||
| 536 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 537 | * @param string $p partial name |
||
| 538 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 539 | * |
||
| 540 | * @return string The rendered string of the partial |
||
| 541 | * |
||
| 542 | */ |
||
| 543 | 100 | public static function p($cx, $p, $v, $pid, $sp = '') |
|
| 556 | |||
| 557 | /** |
||
| 558 | * For {{#* inlinepartial}} . |
||
| 559 | * |
||
| 560 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 561 | * @param string $p partial name |
||
| 562 | * @param Closure $code the compiled partial code |
||
| 563 | * |
||
| 564 | */ |
||
| 565 | 9 | public static function in(&$cx, $p, $code) |
|
| 569 | |||
| 570 | /* For single 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 string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 576 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 577 | * |
||
| 578 | * @return string The rendered string of the token |
||
| 579 | */ |
||
| 580 | 121 | public static function hbch(&$cx, $ch, $vars, $op, &$_this) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * For block custom helpers. |
||
| 603 | * |
||
| 604 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 605 | * @param string $ch the name of custom helper to be executed |
||
| 606 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 607 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 608 | * @param boolean $inverted the logic will be inverted |
||
| 609 | * @param Closure|null $cb callback function to render child context |
||
| 610 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 611 | * |
||
| 612 | * @return string The rendered string of the token |
||
| 613 | */ |
||
| 614 | 57 | public static function hbbch(&$cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Execute custom helper with prepared options |
||
| 692 | * |
||
| 693 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 694 | * @param string $ch the name of custom helper to be executed |
||
| 695 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 696 | * @param array<string,array|string|integer> $options the options object |
||
| 697 | * |
||
| 698 | * @return string The rendered string of the token |
||
| 699 | */ |
||
| 700 | 168 | public static function exch($cx, $ch, $vars, &$options) |
|
| 714 | } |
||
| 715 |
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: