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) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * For {{#if}} . |
||
| 222 | * |
||
| 223 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 224 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 225 | * @param boolean $zero include zero as true |
||
| 226 | * |
||
| 227 | * @return boolean Return true when the value is not null nor false. |
||
| 228 | * |
||
| 229 | * @expect false when input array(), null, false |
||
| 230 | * @expect false when input array(), 0, false |
||
| 231 | * @expect true when input array(), 0, true |
||
| 232 | * @expect false when input array(), false, false |
||
| 233 | * @expect true when input array(), true, false |
||
| 234 | * @expect true when input array(), 1, false |
||
| 235 | * @expect false when input array(), '', false |
||
| 236 | * @expect false when input array(), array(), false |
||
| 237 | * @expect true when input array(), array(''), false |
||
| 238 | * @expect true when input array(), array(0), false |
||
| 239 | */ |
||
| 240 | 74 | public static function ifvar($cx, $v, $zero) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * For {{^var}} . |
||
| 247 | * |
||
| 248 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 249 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 250 | * |
||
| 251 | * @return boolean Return true when the value is not null nor false. |
||
| 252 | * |
||
| 253 | * @expect true when input array(), null |
||
| 254 | * @expect false when input array(), 0 |
||
| 255 | * @expect true when input array(), false |
||
| 256 | * @expect false when input array(), 'false' |
||
| 257 | * @expect true when input array(), array() |
||
| 258 | * @expect false when input array(), array('1') |
||
| 259 | */ |
||
| 260 | 37 | public static function isec($cx, $v) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * For {{var}} . |
||
| 267 | * |
||
| 268 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 269 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 270 | * |
||
| 271 | * @return string The htmlencoded value of the specified variable |
||
| 272 | * |
||
| 273 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 274 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 275 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 276 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
| 277 | */ |
||
| 278 | 46 | public static function enc($cx, $var) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * For {{var}} , do html encode just like handlebars.js . |
||
| 289 | * |
||
| 290 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 291 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 292 | * |
||
| 293 | * @return string The htmlencoded value of the specified variable |
||
| 294 | * |
||
| 295 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 296 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 297 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 298 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 299 | */ |
||
| 300 | 341 | public static function encq($cx, $var) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * For {{#var}} or {{#each}} . |
||
| 311 | * |
||
| 312 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 313 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 314 | * @param array<string>|null $bp block parameters |
||
| 315 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 316 | * @param boolean $each true when rendering #each |
||
| 317 | * @param Closure $cb callback function to render child context |
||
| 318 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 319 | * |
||
| 320 | * @return string The rendered string of the section |
||
| 321 | * |
||
| 322 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 323 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 324 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 325 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 326 | * @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=";} |
||
| 327 | * @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=";} |
||
| 328 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 329 | * @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=";} |
||
| 330 | * @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);} |
||
| 331 | * @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);} |
||
| 332 | * @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);} |
||
| 333 | * @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);} |
||
| 334 | * @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';} |
||
| 335 | * @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';} |
||
| 336 | * @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';} |
||
| 337 | * @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';} |
||
| 338 | * @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';} |
||
| 339 | * @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';} |
||
| 340 | * @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';} |
||
| 341 | * @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';} |
||
| 342 | * @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';} |
||
| 343 | * @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';} |
||
| 344 | * @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;} |
||
| 345 | * @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'];} |
||
| 346 | */ |
||
| 347 | 170 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * For {{#with}} . |
||
| 463 | * |
||
| 464 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 465 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 466 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 467 | * @param array<string>|null $bp block parameters |
||
| 468 | * @param Closure $cb callback function to render child context |
||
| 469 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 470 | * |
||
| 471 | * @return string The rendered string of the token |
||
| 472 | * |
||
| 473 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 474 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 475 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 476 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 477 | */ |
||
| 478 | 30 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get merged context. |
||
| 498 | * |
||
| 499 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 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 | 103 | public static function m($cx, $a, $b) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * For {{> partial}} . |
||
| 527 | * |
||
| 528 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 529 | * @param string $p partial name |
||
| 530 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 531 | * |
||
| 532 | * @return string The rendered string of the partial |
||
| 533 | * |
||
| 534 | */ |
||
| 535 | 100 | public static function p($cx, $p, $v, $pid, $sp = '') |
|
| 548 | |||
| 549 | /** |
||
| 550 | * For {{#* inlinepartial}} . |
||
| 551 | * |
||
| 552 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 553 | * @param string $p partial name |
||
| 554 | * @param Closure $code the compiled partial code |
||
| 555 | * |
||
| 556 | */ |
||
| 557 | 9 | public static function in(&$cx, $p, $code) |
|
| 561 | |||
| 562 | /* For single custom helpers. |
||
| 563 | * |
||
| 564 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 565 | * @param string $ch the name of custom helper to be executed |
||
| 566 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 567 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 568 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 569 | * |
||
| 570 | * @return string The rendered string of the token |
||
| 571 | */ |
||
| 572 | 120 | public static function hbch($cx, $ch, $vars, $op, &$_this) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * For block custom helpers. |
||
| 595 | * |
||
| 596 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 597 | * @param string $ch the name of custom helper to be executed |
||
| 598 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 599 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 600 | * @param boolean $inverted the logic will be inverted |
||
| 601 | * @param Closure|null $cb callback function to render child context |
||
| 602 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 603 | * |
||
| 604 | * @return string The rendered string of the token |
||
| 605 | */ |
||
| 606 | 57 | public static function hbbch($cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Execute custom helper with prepared options |
||
| 684 | * |
||
| 685 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 686 | * @param string $ch the name of custom helper to be executed |
||
| 687 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 688 | * @param array<string,array|string|integer> $options the options object |
||
| 689 | * |
||
| 690 | * @return string The rendered string of the token |
||
| 691 | */ |
||
| 692 | 167 | public static function exch($cx, $ch, $vars, &$options) |
|
| 706 | } |
||
| 707 |
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: