Complex classes like Compiler 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 Compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Compiler extends Validator |
||
32 | { |
||
33 | public static $lastParsed; |
||
34 | |||
35 | /** |
||
36 | * Compile template into PHP code |
||
37 | * |
||
38 | * @param array<string,array|string|integer> $context Current context |
||
39 | * @param string $template handlebars template |
||
40 | * |
||
41 | * @return string|null generated PHP code |
||
42 | */ |
||
43 | 716 | public static function compileTemplate(&$context, $template) { |
|
44 | 716 | array_unshift($context['parsed'], array()); |
|
45 | 716 | Validator::verify($context, $template); |
|
46 | |||
47 | 716 | if (count($context['error'])) { |
|
48 | 70 | return; |
|
49 | } |
||
50 | |||
51 | // Do PHP code generation. |
||
52 | 647 | Parser::setDelimiter($context); |
|
53 | |||
54 | // Handle dynamic partials |
||
55 | 647 | Partial::handleDynamic($context); |
|
56 | |||
57 | 647 | $code = ''; |
|
58 | 647 | foreach ($context['parsed'][0] as $info) { |
|
59 | 647 | if (is_array($info)) { |
|
60 | 606 | $context['tokens']['current']++; |
|
61 | 606 | $tmpl = static::compileToken($context, $info); |
|
62 | 606 | if ($tmpl == $context['ops']['seperator']) { |
|
63 | 1 | $tmpl = ''; |
|
64 | } else { |
||
65 | 605 | $tmpl = "'$tmpl'"; |
|
66 | } |
||
67 | 606 | $code .= $tmpl; |
|
68 | } else { |
||
69 | 647 | $code .= $info; |
|
70 | } |
||
71 | } |
||
72 | |||
73 | 647 | static::$lastParsed = array_shift($context['parsed']); |
|
74 | |||
75 | 647 | return $code; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Compose LightnCandy render codes for include() |
||
80 | * |
||
81 | * @param array<string,array|string|integer> $context Current context |
||
82 | * @param string $code generated PHP code |
||
83 | * |
||
84 | * @return string Composed PHP code |
||
85 | */ |
||
86 | 646 | public static function composePHPRender($context, $code) { |
|
87 | 646 | $flagJStrue = Expression::boolString($context['flags']['jstrue']); |
|
88 | 646 | $flagJSObj = Expression::boolString($context['flags']['jsobj']); |
|
89 | 646 | $flagSPVar = Expression::boolString($context['flags']['spvar']); |
|
90 | 646 | $flagProp = Expression::boolString($context['flags']['prop']); |
|
91 | 646 | $flagMethod = Expression::boolString($context['flags']['method']); |
|
92 | 646 | $flagLambda = Expression::boolString($context['flags']['lambda']); |
|
93 | 646 | $flagMustlok = Expression::boolString($context['flags']['mustlok']); |
|
94 | 646 | $flagMustlam = Expression::boolString($context['flags']['mustlam']); |
|
95 | 646 | $flagEcho = Expression::boolString($context['flags']['echo']); |
|
96 | 646 | $flagPartNC = Expression::boolString($context['flags']['partnc']); |
|
97 | 646 | $flagKnownHlp = Expression::boolString($context['flags']['knohlp']); |
|
98 | |||
99 | 646 | $libstr = Exporter::runtime($context); |
|
100 | 646 | $constants = Exporter::constants($context); |
|
101 | 646 | $helpers = Exporter::helpers($context); |
|
102 | 646 | $bhelpers = Exporter::helpers($context, 'blockhelpers'); |
|
103 | 646 | $hbhelpers = Exporter::helpers($context, 'hbhelpers'); |
|
104 | 646 | $partials = implode(",\n", $context['partialCode']); |
|
105 | 646 | $debug = Runtime::DEBUG_ERROR_LOG; |
|
106 | |||
107 | // Return generated PHP code string. |
||
108 | 646 | return "use {$context['runtime']} as LR; |
|
109 | return function (\$in, \$options = null) { |
||
110 | \$cx = array( |
||
111 | 'flags' => array( |
||
112 | 'jstrue' => $flagJStrue, |
||
113 | 'jsobj' => $flagJSObj, |
||
114 | 'spvar' => $flagSPVar, |
||
115 | 'prop' => $flagProp, |
||
116 | 'method' => $flagMethod, |
||
117 | 'lambda' => $flagLambda, |
||
118 | 'mustlok' => $flagMustlok, |
||
119 | 'mustlam' => $flagMustlam, |
||
120 | 'echo' => $flagEcho, |
||
121 | 'partnc' => $flagPartNC, |
||
122 | 'knohlp' => $flagKnownHlp, |
||
123 | 'debug' => isset(\$options['debug']) ? \$options['debug'] : $debug, |
||
124 | ), |
||
125 | 'constants' => $constants, |
||
126 | 'helpers' => $helpers, |
||
127 | 'blockhelpers' => $bhelpers, |
||
128 | 'hbhelpers' => isset(\$options['helpers']) ? array_merge($hbhelpers, \$options['helpers']) : $hbhelpers, |
||
129 | 'partials' => array($partials), |
||
130 | 'scopes' => array(), |
||
131 | 'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in), |
||
132 | 'blparam' => array(), |
||
133 | 646 | 'runtime' => '{$context['runtime']}', |
|
134 | $libstr |
||
135 | ); |
||
136 | 646 | {$context['renderex']} |
|
137 | 646 | {$context['ops']['op_start']}'$code'{$context['ops']['op_end']} |
|
138 | 646 | }"; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get function name for standalone or none standalone template. |
||
143 | * |
||
144 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
145 | * @param string $name base function name |
||
146 | * @param string $tag original handlabars tag for debug |
||
147 | * |
||
148 | * @return string compiled Function name |
||
149 | * |
||
150 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
151 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
152 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
153 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
154 | */ |
||
155 | 582 | protected static function getFuncName(&$context, $name, $tag) { |
|
168 | |||
169 | /** |
||
170 | * Get string presentation of variables |
||
171 | * |
||
172 | * @param array<string,array|string|integer> $context current compile context |
||
173 | * @param array<array> $vn variable name array. |
||
174 | * @param array<string>|null $blockParams block param list |
||
175 | * |
||
176 | * @return array<string|array> variable names |
||
177 | * |
||
178 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
179 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
180 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
181 | */ |
||
182 | 258 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
183 | 258 | $vars = array(array(), array()); |
|
184 | 258 | $exps = array(); |
|
185 | 258 | foreach ($vn as $i => $v) { |
|
186 | 219 | $V = static::getVariableNameOrSubExpression($context, $v); |
|
187 | 219 | if (is_string($i)) { |
|
188 | 36 | $vars[1][] = "'$i'=>{$V[0]}"; |
|
189 | } else { |
||
190 | 205 | $vars[0][] = $V[0]; |
|
191 | } |
||
192 | 219 | $exps[] = $V[1]; |
|
193 | } |
||
194 | 258 | $bp = $blockParams ? (',array(' . Expression::listString($blockParams) . ')') : ''; |
|
195 | 258 | return array('array(array(' . implode(',', $vars[0]) . '),array(' . implode(',', $vars[1]) . ")$bp)", $exps); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get string presentation of a sub expression |
||
200 | * |
||
201 | * @param array<string,array|string|integer> $context current compile context |
||
202 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
203 | * |
||
204 | * @return array<string> code representing passed expression |
||
205 | */ |
||
206 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
207 | 41 | $origSeperator = $context['ops']['seperator']; |
|
208 | 41 | $context['ops']['seperator'] = ''; |
|
209 | |||
210 | 41 | $ret = static::customHelper($context, $vars, true); |
|
211 | |||
212 | 41 | if (($ret === null) && $context['flags']['lambda']) { |
|
213 | 4 | $ret = static::compileVariable($context, $vars, true); |
|
214 | } |
||
215 | |||
216 | 41 | $context['ops']['seperator'] = $origSeperator; |
|
217 | |||
218 | 41 | return array($ret ? $ret : '', 'FIXME: $subExpression'); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get string presentation of a subexpression or a variable |
||
223 | * |
||
224 | * @param array<array|string|integer> $context current compile context |
||
225 | * @param array<array|string|integer> $var variable parsed path |
||
226 | * |
||
227 | * @return array<string> variable names |
||
|
|||
228 | */ |
||
229 | 360 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
230 | 360 | return Parser::isSubExp($var) ? static::compileSubExpression($context, $var[1]) : static::getVariableName($context, $var); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Get string presentation of a variable |
||
235 | * |
||
236 | * @param array<array|string|integer> $var variable parsed path |
||
237 | * @param array<array|string|integer> $context current compile context |
||
238 | * @param array<string> $lookup extra lookup string as valid PHP variable name |
||
239 | * |
||
240 | * @return array<string> variable names |
||
241 | * |
||
242 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
243 | * @expect array('((isset($in[\'true\']) && is_array($in)) ? $in[\'true\'] : null)', '[true]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('true') |
||
244 | * @expect array('((isset($in[\'false\']) && is_array($in)) ? $in[\'false\'] : null)', '[false]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('false') |
||
245 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
246 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
247 | * @expect array('((isset($in[\'2\']) && is_array($in)) ? $in[\'2\'] : null)', '[2]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('2') |
||
248 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
249 | * @expect array('((isset($in[\'@index\']) && is_array($in)) ? $in[\'@index\'] : null)', '[@index]') when input array('flags'=>array('spvar'=>false,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@index') |
||
250 | * @expect array("((isset(\$cx['sp_vars']['index']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['index'] : null)", '@[index]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@index') |
||
251 | * @expect array("((isset(\$cx['sp_vars']['key']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['key'] : null)", '@[key]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@key') |
||
252 | * @expect array("((isset(\$cx['sp_vars']['first']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['first'] : null)", '@[first]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@first') |
||
253 | * @expect array("((isset(\$cx['sp_vars']['last']) && is_array(\$cx['sp_vars'])) ? \$cx['sp_vars']['last'] : null)", '@[last]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('@last') |
||
254 | * @expect array('((isset($in[\'"a"\']) && is_array($in)) ? $in[\'"a"\'] : null)', '["a"]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('"a"') |
||
255 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
256 | * @expect array('((isset($in[\'a\']) && is_array($in)) ? $in[\'a\'] : null)', '[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array('a') |
||
257 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\']) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-1])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'] : null)', '../[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(1,'a') |
||
258 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\']) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-3])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'] : null)', '../../../[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(3,'a') |
||
259 | * @expect array('((isset($in[\'id\']) && is_array($in)) ? $in[\'id\'] : null)', 'this.[id]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0)), array(null, 'id') |
||
260 | * @expect array('LR::v($cx, $in, isset($in) ? $in : null, array(\'id\'))', 'this.[id]') when input array('flags'=>array('prop'=>true,'spvar'=>true,'debug'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0, 'lambda'=>0,'standalone'=>0), 'runtime' => 'Runtime'), array(null, 'id') |
||
261 | */ |
||
262 | 577 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
307 | |||
308 | /** |
||
309 | * Return compiled PHP code for a handlebars token |
||
310 | * |
||
311 | * @param array<string,array|string|integer> $context current compile context |
||
312 | * @param array<string,array|boolean> $info parsed information |
||
313 | * |
||
314 | * @return string Return compiled code segment for the token |
||
315 | */ |
||
316 | 606 | protected static function compileToken(&$context, $info) { |
|
317 | 606 | list($raw, $vars, $token, $indent) = $info; |
|
318 | |||
319 | 606 | $context['tokens']['partialind'] = $indent; |
|
320 | 606 | $context['currentToken'] = $token; |
|
321 | |||
322 | // Do not touch the tag, keep it as is. |
||
323 | 606 | if ($raw === -1) { |
|
324 | return ".'" . Token::toString($token) . "'."; |
||
325 | } |
||
326 | |||
327 | 606 | if ($ret = static::operator($token[Token::POS_OP], $context, $vars)) { |
|
328 | 372 | return $ret; |
|
329 | } |
||
330 | |||
331 | 470 | if (isset($vars[0][0])) { |
|
332 | 438 | if ($ret = static::customHelper($context, $vars, $raw)) { |
|
333 | 112 | return $ret; |
|
334 | } |
||
335 | 333 | if ($vars[0][0] === 'else') { |
|
336 | 24 | return static::doElse($context); |
|
337 | } |
||
338 | 318 | if ($vars[0][0] === 'lookup') { |
|
339 | 2 | return static::compileLookup($context, $vars, $raw); |
|
340 | } |
||
341 | } |
||
342 | |||
343 | 355 | return static::compileVariable($context, $vars, $raw); |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * handle partial |
||
348 | * |
||
349 | * @param array<string,array|string|integer> $context current compile context |
||
350 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
351 | * |
||
352 | * @return string Return compiled code segment for the partial |
||
353 | */ |
||
354 | 86 | public static function partial(&$context, $vars) { |
|
355 | // mustache spec: ignore missing partial |
||
356 | 86 | if (($context['usedFeature']['dynpartial'] === 0) && ($context['usedFeature']['inlpartial'] === 0) && !isset($context['usedPartial'][$vars[0][0]])) { |
|
357 | 1 | return $context['ops']['seperator']; |
|
358 | } |
||
359 | 85 | Parser::getBlockParams($vars); |
|
360 | 85 | $p = array_shift($vars); |
|
361 | 85 | if ($context['flags']['runpart']) { |
|
362 | 77 | if (!isset($vars[0])) { |
|
363 | 70 | $vars[0] = $context['flags']['partnc'] ? array(0, 'null') : array(); |
|
364 | } |
||
365 | 77 | $v = static::getVariableNames($context, $vars); |
|
366 | 77 | $tag = ">$p[0] " .implode(' ', $v[1]); |
|
367 | 77 | if (Parser::isSubExp($p)) { |
|
368 | 4 | list($p) = static::compileSubExpression($context, $p[1]); |
|
369 | } else { |
||
370 | 73 | $p = "'$p[0]'"; |
|
371 | } |
||
372 | 77 | $sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : ''; |
|
373 | 77 | return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0]$sp){$context['ops']['seperator']}"; |
|
374 | } |
||
375 | 8 | return "{$context['ops']['seperator']}'" . Partial::compileStatic($context, $p[0]) . "'{$context['ops']['seperator']}"; |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * handle inline partial |
||
380 | * |
||
381 | * @param array<string,array|string|integer> $context current compile context |
||
382 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
383 | * |
||
384 | * @return string Return compiled code segment for the partial |
||
385 | */ |
||
386 | 10 | public static function inline(&$context, $vars) { |
|
397 | |||
398 | /** |
||
399 | * Return compiled PHP code for a handlebars inverted section begin token |
||
400 | * |
||
401 | * @param array<string,array|string|integer> $context current compile context |
||
402 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
403 | * |
||
404 | * @return string Return compiled code segment for the token |
||
405 | */ |
||
406 | 38 | protected static function invertedSection(&$context, $vars) { |
|
407 | 38 | $v = static::getVariableName($context, $vars[0]); |
|
408 | 38 | return "{$context['ops']['cnd_start']}(" . static::getFuncName($context, 'isec', '^' . $v[1]) . "\$cx, {$v[0]})){$context['ops']['cnd_then']}"; |
|
409 | } |
||
410 | |||
411 | /** |
||
412 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
413 | * |
||
414 | * @param array<string,array|string|integer> $context current compile context |
||
415 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
416 | * @param boolean $inverted the logic will be inverted |
||
417 | * |
||
418 | * @return string Return compiled code segment for the token |
||
419 | */ |
||
420 | 61 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
421 | 61 | $notHBCH = !isset($context['hbhelpers'][$vars[0][0]]); |
|
422 | |||
423 | 61 | $bp = Parser::getBlockParams($vars); |
|
424 | 61 | $ch = array_shift($vars); |
|
425 | 61 | $inverted = $inverted ? 'true' : 'false'; |
|
426 | 61 | static::addUsageCount($context, $notHBCH ? 'blockhelpers' : 'hbhelpers', $ch[0]); |
|
427 | 61 | $v = static::getVariableNames($context, $vars, $bp); |
|
428 | |||
429 | 61 | return $context['ops']['seperator'] . static::getFuncName($context, $notHBCH ? 'bch' : 'hbch', ($inverted ? '^' : '#') . implode(' ', $v[1])) . "\$cx, '$ch[0]', {$v[0]}, \$in, $inverted, function(\$cx, \$in) {{$context['ops']['f_start']}"; |
|
430 | } |
||
431 | |||
432 | /** |
||
433 | * Return compiled PHP code for a handlebars block end token |
||
434 | * |
||
435 | * @param array<string,array|string|integer> $context current compile context |
||
436 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
437 | * @param string|null $matchop should also match to this operator |
||
438 | * |
||
439 | * @return string Return compiled code segment for the token |
||
440 | */ |
||
441 | 316 | protected static function blockEnd(&$context, $vars, $matchop = NULL) { |
|
472 | |||
473 | /** |
||
474 | * Return compiled PHP code for a handlebars block begin token |
||
475 | * |
||
476 | * @param array<string,array|string|integer> $context current compile context |
||
477 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
478 | * |
||
479 | * @return string Return compiled code segment for the token |
||
480 | */ |
||
481 | 232 | protected static function blockBegin(&$context, $vars) { |
|
482 | 232 | $v = isset($vars[1]) ? static::getVariableNameOrSubExpression($context, $vars[1]) : array(null, array()); |
|
483 | 232 | if (!$context['flags']['nohbh']) { |
|
484 | 194 | switch (isset($vars[0][0]) ? $vars[0][0] : null) { |
|
485 | 4 | case 'if': |
|
486 | 59 | $includeZero = (isset($vars['includeZero'][1]) && $vars['includeZero'][1]) ? 'true' : 'false'; |
|
487 | 59 | return "{$context['ops']['cnd_start']}(" . static::getFuncName($context, 'ifvar', $v[1]) . "\$cx, {$v[0]}, {$includeZero})){$context['ops']['cnd_then']}"; |
|
488 | 4 | case 'unless': |
|
489 | 3 | return "{$context['ops']['cnd_start']}(!" . static::getFuncName($context, 'ifvar', $v[1]) . "\$cx, {$v[0]}, false)){$context['ops']['cnd_then']}"; |
|
490 | 4 | case 'each': |
|
491 | 47 | return static::section($context, $vars, true); |
|
492 | 74 | case 'with': |
|
493 | 27 | if ($r = static::with($context, $vars)) { |
|
494 | 27 | return $r; |
|
495 | } |
||
496 | } |
||
497 | } |
||
498 | |||
499 | 112 | return static::section($context, $vars); |
|
500 | } |
||
501 | |||
502 | /** |
||
503 | * compile {{#foo}} token |
||
504 | * |
||
505 | * @param array<string,array|string|integer> $context current compile context |
||
506 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
507 | * @param boolean $isEach the section is #each |
||
508 | * |
||
509 | * @return string|null Return compiled code segment for the token |
||
510 | */ |
||
511 | 159 | protected static function section(&$context, $vars, $isEach = false) { |
|
512 | 159 | $bs = 'null'; |
|
513 | 159 | $be = ''; |
|
514 | 159 | if ($isEach) { |
|
515 | 47 | $bp = Parser::getBlockParams($vars); |
|
516 | 47 | $bs = $bp ? ('array(' . Expression::listString($bp) . ')') : 'null'; |
|
517 | 47 | $be = $bp ? " as |$bp[0] $bp[1]|" : ''; |
|
518 | 47 | array_shift($vars); |
|
519 | 47 | if (!isset($vars[0])) { |
|
520 | $vars[0] = array(null); |
||
521 | } |
||
522 | } |
||
523 | 159 | if ($context['flags']['lambda'] && !$isEach) { |
|
524 | 66 | $V = array_shift($vars); |
|
525 | 66 | $v = static::getVariableName($context, $V, null, count($vars) ? static::getVariableNames($context, $vars) : array('0',array(''))); |
|
526 | } else { |
||
527 | 93 | $v = static::getVariableNameOrSubExpression($context, $vars[0]); |
|
528 | } |
||
529 | 159 | $each = $isEach ? 'true' : 'false'; |
|
530 | 159 | return $context['ops']['seperator'] . static::getFuncName($context, 'sec', ($isEach ? 'each ' : '') . $v[1] . $be) . "\$cx, {$v[0]}, $bs, \$in, $each, function(\$cx, \$in) {{$context['ops']['f_start']}"; |
|
531 | } |
||
532 | |||
533 | /** |
||
534 | * compile {{with}} token |
||
535 | * |
||
536 | * @param array<string,array|string|integer> $context current compile context |
||
537 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
538 | * |
||
539 | * @return string|null Return compiled code segment for the token |
||
540 | */ |
||
541 | 27 | protected static function with(&$context, $vars) { |
|
542 | 27 | $v = isset($vars[1]) ? static::getVariableNameOrSubExpression($context, $vars[1]) : array(null, array()); |
|
543 | 27 | $bp = Parser::getBlockParams($vars); |
|
544 | 27 | $bs = $bp ? ('array(' . Expression::listString($bp) . ')') : 'null'; |
|
545 | 27 | $be = $bp ? " as |$bp[0]|" : ''; |
|
546 | 27 | return $context['ops']['seperator'] . static::getFuncName($context, 'wi', 'with ' . $v[1] . $be) . "\$cx, {$v[0]}, $bs, \$in, function(\$cx, \$in) {{$context['ops']['f_start']}"; |
|
547 | } |
||
548 | |||
549 | /** |
||
550 | * Return compiled PHP code for a handlebars custom helper token |
||
551 | * |
||
552 | * @param array<string,array|string|integer> $context current compile context |
||
553 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
554 | * @param boolean $raw is this {{{ token or not |
||
555 | * |
||
556 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
557 | */ |
||
558 | 446 | protected static function customHelper(&$context, $vars, $raw) { |
|
570 | |||
571 | /** |
||
572 | * Return compiled PHP code for a handlebars else token |
||
573 | * |
||
574 | * @param array<string,array|string|integer> $context current compile context |
||
575 | * |
||
576 | * @return string Return compiled code segment for the token when the token is else |
||
577 | */ |
||
578 | 46 | protected static function doElse(&$context) { |
|
579 | 46 | switch ($context['stack'][count($context['stack']) - 2]) { |
|
580 | case '[if]': |
||
581 | 27 | case '[unless]': |
|
582 | 19 | $context['stack'][] = ':'; |
|
583 | 19 | return "{$context['ops']['cnd_else']}"; |
|
584 | default: |
||
585 | 27 | return "{$context['ops']['f_end']}}, function(\$cx, \$in) {{$context['ops']['f_start']}"; |
|
586 | } |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * Return compiled PHP code for a handlebars lookup token |
||
591 | * |
||
592 | * @param array<string,array|string|integer> $context current compile context |
||
593 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
594 | * @param boolean $raw is this {{{ token or not |
||
595 | * |
||
596 | * @return string Return compiled code segment for the token |
||
597 | */ |
||
598 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
607 | |||
608 | /** |
||
609 | * Return compiled PHP code for a handlebars variable token |
||
610 | * |
||
611 | * @param array<string,array|string|integer> $context current compile context |
||
612 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
613 | * @param boolean $raw is this {{{ token or not |
||
614 | * |
||
615 | * @return string Return compiled code segment for the token |
||
616 | */ |
||
617 | 358 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
630 | |||
631 | /** |
||
632 | * Add usage count to context |
||
633 | * |
||
634 | * @param array<string,array|string|integer> $context current context |
||
635 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
636 | * @param string $name used name |
||
637 | * @param integer $count increment |
||
638 | * |
||
639 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
640 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
641 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
642 | */ |
||
643 | 582 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
649 | } |
||
650 | |||
651 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.