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 |
||
| 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) { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Handle error by error_log or throw exception. |
||
| 78 | * |
||
| 79 | * @param array<string,array|string|integer> $cx render time context |
||
| 80 | * @param string $err error message |
||
| 81 | * |
||
| 82 | * @throws \Exception |
||
| 83 | */ |
||
| 84 | 12 | public static function err($cx, $err) { |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Handle missing data error. |
||
| 96 | * |
||
| 97 | * @param array<string,array|string|integer> $cx render time context |
||
| 98 | * @param string $v expression |
||
| 99 | */ |
||
| 100 | 5 | public static function miss($cx, $v) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * For {{log}} . |
||
| 106 | * |
||
| 107 | * @param array<string,array|string|integer> $cx render time context |
||
| 108 | * @param string $v expression |
||
| 109 | */ |
||
| 110 | public static function lo($cx, $v) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
| 117 | * |
||
| 118 | * @param array<string,array|string|integer> $cx render time context |
||
| 119 | * @param array|string|boolean|integer|double|null $in current context |
||
| 120 | * @param array<array|string|integer> $base current variable context |
||
| 121 | * @param array<string|integer> $path array of names for path |
||
| 122 | * @param array|null $args extra arguments for lambda |
||
| 123 | * |
||
| 124 | * @return null|string Return the value or null when not found |
||
| 125 | * |
||
| 126 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
| 127 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
| 128 | * @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') |
||
| 129 | * @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') |
||
| 130 | */ |
||
| 131 | 385 | public static function v($cx, $in, $base, $path, $args = null) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * For {{#if}} . |
||
| 194 | * |
||
| 195 | * @param array<string,array|string|integer> $cx render time context |
||
| 196 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 197 | * @param boolean $zero include zero as true |
||
| 198 | * |
||
| 199 | * @return boolean Return true when the value is not null nor false. |
||
| 200 | * |
||
| 201 | * @expect false when input array(), null, false |
||
| 202 | * @expect false when input array(), 0, false |
||
| 203 | * @expect true when input array(), 0, true |
||
| 204 | * @expect false when input array(), false, false |
||
| 205 | * @expect true when input array(), true, false |
||
| 206 | * @expect true when input array(), 1, false |
||
| 207 | * @expect false when input array(), '', false |
||
| 208 | * @expect false when input array(), array(), false |
||
| 209 | * @expect true when input array(), array(''), false |
||
| 210 | * @expect true when input array(), array(0), false |
||
| 211 | */ |
||
| 212 | 74 | public static function ifvar($cx, $v, $zero) { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * For {{^var}} . |
||
| 218 | * |
||
| 219 | * @param array<string,array|string|integer> $cx render time context |
||
| 220 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 221 | * |
||
| 222 | * @return boolean Return true when the value is not null nor false. |
||
| 223 | * |
||
| 224 | * @expect true when input array(), null |
||
| 225 | * @expect false when input array(), 0 |
||
| 226 | * @expect true when input array(), false |
||
| 227 | * @expect false when input array(), 'false' |
||
| 228 | * @expect true when input array(), array() |
||
| 229 | * @expect false when input array(), array('1') |
||
| 230 | */ |
||
| 231 | 37 | public static function isec($cx, $v) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * For {{var}} . |
||
| 237 | * |
||
| 238 | * @param array<string,array|string|integer> $cx render time context |
||
| 239 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 240 | * |
||
| 241 | * @return string The htmlencoded value of the specified variable |
||
| 242 | * |
||
| 243 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 244 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 245 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 246 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
| 247 | */ |
||
| 248 | 45 | public static function enc($cx, $var) { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * For {{var}} , do html encode just like handlebars.js . |
||
| 258 | * |
||
| 259 | * @param array<string,array|string|integer> $cx render time context |
||
| 260 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 261 | * |
||
| 262 | * @return string The htmlencoded value of the specified variable |
||
| 263 | * |
||
| 264 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 265 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 266 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 267 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 268 | */ |
||
| 269 | 331 | public static function encq($cx, $var) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * For {{#var}} or {{#each}} . |
||
| 279 | * |
||
| 280 | * @param array<string,array|string|integer> $cx render time context |
||
| 281 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 282 | * @param array<string>|null $bp block parameters |
||
| 283 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 284 | * @param boolean $each true when rendering #each |
||
| 285 | * @param Closure $cb callback function to render child context |
||
| 286 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 287 | * |
||
| 288 | * @return string The rendered string of the section |
||
| 289 | * |
||
| 290 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 291 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 292 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 293 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 294 | * @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=";} |
||
| 295 | * @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=";} |
||
| 296 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 297 | * @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=";} |
||
| 298 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
| 299 | * @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);} |
||
| 300 | * @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);} |
||
| 301 | * @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);} |
||
| 302 | * @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';} |
||
| 303 | * @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';} |
||
| 304 | * @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';} |
||
| 305 | * @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';} |
||
| 306 | * @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';} |
||
| 307 | * @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';} |
||
| 308 | * @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';} |
||
| 309 | * @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';} |
||
| 310 | * @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';} |
||
| 311 | * @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';} |
||
| 312 | * @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;} |
||
| 313 | * @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'];} |
||
| 314 | */ |
||
| 315 | 162 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * For {{#with}} . |
||
| 426 | * |
||
| 427 | * @param array<string,array|string|integer> $cx render time context |
||
| 428 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 429 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 430 | * @param array<string>|null $bp block parameters |
||
| 431 | * @param Closure $cb callback function to render child context |
||
| 432 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 433 | * |
||
| 434 | * @return string The rendered string of the token |
||
| 435 | * |
||
| 436 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 437 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 438 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 439 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 440 | */ |
||
| 441 | 28 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Get merged context. |
||
| 460 | * |
||
| 461 | * @param array<string,array|string|integer> $cx render time context |
||
| 462 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 463 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 464 | * |
||
| 465 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 466 | * |
||
| 467 | */ |
||
| 468 | 92 | public static function m($cx, $a, $b) { |
|
| 482 | |||
| 483 | /** |
||
| 484 | * For {{> partial}} . |
||
| 485 | * |
||
| 486 | * @param array<string,array|string|integer> $cx render time context |
||
| 487 | * @param string $p partial name |
||
| 488 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 489 | * |
||
| 490 | * @return string The rendered string of the partial |
||
| 491 | * |
||
| 492 | */ |
||
| 493 | 88 | public static function p($cx, $p, $v, $pid, $sp = '') { |
|
| 505 | |||
| 506 | /** |
||
| 507 | * For {{#* inlinepartial}} . |
||
| 508 | * |
||
| 509 | * @param array<string,array|string|integer> $cx render time context |
||
| 510 | * @param string $p partial name |
||
| 511 | * @param Closure $code the compiled partial code |
||
| 512 | * |
||
| 513 | */ |
||
| 514 | 7 | public static function in(&$cx, $p, $code) { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * For custom helpers. |
||
| 520 | * |
||
| 521 | * @param array<string,array|string|integer> $cx render time context |
||
| 522 | * @param string $ch the name of custom helper to be executed |
||
| 523 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 524 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 525 | * @param boolean $inverted the logic will be inverted |
||
| 526 | * @param Closure|null $cb callback function to render child context |
||
| 527 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 528 | * |
||
| 529 | * @return string The rendered string of the token |
||
| 530 | */ |
||
| 531 | 163 | public static function hbch($cx, $ch, $vars, $op, $inverted, $cb = null, $else = null) { |
|
| 633 | } |
||
| 634 | |||
| 635 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.