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 | 758 | public static function compileTemplate(&$context, $template) { |
|
44 | 758 | array_unshift($context['parsed'], array()); |
|
45 | 758 | Validator::verify($context, $template); |
|
46 | 758 | static::$lastParsed = $context['parsed']; |
|
47 | |||
48 | 758 | if (count($context['error'])) { |
|
49 | 77 | return; |
|
50 | } |
||
51 | |||
52 | 682 | Parser::setDelimiter($context); |
|
53 | |||
54 | 682 | $context['compile'] = true; |
|
55 | |||
56 | // Handle dynamic partials |
||
57 | 682 | Partial::handleDynamic($context); |
|
58 | |||
59 | // Do PHP code generation. |
||
60 | 682 | $code = ''; |
|
61 | 682 | foreach ($context['parsed'][0] as $info) { |
|
62 | 682 | if (is_array($info)) { |
|
63 | 641 | $context['tokens']['current']++; |
|
64 | 641 | $code .= "'" . static::compileToken($context, $info) . "'"; |
|
65 | } else { |
||
66 | 682 | $code .= $info; |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 682 | array_shift($context['parsed']); |
|
71 | |||
72 | 682 | return $code; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Compose LightnCandy render codes for include() |
||
77 | * |
||
78 | * @param array<string,array|string|integer> $context Current context |
||
79 | * @param string $code generated PHP code |
||
80 | * |
||
81 | * @return string Composed PHP code |
||
82 | */ |
||
83 | 681 | public static function composePHPRender($context, $code) { |
|
84 | 681 | $flagJStrue = Expression::boolString($context['flags']['jstrue']); |
|
85 | 681 | $flagJSObj = Expression::boolString($context['flags']['jsobj']); |
|
86 | 681 | $flagSPVar = Expression::boolString($context['flags']['spvar']); |
|
87 | 681 | $flagProp = Expression::boolString($context['flags']['prop']); |
|
88 | 681 | $flagMethod = Expression::boolString($context['flags']['method']); |
|
89 | 681 | $flagLambda = Expression::boolString($context['flags']['lambda']); |
|
90 | 681 | $flagMustlok = Expression::boolString($context['flags']['mustlok']); |
|
91 | 681 | $flagMustlam = Expression::boolString($context['flags']['mustlam']); |
|
92 | 681 | $flagEcho = Expression::boolString($context['flags']['echo']); |
|
93 | 681 | $flagPartNC = Expression::boolString($context['flags']['partnc']); |
|
94 | 681 | $flagKnownHlp = Expression::boolString($context['flags']['knohlp']); |
|
95 | |||
96 | 681 | $constants = Exporter::constants($context); |
|
97 | 681 | $helpers = Exporter::helpers($context); |
|
98 | 681 | $partials = implode(",\n", $context['partialCode']); |
|
99 | 681 | $debug = Runtime::DEBUG_ERROR_LOG; |
|
100 | 681 | $use = $context['flags']['standalone'] ? Exporter::runtime($context) : "use {$context['runtime']} as {$context['runtimealias']};"; |
|
101 | 681 | $safeString = (($context['usedFeature']['enc'] > 0) && ($context['flags']['standalone'] === 0)) ? "use {$context['safestring']} as SafeString;" : ''; |
|
102 | 681 | $exportSafeString = (($context['usedFeature']['enc'] > 0) && ($context['flags']['standalone'] >0)) ? Exporter::safestring($context) : ''; |
|
103 | |||
104 | // Return generated PHP code string. |
||
105 | return <<<VAREND |
||
106 | 681 | $safeString{$use}{$exportSafeString}return function (\$in = null, \$options = null) { |
|
107 | 681 | \$helpers = $helpers; |
|
108 | 681 | \$partials = array($partials); |
|
109 | \$cx = array( |
||
110 | 'flags' => array( |
||
111 | 681 | 'jstrue' => $flagJStrue, |
|
112 | 681 | 'jsobj' => $flagJSObj, |
|
113 | 681 | 'spvar' => $flagSPVar, |
|
114 | 681 | 'prop' => $flagProp, |
|
115 | 681 | 'method' => $flagMethod, |
|
116 | 681 | 'lambda' => $flagLambda, |
|
117 | 681 | 'mustlok' => $flagMustlok, |
|
118 | 681 | 'mustlam' => $flagMustlam, |
|
119 | 681 | 'echo' => $flagEcho, |
|
120 | 681 | 'partnc' => $flagPartNC, |
|
121 | 681 | 'knohlp' => $flagKnownHlp, |
|
122 | 681 | 'debug' => isset(\$options['debug']) ? \$options['debug'] : $debug, |
|
123 | ), |
||
124 | 681 | 'constants' => $constants, |
|
125 | 'helpers' => isset(\$options['helpers']) ? array_merge(\$helpers, \$options['helpers']) : \$helpers, |
||
126 | 'partials' => isset(\$options['partials']) ? array_merge(\$partials, \$options['partials']) : \$partials, |
||
127 | 'scopes' => array(), |
||
128 | 'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in), |
||
129 | 'blparam' => array(), |
||
130 | 'partialid' => 0, |
||
131 | 681 | 'runtime' => '{$context['runtime']}', |
|
132 | ); |
||
133 | 681 | {$context['renderex']} |
|
134 | 681 | {$context['ops']['op_start']}'$code'{$context['ops']['op_end']} |
|
135 | 681 | }; |
|
136 | VAREND |
||
137 | ; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get function name for standalone or none standalone template. |
||
142 | * |
||
143 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
144 | * @param string $name base function name |
||
145 | * @param string $tag original handlabars tag for debug |
||
146 | * |
||
147 | * @return string compiled Function name |
||
148 | * |
||
149 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), 'test', '' |
||
150 | * @expect 'LL::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', '' |
||
151 | * @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 0, 'funcprefix' => 'lala_abc'), 'test3', '' |
||
152 | * @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc' |
||
153 | */ |
||
154 | 616 | protected static function getFuncName(&$context, $name, $tag) { |
|
155 | 616 | static::addUsageCount($context, 'runtime', $name); |
|
156 | |||
157 | 616 | if ($context['flags']['debug'] && ($name != 'miss')) { |
|
158 | 10 | $dbg = "'$tag', '$name', "; |
|
159 | 10 | $name = 'debug'; |
|
160 | 10 | static::addUsageCount($context, 'runtime', 'debug'); |
|
161 | } else { |
||
162 | 614 | $dbg = ''; |
|
163 | } |
||
164 | |||
165 | 616 | return $context['flags']['standalone'] ? "{$context['funcprefix']}$name($dbg" : "{$context['runtimealias']}::$name($dbg"; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get string presentation of variables |
||
170 | * |
||
171 | * @param array<string,array|string|integer> $context current compile context |
||
172 | * @param array<array> $vn variable name array. |
||
173 | * @param array<string>|null $blockParams block param list |
||
174 | * |
||
175 | * @return array<string|array> variable names |
||
176 | * |
||
177 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
178 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
179 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
180 | */ |
||
181 | 278 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
196 | |||
197 | /** |
||
198 | * Get string presentation of a sub expression |
||
199 | * |
||
200 | * @param array<string,array|string|integer> $context current compile context |
||
201 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
202 | * |
||
203 | * @return array<string> code representing passed expression |
||
204 | */ |
||
205 | 43 | public static function compileSubExpression(&$context, $vars) { |
|
206 | 43 | $ret = static::customHelper($context, $vars, true, true, true); |
|
207 | |||
208 | 43 | if (($ret === null) && $context['flags']['lambda']) { |
|
209 | 4 | $ret = static::compileVariable($context, $vars, true, true); |
|
210 | } |
||
211 | |||
212 | 43 | return array($ret ? $ret : '', 'FIXME: $subExpression'); |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Get string presentation of a subexpression or a variable |
||
217 | * |
||
218 | * @param array<array|string|integer> $context current compile context |
||
219 | * @param array<array|string|integer> $var variable parsed path |
||
220 | * |
||
221 | * @return array<string> variable names |
||
|
|||
222 | */ |
||
223 | 390 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
226 | |||
227 | /** |
||
228 | * Get string presentation of a variable |
||
229 | * |
||
230 | * @param array<array|string|integer> $var variable parsed path |
||
231 | * @param array<array|string|integer> $context current compile context |
||
232 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
233 | * |
||
234 | * @return array<string> variable names |
||
235 | * |
||
236 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
237 | * @expect array('((is_array($in) && isset($in[\'true\'])) ? $in[\'true\'] : null)', '[true]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('true') |
||
238 | * @expect array('((is_array($in) && isset($in[\'false\'])) ? $in[\'false\'] : null)', '[false]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('false') |
||
239 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
240 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
241 | * @expect array('((is_array($in) && isset($in[\'2\'])) ? $in[\'2\'] : null)', '[2]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('2') |
||
242 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
243 | * @expect array('((is_array($in) && isset($in[\'@index\'])) ? $in[\'@index\'] : null)', '[@index]') when input array('flags'=>array('spvar'=>false,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('@index') |
||
244 | * @expect array("(isset(\$cx['sp_vars']['index']) ? \$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,'jslen'=>0)), array('@index') |
||
245 | * @expect array("(isset(\$cx['sp_vars']['key']) ? \$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,'jslen'=>0)), array('@key') |
||
246 | * @expect array("(isset(\$cx['sp_vars']['first']) ? \$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,'jslen'=>0)), array('@first') |
||
247 | * @expect array("(isset(\$cx['sp_vars']['last']) ? \$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,'jslen'=>0)), array('@last') |
||
248 | * @expect array('((is_array($in) && isset($in[\'"a"\'])) ? $in[\'"a"\'] : null)', '["a"]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('"a"') |
||
249 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
250 | * @expect array('((is_array($in) && isset($in[\'a\'])) ? $in[\'a\'] : null)', '[a]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array('a') |
||
251 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'])) ? $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,'jslen'=>0)), array(1,'a') |
||
252 | * @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'])) ? $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,'jslen'=>0)), array(3,'a') |
||
253 | * @expect array('((is_array($in) && isset($in[\'id\'])) ? $in[\'id\'] : null)', 'this.[id]') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0,'mustlok'=>0,'mustlam'=>0,'lambda'=>0,'jslen'=>0)), array(null, 'id') |
||
254 | * @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,'jslen'=>0,'standalone'=>0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), array(null, 'id') |
||
255 | */ |
||
256 | 609 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
257 | 609 | if (isset($var[0]) && ($var[0] === Parser::LITERAL)) { |
|
258 | 87 | if ($var[1] === "undefined") { |
|
259 | 2 | $var[1] = "null"; |
|
260 | } |
||
261 | 87 | return array($var[1], preg_replace('/\'(.*)\'/', '$1', $var[1])); |
|
262 | } |
||
263 | |||
264 | 568 | list($levels, $spvar, $var) = Expression::analyze($context, $var); |
|
265 | 568 | $exp = Expression::toString($levels, $spvar, $var); |
|
266 | 568 | $base = $spvar ? "\$cx['sp_vars']" : '$in'; |
|
267 | |||
268 | // change base when trace to parent |
||
269 | 568 | if ($levels > 0) { |
|
270 | 41 | if ($spvar) { |
|
271 | 2 | $base .= str_repeat("['_parent']", $levels); |
|
272 | } else { |
||
273 | 39 | $base = "\$cx['scopes'][count(\$cx['scopes'])-$levels]"; |
|
274 | } |
||
275 | } |
||
276 | |||
277 | 568 | if (((count($var) == 0) || (($var[0] === null) && (count($var) == 1))) && ($lookup === null)) { |
|
278 | 148 | return array($base, $exp); |
|
279 | } |
||
280 | |||
281 | 507 | if ((count($var) > 0) && ($var[0] === null)) { |
|
282 | 1 | array_shift($var); |
|
283 | } |
||
284 | |||
285 | // To support recursive context lookup, instance properties + methods and lambdas |
||
286 | // the only way is using slower rendering time variable resolver. |
||
287 | 507 | if ($context['flags']['prop'] || $context['flags']['method'] || $context['flags']['mustlok'] || $context['flags']['mustlam'] || $context['flags']['lambda']) { |
|
288 | 385 | $L = $lookup ? ", $lookup[0]" : ''; |
|
289 | 385 | $A = $args ? ",$args[0]" : ''; |
|
290 | 385 | $E = $args ? ' ' . implode(' ', $args[1]) : ''; |
|
291 | 385 | return array(static::getFuncName($context, 'v', $exp) . "\$cx, \$in, isset($base) ? $base : null, array(" . Expression::listString($var) . "$L)$A)", $lookup ? "lookup $exp $lookup[1]" : "$exp$E"); |
|
292 | } |
||
293 | |||
294 | 123 | $n = Expression::arrayString($var); |
|
295 | 123 | $k = array_pop($var); |
|
296 | 123 | $L = $lookup ? "[{$lookup[0]}]" : ''; |
|
297 | 123 | $p = $lookup ? $n : (count($var) ? Expression::arrayString($var) : ''); |
|
298 | |||
299 | 123 | $checks = array(); |
|
300 | 123 | if ($levels > 0) { |
|
301 | 11 | $checks[] = "isset($base)"; |
|
302 | } |
||
303 | 123 | if (!$spvar) { |
|
304 | 119 | if (($levels === 0) && $p) { |
|
305 | 13 | $checks[] = "isset($base$p)"; |
|
306 | } |
||
307 | 119 | $checks[] = "is_array($base$p)"; |
|
308 | } |
||
309 | 123 | $checks[] = "isset($base$n$L)"; |
|
310 | 123 | $check = ((count($checks) > 1) ? '(' : '') . implode(' && ', $checks) . ((count($checks) > 1) ? ')' : ''); |
|
311 | |||
312 | 123 | $lenStart = ''; |
|
313 | 123 | $lenEnd = ''; |
|
314 | |||
315 | 123 | if ($context['flags']['jslen']) { |
|
316 | 50 | if (($lookup === null) && ($k === 'length')) { |
|
317 | 1 | array_pop($checks); |
|
318 | 1 | $lenStart = '(' . ((count($checks) > 1) ? '(' : '') . implode(' && ', $checks) . ((count($checks) > 1) ? ')' : '') . " ? count($base" . Expression::arrayString($var) . ') : '; |
|
319 | 1 | $lenEnd = ')'; |
|
320 | } |
||
321 | } |
||
322 | |||
323 | 123 | return array("($check ? $base$n$L : $lenStart" . ($context['flags']['debug'] ? (static::getFuncName($context, 'miss', '') . "\$cx, '$exp')") : 'null' ) . ")$lenEnd", $lookup ? "lookup $exp $lookup[1]" : $exp); |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Return compiled PHP code for a handlebars token |
||
328 | * |
||
329 | * @param array<string,array|string|integer> $context current compile context |
||
330 | * @param array<string,array|boolean> $info parsed information |
||
331 | * |
||
332 | * @return string Return compiled code segment for the token |
||
333 | */ |
||
334 | 641 | protected static function compileToken(&$context, $info) { |
|
335 | 641 | list($raw, $vars, $token, $indent) = $info; |
|
336 | |||
337 | 641 | $context['tokens']['partialind'] = $indent; |
|
338 | 641 | $context['currentToken'] = $token; |
|
339 | |||
340 | 641 | if ($ret = static::operator($token[Token::POS_OP], $context, $vars)) { |
|
341 | 396 | return $ret; |
|
342 | } |
||
343 | |||
344 | 499 | if (isset($vars[0][0])) { |
|
345 | 465 | if ($ret = static::customHelper($context, $vars, $raw, true)) { |
|
346 | 118 | return static::compileOutput($context, $ret, 'FIXME: helper', $raw, false); |
|
347 | } |
||
348 | 354 | if ($context['flags']['else'] && ($vars[0][0] === 'else')) { |
|
349 | 34 | return static::doElse($context, $vars); |
|
350 | } |
||
351 | 331 | if ($vars[0][0] === 'lookup') { |
|
352 | 4 | return static::compileLookup($context, $vars, $raw); |
|
353 | } |
||
354 | 327 | if ($vars[0][0] === 'log') { |
|
355 | 2 | return static::compileLog($context, $vars, $raw); |
|
356 | } |
||
357 | } |
||
358 | |||
359 | 367 | return static::compileVariable($context, $vars, $raw, false); |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * handle partial |
||
364 | * |
||
365 | * @param array<string,array|string|integer> $context current compile context |
||
366 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
367 | * |
||
368 | * @return string Return compiled code segment for the partial |
||
369 | */ |
||
370 | 97 | public static function partial(&$context, $vars) { |
|
371 | 97 | Parser::getBlockParams($vars); |
|
372 | 97 | $pid = Parser::getPartialBlock($vars); |
|
373 | 97 | $p = array_shift($vars); |
|
374 | 97 | if ($context['flags']['runpart']) { |
|
375 | 88 | if (!isset($vars[0])) { |
|
376 | 81 | $vars[0] = $context['flags']['partnc'] ? array(0, 'null') : array(); |
|
377 | } |
||
378 | 88 | $v = static::getVariableNames($context, $vars); |
|
379 | 88 | $tag = ">$p[0] " .implode(' ', $v[1]); |
|
380 | 88 | if (Parser::isSubExp($p)) { |
|
381 | 5 | list($p) = static::compileSubExpression($context, $p[1]); |
|
382 | } else { |
||
383 | 83 | $p = "'$p[0]'"; |
|
384 | } |
||
385 | 88 | $sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : ''; |
|
386 | 88 | return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0],$pid$sp){$context['ops']['seperator']}"; |
|
387 | } |
||
388 | 9 | return isset($context['usedPartial'][$p[0]]) ? "{$context['ops']['seperator']}'" . Partial::compileStatic($context, $p[0]) . "'{$context['ops']['seperator']}" : $context['ops']['seperator']; |
|
389 | } |
||
390 | |||
391 | /** |
||
392 | * handle inline partial |
||
393 | * |
||
394 | * @param array<string,array|string|integer> $context current compile context |
||
395 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
396 | * |
||
397 | * @return string Return compiled code segment for the partial |
||
398 | */ |
||
399 | 11 | public static function inline(&$context, $vars) { |
|
410 | |||
411 | /** |
||
412 | * Return compiled PHP code for a handlebars inverted section 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 | * |
||
417 | * @return string Return compiled code segment for the token |
||
418 | */ |
||
419 | 38 | protected static function invertedSection(&$context, $vars) { |
|
423 | |||
424 | /** |
||
425 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
426 | * |
||
427 | * @param array<string,array|string|integer> $context current compile context |
||
428 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
429 | * @param boolean $inverted the logic will be inverted |
||
430 | * |
||
431 | * @return string Return compiled code segment for the token |
||
432 | */ |
||
433 | 63 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
434 | 63 | $bp = Parser::getBlockParams($vars); |
|
435 | 63 | $ch = array_shift($vars); |
|
436 | 63 | $inverted = $inverted ? 'true' : 'false'; |
|
437 | 63 | static::addUsageCount($context, 'helpers', $ch[0]); |
|
438 | 63 | $v = static::getVariableNames($context, $vars, $bp); |
|
439 | |||
440 | 63 | return $context['ops']['seperator'] . static::getFuncName($context, 'hbch', ($inverted ? '^' : '#') . implode(' ', $v[1])) . "\$cx, '$ch[0]', {$v[0]}, \$in, $inverted, function(\$cx, \$in) {{$context['ops']['f_start']}"; |
|
441 | } |
||
442 | |||
443 | /** |
||
444 | * Return compiled PHP code for a handlebars block end token |
||
445 | * |
||
446 | * @param array<string,array|string|integer> $context current compile context |
||
447 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
448 | * @param string|null $matchop should also match to this operator |
||
449 | * |
||
450 | * @return string Return compiled code segment for the token |
||
451 | */ |
||
452 | 331 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
453 | 331 | $pop = $context['stack'][count($context['stack']) - 1]; |
|
454 | |||
455 | 331 | switch (isset($context['helpers'][$context['currentToken'][Token::POS_INNERTAG]]) ? 'skip' : $context['currentToken'][Token::POS_INNERTAG]) { |
|
456 | 331 | case 'if': |
|
457 | 275 | case 'unless': |
|
458 | 82 | if ($pop === ':') { |
|
459 | 29 | array_pop($context['stack']); |
|
460 | 29 | return "{$context['ops']['cnd_end']}"; |
|
461 | } |
||
462 | 56 | if (!$context['flags']['nohbh']) { |
|
463 | 54 | return "{$context['ops']['cnd_else']}''{$context['ops']['cnd_end']}"; |
|
464 | } |
||
465 | 2 | break; |
|
466 | 271 | case 'with': |
|
467 | 31 | if (!$context['flags']['nohbh']) { |
|
468 | 30 | return "{$context['ops']['f_end']}}){$context['ops']['seperator']}"; |
|
469 | } |
||
470 | } |
||
471 | |||
472 | 250 | if ($pop === ':') { |
|
473 | array_pop($context['stack']); |
||
474 | return "{$context['ops']['f_end']}}){$context['ops']['seperator']}"; |
||
475 | } |
||
476 | |||
477 | switch($pop) { |
||
478 | 250 | case '#': |
|
479 | 225 | return "{$context['ops']['f_end']}}){$context['ops']['seperator']}"; |
|
480 | 33 | case '^': |
|
481 | 33 | return "{$context['ops']['cnd_else']}''{$context['ops']['cnd_end']}"; |
|
482 | } |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Return compiled PHP code for a handlebars block begin token |
||
487 | * |
||
488 | * @param array<string,array|string|integer> $context current compile context |
||
489 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
490 | * |
||
491 | * @return string Return compiled code segment for the token |
||
492 | */ |
||
493 | 247 | protected static function blockBegin(&$context, $vars) { |
|
513 | |||
514 | /** |
||
515 | * compile {{#foo}} token |
||
516 | * |
||
517 | * @param array<string,array|string|integer> $context current compile context |
||
518 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
519 | * @param boolean $isEach the section is #each |
||
520 | * |
||
521 | * @return string|null Return compiled code segment for the token |
||
522 | */ |
||
523 | 166 | protected static function section(&$context, $vars, $isEach = false) { |
|
524 | 166 | $bs = 'null'; |
|
525 | 166 | $be = ''; |
|
526 | 166 | if ($isEach) { |
|
527 | 50 | $bp = Parser::getBlockParams($vars); |
|
528 | 50 | $bs = $bp ? ('array(' . Expression::listString($bp) . ')') : 'null'; |
|
529 | 50 | $be = $bp ? " as |$bp[0] $bp[1]|" : ''; |
|
530 | 50 | array_shift($vars); |
|
531 | } |
||
532 | 166 | if ($context['flags']['lambda'] && !$isEach) { |
|
533 | 69 | $V = array_shift($vars); |
|
534 | 69 | $v = static::getVariableName($context, $V, null, count($vars) ? static::getVariableNames($context, $vars) : array('0',array(''))); |
|
535 | } else { |
||
536 | 97 | $v = static::getVariableNameOrSubExpression($context, $vars[0]); |
|
537 | } |
||
538 | 166 | $each = $isEach ? 'true' : 'false'; |
|
539 | 166 | 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']}"; |
|
540 | } |
||
541 | |||
542 | /** |
||
543 | * compile {{with}} token |
||
544 | * |
||
545 | * @param array<string,array|string|integer> $context current compile context |
||
546 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
547 | * |
||
548 | * @return string|null Return compiled code segment for the token |
||
549 | */ |
||
550 | 30 | protected static function with(&$context, $vars) { |
|
557 | |||
558 | /** |
||
559 | * Return compiled PHP code for a handlebars custom helper token |
||
560 | * |
||
561 | * @param array<string,array|string|integer> $context current compile context |
||
562 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
563 | * @param boolean $raw is this {{{ token or not |
||
564 | * @param boolean $nosep true to compile without seperator |
||
565 | * @param boolean $subExp true when compile for subexpression |
||
566 | * |
||
567 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
568 | */ |
||
569 | 474 | protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) { |
|
591 | |||
592 | /** |
||
593 | * Return compiled PHP code for a handlebars else token |
||
594 | * |
||
595 | * @param array<string,array|string|integer> $context current compile context |
||
596 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
597 | * |
||
598 | * @return string Return compiled code segment for the token when the token is else |
||
599 | */ |
||
600 | 56 | protected static function doElse(&$context, $vars) { |
|
611 | |||
612 | /** |
||
613 | * Return compiled PHP code for a handlebars log token |
||
614 | * |
||
615 | * @param array<string,array|string|integer> $context current compile context |
||
616 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
617 | * @param boolean $raw is this {{{ token or not |
||
618 | * |
||
619 | * @return string Return compiled code segment for the token |
||
620 | */ |
||
621 | 2 | protected static function compileLog(&$context, &$vars, $raw) { |
|
626 | |||
627 | /** |
||
628 | * Return compiled PHP code for a handlebars lookup token |
||
629 | * |
||
630 | * @param array<string,array|string|integer> $context current compile context |
||
631 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
632 | * @param boolean $raw is this {{{ token or not |
||
633 | * @param boolean $nosep true to compile without seperator |
||
634 | * |
||
635 | * @return string Return compiled code segment for the token |
||
636 | */ |
||
637 | 6 | protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) { |
|
649 | |||
650 | /** |
||
651 | * Return compiled PHP code for template output |
||
652 | * |
||
653 | * @param array<string,array|string|integer> $context current compile context |
||
654 | * @param string $variable PHP code for the variable |
||
655 | * @param string $expression normalized handlebars expression |
||
656 | * @param boolean $raw is this {{{ token or not |
||
657 | * @param boolean $nosep true to compile without seperator |
||
658 | * |
||
659 | * @return string Return compiled code segment for the token |
||
660 | */ |
||
661 | 476 | protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) { |
|
669 | |||
670 | /** |
||
671 | * Return compiled PHP code for a handlebars variable token |
||
672 | * |
||
673 | * @param array<string,array|string|integer> $context current compile context |
||
674 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
675 | * @param boolean $raw is this {{{ token or not |
||
676 | * @param boolean $nosep true to compile without seperator |
||
677 | * |
||
678 | * @return string Return compiled code segment for the token |
||
679 | */ |
||
680 | 370 | protected static function compileVariable(&$context, &$vars, $raw, $nosep) { |
|
689 | |||
690 | /** |
||
691 | * Add usage count to context |
||
692 | * |
||
693 | * @param array<string,array|string|integer> $context current context |
||
694 | * @param string $category category name, can be one of: 'var', 'helpers', 'runtime' |
||
695 | * @param string $name used name |
||
696 | * @param integer $count increment |
||
697 | * |
||
698 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
699 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
700 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
701 | */ |
||
702 | 616 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
708 | } |
||
709 | |||
710 |
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.