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 |
||
| 26 | class Runtime |
||
| 27 | { |
||
| 28 | const DEBUG_ERROR_LOG = 1; |
||
| 29 | const DEBUG_ERROR_EXCEPTION = 2; |
||
| 30 | const DEBUG_TAGS = 4; |
||
| 31 | const DEBUG_TAGS_ANSI = 12; |
||
| 32 | const DEBUG_TAGS_HTML = 20; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * LightnCandy runtime method for output debug info. |
||
| 36 | * |
||
| 37 | * @param string $v expression |
||
| 38 | * @param string $f runtime function name |
||
| 39 | * @param array<string,array|string|integer> $cx render time context |
||
| 40 | * |
||
| 41 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
| 42 | * @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';} |
||
| 43 | */ |
||
| 44 | 9 | public static function debug($v, $f, $cx) { |
|
| 45 | 9 | $params = array_slice(func_get_args(), 2); |
|
| 46 | 9 | $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params); |
|
| 47 | |||
| 48 | 8 | if ($cx['flags']['debug'] & static::DEBUG_TAGS) { |
|
| 49 | 5 | $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS); |
|
| 50 | 5 | $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS); |
|
| 51 | 5 | $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '') |
|
| 52 | 5 | . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : ''); |
|
| 53 | 5 | $ce = ($html ? '<!--))-->' : '') |
|
| 54 | 5 | . ($ansi ? "\033[0m" : ''); |
|
| 55 | switch ($f) { |
||
| 56 | 5 | case 'sec': |
|
| 57 | case 'ifv': |
||
| 58 | case 'unl': |
||
| 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 | * LightnCandy runtime method for error |
||
| 79 | * |
||
| 80 | * @param array<string,array|string|integer> $cx render time context |
||
| 81 | * @param string $err error message |
||
| 82 | * |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | 8 | public static function err($cx, $err) { |
|
| 86 | 8 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_LOG) { |
|
| 87 | 2 | error_log($err); |
|
| 88 | 2 | return; |
|
| 89 | } |
||
| 90 | 6 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_EXCEPTION) { |
|
| 91 | 3 | throw new \Exception($err); |
|
| 92 | } |
||
| 93 | 3 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * LightnCandy runtime method for 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 | * LightnCandy runtime method for variable lookup. It is slower and only be used for instance property or method detection or lambdas. |
||
| 107 | * |
||
| 108 | * @param array<string,array|string|integer> $cx render time context |
||
| 109 | * @param array<array|string|integer> $base current variable context |
||
| 110 | * @param array<string|integer> $path array of names for path |
||
| 111 | * |
||
| 112 | * @return null|string Return the value or null when not found |
||
| 113 | * |
||
| 114 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), 0, array('a', 'b') |
||
| 115 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), array('a' => array('b' => 3)), array('a', 'b') |
||
| 116 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), (Object) array('a' => array('b' => 3)), array('a', 'b') |
||
| 117 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 1, 'method' => 0, 'mustlok' => 0)), (Object) array('a' => array('b' => 3)), array('a', 'b') |
||
| 118 | */ |
||
| 119 | 341 | public static function v($cx, $base, $path) { |
|
| 120 | 341 | $count = count($cx['scopes']); |
|
| 121 | 341 | while ($base) { |
|
| 122 | 312 | $v = $base; |
|
| 123 | 312 | foreach ($path as $name) { |
|
| 124 | 312 | if (is_array($v) && isset($v[$name])) { |
|
| 125 | 307 | $v = $v[$name]; |
|
| 126 | 307 | continue; |
|
| 127 | } |
||
| 128 | 32 | if (is_object($v)) { |
|
| 129 | 5 | if ($cx['flags']['prop'] && !($v instanceof \Closure) && isset($v->$name)) { |
|
| 130 | 3 | $v = $v->$name; |
|
| 131 | 3 | continue; |
|
| 132 | } |
||
| 133 | 5 | if ($cx['flags']['method'] && is_callable(array($v, $name))) { |
|
| 134 | 4 | $v = $v->$name(); |
|
| 135 | 4 | continue; |
|
| 136 | } |
||
| 137 | } |
||
| 138 | 30 | if ($cx['flags']['mustlok']) { |
|
| 139 | 27 | unset($v); |
|
| 140 | 27 | break; |
|
| 141 | } |
||
| 142 | 3 | return null; |
|
| 143 | } |
||
| 144 | 312 | if (isset($v)) { |
|
| 145 | 301 | if ($v instanceof \Closure) { |
|
| 146 | 12 | if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) { |
|
| 147 | 12 | $v = $v(); |
|
| 148 | } |
||
| 149 | } |
||
| 150 | 301 | return $v; |
|
| 151 | } |
||
| 152 | 27 | $count--; |
|
| 153 | switch ($count) { |
||
| 154 | 27 | case -1: |
|
| 155 | 26 | $base = $cx['sp_vars']['root']; |
|
| 156 | 26 | break; |
|
| 157 | 5 | case -2: |
|
| 158 | 25 | return null; |
|
| 159 | default: |
||
| 160 | 5 | $base = $cx['scopes'][$count]; |
|
| 161 | } |
||
| 162 | } |
||
| 163 | 33 | } |
|
| 164 | |||
| 165 | /** |
||
| 166 | * LightnCandy runtime method for {{#if var}}. |
||
| 167 | * |
||
| 168 | * @param array<string,array|string|integer> $cx render time context |
||
| 169 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 170 | * @param boolean $zero include zero as true |
||
| 171 | * |
||
| 172 | * @return boolean Return true when the value is not null nor false. |
||
| 173 | * |
||
| 174 | * @expect false when input array(), null, false |
||
| 175 | * @expect false when input array(), 0, false |
||
| 176 | * @expect true when input array(), 0, true |
||
| 177 | * @expect false when input array(), false, false |
||
| 178 | * @expect true when input array(), true, false |
||
| 179 | * @expect true when input array(), 1, false |
||
| 180 | * @expect false when input array(), '', false |
||
| 181 | * @expect false when input array(), array(), false |
||
| 182 | * @expect true when input array(), array(''), false |
||
| 183 | * @expect true when input array(), array(0), false |
||
| 184 | */ |
||
| 185 | 54 | public static function ifvar($cx, $v, $zero) { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * LightnCandy runtime method for {{#if var}} when {{../var}} used. |
||
| 191 | * |
||
| 192 | * @param array<string,array|string|integer> $cx render time context |
||
| 193 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 194 | * @param boolean $zero include zero as true |
||
| 195 | * @param array<array|string|integer> $in input data with current scope |
||
| 196 | * @param Closure|null $truecb callback function when test result is true |
||
| 197 | * @param Closure|null $falsecb callback function when test result is false |
||
| 198 | * |
||
| 199 | * @return string The rendered string of the section |
||
| 200 | * |
||
| 201 | * @expect '' when input array('scopes' => array()), null, false, array(), null |
||
| 202 | * @expect '' when input array('scopes' => array()), null, false, array(), function () {return 'Y';} |
||
| 203 | * @expect 'Y' when input array('scopes' => array()), 1, false, array(), function () {return 'Y';} |
||
| 204 | * @expect 'N' when input array('scopes' => array()), null, false, array(), function () {return 'Y';}, function () {return 'N';} |
||
| 205 | */ |
||
| 206 | 1 | public static function ifv($cx, $v, $zero, $in, $truecb, $falsecb = null) { |
|
| 207 | 1 | if (static::ifvar($cx, $v, $zero)) { |
|
| 208 | 1 | if ($truecb) { |
|
| 209 | 1 | return $truecb($cx, $in); |
|
| 210 | } |
||
| 211 | } else { |
||
| 212 | 1 | if ($falsecb) { |
|
| 213 | 1 | return $falsecb($cx, $in); |
|
| 214 | } |
||
| 215 | } |
||
| 216 | 1 | return ''; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * LightnCandy runtime method for {{#unless var}} when {{../var}} used. |
||
| 221 | * |
||
| 222 | * @param array<string,array|string|integer> $cx render time context |
||
| 223 | * @param array<array|string|integer>|string|integer|null $var value be tested |
||
| 224 | * @param boolean $zero include zero as true |
||
| 225 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 226 | * @param Closure $truecb callback function when test result is true |
||
| 227 | * @param Closure|null $falsecb callback function when test result is false |
||
| 228 | * |
||
| 229 | * @return string Return rendered string when the value is not null nor false. |
||
| 230 | * |
||
| 231 | * @expect '' when input array('scopes' => array()), null, false, array(), null |
||
| 232 | * @expect 'Y' when input array('scopes' => array()), null, false, array(), function () {return 'Y';} |
||
| 233 | * @expect '' when input array('scopes' => array()), 1, false, array(), function () {return 'Y';} |
||
| 234 | * @expect 'Y' when input array('scopes' => array()), null, false, array(), function () {return 'Y';}, function () {return 'N';} |
||
| 235 | * @expect 'N' when input array('scopes' => array()), true, false, array(), function () {return 'Y';}, function () {return 'N';} |
||
| 236 | */ |
||
| 237 | 1 | public static function unl($cx, $var, $zero, $in, $truecb, $falsecb = null) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * LightnCandy runtime method for {{^var}} inverted section. |
||
| 243 | * |
||
| 244 | * @param array<string,array|string|integer> $cx render time context |
||
| 245 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 246 | * |
||
| 247 | * @return boolean Return true when the value is not null nor false. |
||
| 248 | * |
||
| 249 | * @expect true when input array(), null |
||
| 250 | * @expect false when input array(), 0 |
||
| 251 | * @expect true when input array(), false |
||
| 252 | * @expect false when input array(), 'false' |
||
| 253 | * @expect true when input array(), array() |
||
| 254 | * @expect false when input array(), array('1') |
||
| 255 | */ |
||
| 256 | 36 | public static function isec($cx, $v) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * LightnCandy runtime method for {{{var}}} . |
||
| 262 | * |
||
| 263 | * @param array<string,array|string|integer> $cx render time context |
||
| 264 | * @param array<array|string|integer>|string|integer|null $v value to be output |
||
| 265 | * |
||
| 266 | * @return string The raw value of the specified variable |
||
| 267 | * |
||
| 268 | * @expect true when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), true |
||
| 269 | * @expect 'true' when input array('flags' => array('jstrue' => 1)), true |
||
| 270 | * @expect '' when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), false |
||
| 271 | * @expect 'false' when input array('flags' => array('jstrue' => 1)), false |
||
| 272 | * @expect 'false' when input array('flags' => array('jstrue' => 1)), false, true |
||
| 273 | * @expect 'Array' when input array('flags' => array('jstrue' => 1, 'jsobj' => 0)), array('a', 'b') |
||
| 274 | * @expect 'a,b' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', 'b') |
||
| 275 | * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('a', 'c' => 'b') |
||
| 276 | * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('c' => 'b') |
||
| 277 | * @expect 'a,true' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', true) |
||
| 278 | * @expect 'a,1' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',true) |
||
| 279 | * @expect 'a,' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false) |
||
| 280 | * @expect 'a,false' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false) |
||
| 281 | */ |
||
| 282 | 277 | public static function raw($cx, $v) { |
|
| 283 | 277 | if ($v === true) { |
|
| 284 | 1 | if ($cx['flags']['jstrue']) { |
|
| 285 | 1 | return 'true'; |
|
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | 277 | if (($v === false)) { |
|
| 290 | 6 | if ($cx['flags']['jstrue']) { |
|
| 291 | 6 | return 'false'; |
|
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | 272 | if (is_array($v)) { |
|
| 296 | 6 | if ($cx['flags']['jsobj']) { |
|
| 297 | 6 | if (count(array_diff_key($v, array_keys(array_keys($v)))) > 0) { |
|
| 298 | 2 | return '[object Object]'; |
|
| 299 | } else { |
||
| 300 | 5 | $ret = array(); |
|
| 301 | 5 | foreach ($v as $k => $vv) { |
|
| 302 | 5 | $ret[] = static::raw($cx, $vv); |
|
| 303 | } |
||
| 304 | 5 | return join(',', $ret); |
|
| 305 | } |
||
| 306 | } else { |
||
| 307 | 1 | return 'Array'; |
|
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | 272 | return "$v"; |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * LightnCandy runtime method for {{var}} . |
||
| 316 | * |
||
| 317 | * @param array<string,array|string|integer> $cx render time context |
||
| 318 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 319 | * |
||
| 320 | * @return string The htmlencoded value of the specified variable |
||
| 321 | * |
||
| 322 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 323 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 324 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 325 | */ |
||
| 326 | 44 | public static function enc($cx, $var) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * LightnCandy runtime method for {{var}} , and deal with single quote to same as handlebars.js . |
||
| 332 | * |
||
| 333 | * @param array<string,array|string|integer> $cx render time context |
||
| 334 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 335 | * |
||
| 336 | * @return string The htmlencoded value of the specified variable |
||
| 337 | * |
||
| 338 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 339 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 340 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 341 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 342 | */ |
||
| 343 | 206 | public static function encq($cx, $var) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * LightnCandy runtime method for {{#var}} section. |
||
| 349 | * |
||
| 350 | * @param array<string,array|string|integer> $cx render time context |
||
| 351 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 352 | * @param array<string>|null $bp block parameters |
||
| 353 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 354 | * @param boolean $each true when rendering #each |
||
| 355 | * @param Closure $cb callback function to render child context |
||
| 356 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 357 | * |
||
| 358 | * @return string The rendered string of the section |
||
| 359 | * |
||
| 360 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 361 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 362 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 363 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 364 | * @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=";} |
||
| 365 | * @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=";} |
||
| 366 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 367 | * @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=";} |
||
| 368 | * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return count($i);} |
||
| 369 | * @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);} |
||
| 370 | * @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);} |
||
| 371 | * @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);} |
||
| 372 | * @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';} |
||
| 373 | * @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';} |
||
| 374 | * @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';} |
||
| 375 | * @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';} |
||
| 376 | * @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';} |
||
| 377 | * @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';} |
||
| 378 | * @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';} |
||
| 379 | * @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';} |
||
| 380 | * @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';} |
||
| 381 | * @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';} |
||
| 382 | * @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;} |
||
| 383 | * @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'];} |
||
| 384 | */ |
||
| 385 | 152 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
| 386 | 152 | $push = ($in !== $v) || $each; |
|
| 387 | |||
| 388 | 152 | if ($v instanceof \Closure) { |
|
| 389 | if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) { |
||
| 390 | $v = $v(); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | 152 | $isAry = is_array($v) || ($v instanceof \ArrayObject); |
|
| 395 | 152 | $isTrav = $v instanceof \Traversable; |
|
| 396 | 152 | $loop = $each; |
|
| 397 | 152 | $keys = null; |
|
| 398 | 152 | $last = null; |
|
| 399 | 152 | $isObj = false; |
|
| 400 | |||
| 401 | 152 | if ($isAry && $else !== null && count($v) === 0) { |
|
| 402 | 3 | $ret = $else($cx, $in); |
|
| 403 | 3 | return $ret; |
|
| 404 | } |
||
| 405 | |||
| 406 | // #var, detect input type is object or not |
||
| 407 | 150 | if (!$loop && $isAry) { |
|
| 408 | 63 | $keys = array_keys($v); |
|
| 409 | 63 | $loop = (count(array_diff_key($v, array_keys($keys))) == 0); |
|
| 410 | 63 | $isObj = !$loop; |
|
| 411 | } |
||
| 412 | |||
| 413 | 150 | if ($cx['flags']['mustlam'] && ($v instanceof \Closure)) { |
|
| 414 | static::err('Do not support Section Lambdas!'); |
||
| 415 | } |
||
| 416 | |||
| 417 | 150 | if (($loop && $isAry) || $isTrav) { |
|
| 418 | 98 | if ($each && !$isTrav) { |
|
| 419 | // Detect input type is object or not when never done once |
||
| 420 | 43 | if ($keys == null) { |
|
| 421 | 43 | $keys = array_keys($v); |
|
| 422 | 43 | $isObj = (count(array_diff_key($v, array_keys($keys))) > 0); |
|
| 423 | } |
||
| 424 | } |
||
| 425 | 98 | $ret = array(); |
|
| 426 | 98 | if ($push) { |
|
| 427 | 96 | $cx['scopes'][] = $in; |
|
| 428 | } |
||
| 429 | 98 | $i = 0; |
|
| 430 | 98 | if ($cx['flags']['spvar']) { |
|
| 431 | 87 | $old_spvar = $cx['sp_vars']; |
|
| 432 | 87 | $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $old_spvar, array('_parent' => $old_spvar)); |
|
| 433 | 87 | if (!$isTrav) { |
|
| 434 | 86 | $last = count($keys) - 1; |
|
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | 98 | $isSparceArray = $isObj && (count(array_filter(array_keys($v), 'is_string')) == 0); |
|
| 439 | 98 | foreach ($v as $index => $raw) { |
|
| 440 | 92 | if ($cx['flags']['spvar']) { |
|
| 441 | 82 | $cx['sp_vars']['first'] = ($i === 0); |
|
| 442 | 82 | $cx['sp_vars']['last'] = ($i == $last); |
|
| 443 | 82 | $cx['sp_vars']['key'] = $index; |
|
| 444 | 82 | $cx['sp_vars']['index'] = $isSparceArray ? $index : $i; |
|
| 445 | 82 | $i++; |
|
| 446 | } |
||
| 447 | 92 | if (isset($bp[0])) { |
|
| 448 | 1 | $raw = static::m($cx, $raw, array($bp[0] => $raw)); |
|
| 449 | } |
||
| 450 | 92 | if (isset($bp[1])) { |
|
| 451 | 1 | $raw = static::m($cx, $raw, array($bp[1] => $cx['sp_vars']['index'])); |
|
| 452 | } |
||
| 453 | 92 | $ret[] = $cb($cx, $raw); |
|
| 454 | } |
||
| 455 | 97 | if ($cx['flags']['spvar']) { |
|
| 456 | 86 | if ($isObj) { |
|
| 457 | 14 | unset($cx['sp_vars']['key']); |
|
| 458 | } else { |
||
| 459 | 74 | unset($cx['sp_vars']['last']); |
|
| 460 | } |
||
| 461 | 86 | unset($cx['sp_vars']['index']); |
|
| 462 | 86 | unset($cx['sp_vars']['first']); |
|
| 463 | 86 | $cx['sp_vars'] = $old_spvar; |
|
| 464 | } |
||
| 465 | 97 | if ($push) { |
|
| 466 | 95 | array_pop($cx['scopes']); |
|
| 467 | } |
||
| 468 | 97 | return join('', $ret); |
|
| 469 | } |
||
| 470 | 55 | if ($each) { |
|
| 471 | 2 | if ($else !== null) { |
|
| 472 | 1 | $ret = $else($cx, $v); |
|
| 473 | 1 | return $ret; |
|
| 474 | } |
||
| 475 | 2 | return ''; |
|
| 476 | } |
||
| 477 | 54 | if ($isAry) { |
|
| 478 | 9 | if ($push) { |
|
| 479 | 8 | $cx['scopes'][] = $in; |
|
| 480 | } |
||
| 481 | 9 | $ret = $cb($cx, $v); |
|
| 482 | 9 | if ($push) { |
|
| 483 | 8 | array_pop($cx['scopes']); |
|
| 484 | } |
||
| 485 | 9 | return $ret; |
|
| 486 | } |
||
| 487 | |||
| 488 | 46 | if ($v === true) { |
|
| 489 | 22 | return $cb($cx, $in); |
|
| 490 | } |
||
| 491 | |||
| 492 | 25 | if (($v !== null) && ($v !== false)) { |
|
| 493 | 5 | return $cb($cx, $v); |
|
| 494 | } |
||
| 495 | |||
| 496 | 21 | if ($else !== null) { |
|
| 497 | 7 | $ret = $else($cx, $in); |
|
| 498 | 7 | return $ret; |
|
| 499 | } |
||
| 500 | |||
| 501 | 15 | return ''; |
|
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * LightnCandy runtime method for {{#with var}} . |
||
| 506 | * |
||
| 507 | * @param array<string,array|string|integer> $cx render time context |
||
| 508 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 509 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 510 | * @param array<string>|null $bp block parameters |
||
| 511 | * @param Closure $cb callback function to render child context |
||
| 512 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 513 | * |
||
| 514 | * @return string The rendered string of the token |
||
| 515 | * |
||
| 516 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 517 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 518 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 519 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 520 | */ |
||
| 521 | 15 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
| 522 | 15 | if (isset($bp[0])) { |
|
| 523 | 2 | $v = static::m($cx, $v, array($bp[0] => $v)); |
|
| 524 | } |
||
| 525 | 15 | if (($v === false) || ($v === null)) { |
|
| 526 | 4 | return $else ? $else($cx, $in) : ''; |
|
| 527 | } |
||
| 528 | 12 | $cx['scopes'][] = $in; |
|
| 529 | 12 | $ret = $cb($cx, $v); |
|
| 530 | 12 | array_pop($cx['scopes']); |
|
| 531 | 12 | return $ret; |
|
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * LightnCandy runtime method to get merged context |
||
| 536 | * |
||
| 537 | * @param array<string,array|string|integer> $cx render time context |
||
| 538 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 539 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 540 | * |
||
| 541 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 542 | * |
||
| 543 | */ |
||
| 544 | 60 | public static function m($cx, $a, $b) { |
|
| 545 | 60 | if (is_array($b)) { |
|
| 546 | 60 | if ($a === null) { |
|
| 547 | 6 | return $b; |
|
| 548 | 54 | } else if (is_array($a)) { |
|
| 549 | 51 | return array_merge($a, $b); |
|
| 550 | 3 | } else if (($cx['flags']['method'] || $cx['flags']['prop']) && is_object($a)) { |
|
| 551 | 1 | foreach ($b as $i => $v) { |
|
| 552 | 1 | $a->$i = $v; |
|
| 553 | } |
||
| 554 | } |
||
| 555 | } |
||
| 556 | 3 | return $a; |
|
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * LightnCandy runtime method for {{> partial}} . |
||
| 561 | * |
||
| 562 | * @param array<string,array|string|integer> $cx render time context |
||
| 563 | * @param string $p partial name |
||
| 564 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 565 | * |
||
| 566 | * @return string The rendered string of the partial |
||
| 567 | * |
||
| 568 | */ |
||
| 569 | 56 | public static function p($cx, $p, $v, $sp = '') { |
|
| 570 | 56 | if (!isset($cx['partials'][$p])) { |
|
| 571 | 1 | static::err($cx, "Can not find partial named as '$p' !!"); |
|
| 572 | return ''; |
||
| 573 | } |
||
| 574 | |||
| 575 | 55 | return call_user_func($cx['partials'][$p], $cx, static::m($cx, $v[0][0], $v[1]), $sp); |
|
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * LightnCandy runtime method for custom helpers. |
||
| 580 | * |
||
| 581 | * @param array<string,array|string|integer> $cx render time context |
||
| 582 | * @param string $ch the name of custom helper to be executed |
||
| 583 | * @param array<array> $vars variables for the helper |
||
| 584 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 585 | * |
||
| 586 | * @return string The rendered string of the token |
||
| 587 | * |
||
| 588 | * @expect '---' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('-'),array()), 'raw' |
||
| 589 | * @expect '-&-' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('&'),array()), 'enc' |
||
| 590 | * @expect '-'-' when input array('helpers' => array('a' => function ($i) {return "-$i[0]-";})), 'a', array(array('\''),array()), 'encq' |
||
| 591 | * @expect '-b-' when input array('helpers' => array('a' => function ($i,$j) {return "-{$j['a']}-";})), 'a', array(array(),array('a' => 'b')), 'raw' |
||
| 592 | */ |
||
| 593 | 22 | public static function ch($cx, $ch, $vars, $op) { |
|
| 596 | |||
| 597 | /** |
||
| 598 | * LightnCandy runtime method to handle response of custom helpers. |
||
| 599 | * |
||
| 600 | * @param string|array<string,array|string|integer> $ret return value from custom helper |
||
| 601 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 602 | * |
||
| 603 | * @return string The rendered string of the token |
||
| 604 | * |
||
| 605 | * @expect '-&-' when input '-&-', 'raw' |
||
| 606 | * @expect '-&'-' when input '-&\'-', 'enc' |
||
| 607 | * @expect '-&'-' when input '-&\'-', 'encq' |
||
| 608 | * @expect '-&'-' when input array('-&\'-'), 'enc' |
||
| 609 | * @expect '-&'-' when input array('-&\'-'), 'encq' |
||
| 610 | * @expect '-&-' when input array('-&-', false), 'enc' |
||
| 611 | * @expect '-&-' when input array('-&-', false), 'raw' |
||
| 612 | * @expect '-&-' when input array('-&-', 'raw'), 'enc' |
||
| 613 | * @expect '-&'-' when input array('-&\'-', 'encq'), 'raw' |
||
| 614 | */ |
||
| 615 | 146 | public static function chret($ret, $op) { |
|
| 631 | |||
| 632 | /** |
||
| 633 | * LightnCandy runtime method for Handlebars.js style custom helpers. |
||
| 634 | * |
||
| 635 | * @param array<string,array|string|integer> $cx render time context |
||
| 636 | * @param string $ch the name of custom helper to be executed |
||
| 637 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 638 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 639 | * @param boolean $inverted the logic will be inverted |
||
| 640 | * @param Closure|null $cb callback function to render child context |
||
| 641 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 642 | * |
||
| 643 | * @return string The rendered string of the token |
||
| 644 | */ |
||
| 645 | 126 | public static function hbch($cx, $ch, $vars, $op, $inverted, $cb = null, $else = null) { |
|
| 737 | |||
| 738 | /** |
||
| 739 | * LightnCandy runtime method for block custom helpers. |
||
| 740 | * |
||
| 741 | * @param array<string,array|string|integer> $cx render time context |
||
| 742 | * @param string $ch the name of custom helper to be executed |
||
| 743 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 744 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 745 | * @param boolean $inverted the logic will be inverted |
||
| 746 | * @param Closure $cb callback function to render child context |
||
| 747 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 748 | * |
||
| 749 | * @return string The rendered string of the token |
||
| 750 | * |
||
| 751 | * @expect '4.2.3' when input array('blockhelpers' => array('a' => function ($cx) {return array($cx,2,3);})), 'a', array(0, 0), 4, false, function($cx, $i) {return implode('.', $i);} |
||
| 752 | * @expect '2.6.5' when input array('blockhelpers' => array('a' => function ($cx,$in) {return array($cx,$in[0],5);})), 'a', array('6', 0), 2, false, function($cx, $i) {return implode('.', $i);} |
||
| 753 | * @expect '' when input array('blockhelpers' => array('a' => function ($cx,$in) {})), 'a', array('6', 0), 2, false, function($cx, $i) {return implode('.', $i);} |
||
| 754 | */ |
||
| 755 | 4 | public static function bch($cx, $ch, $vars, $in, $inverted, $cb, $else = null) { |
|
| 782 | } |
||
| 783 | |||
| 784 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.