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 | 720 | public static function compileTemplate(&$context, $template) { |
|
| 44 | 720 | array_unshift($context['parsed'], array()); |
|
| 45 | 720 | Validator::verify($context, $template); |
|
| 46 | |||
| 47 | 720 | if (count($context['error'])) { |
|
| 48 | 70 | return; |
|
| 49 | } |
||
| 50 | |||
| 51 | // Do PHP code generation. |
||
| 52 | 651 | Parser::setDelimiter($context); |
|
| 53 | |||
| 54 | // Handle dynamic partials |
||
| 55 | 651 | Partial::handleDynamic($context); |
|
| 56 | |||
| 57 | 651 | $code = ''; |
|
| 58 | 651 | foreach ($context['parsed'][0] as $info) { |
|
| 59 | 651 | if (is_array($info)) { |
|
| 60 | 610 | $context['tokens']['current']++; |
|
| 61 | 610 | $tmpl = static::compileToken($context, $info); |
|
| 62 | 610 | if ($tmpl == $context['ops']['seperator']) { |
|
| 63 | 1 | $tmpl = ''; |
|
| 64 | } else { |
||
| 65 | 609 | $tmpl = "'$tmpl'"; |
|
| 66 | } |
||
| 67 | 610 | $code .= $tmpl; |
|
| 68 | } else { |
||
| 69 | 651 | $code .= $info; |
|
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | 651 | static::$lastParsed = array_shift($context['parsed']); |
|
| 74 | |||
| 75 | 651 | 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 | 650 | public static function composePHPRender($context, $code) { |
|
| 87 | 650 | $flagJStrue = Expression::boolString($context['flags']['jstrue']); |
|
| 88 | 650 | $flagJSObj = Expression::boolString($context['flags']['jsobj']); |
|
| 89 | 650 | $flagSPVar = Expression::boolString($context['flags']['spvar']); |
|
| 90 | 650 | $flagProp = Expression::boolString($context['flags']['prop']); |
|
| 91 | 650 | $flagMethod = Expression::boolString($context['flags']['method']); |
|
| 92 | 650 | $flagLambda = Expression::boolString($context['flags']['lambda']); |
|
| 93 | 650 | $flagMustlok = Expression::boolString($context['flags']['mustlok']); |
|
| 94 | 650 | $flagMustlam = Expression::boolString($context['flags']['mustlam']); |
|
| 95 | 650 | $flagEcho = Expression::boolString($context['flags']['echo']); |
|
| 96 | 650 | $flagPartNC = Expression::boolString($context['flags']['partnc']); |
|
| 97 | 650 | $flagKnownHlp = Expression::boolString($context['flags']['knohlp']); |
|
| 98 | |||
| 99 | 650 | $constants = Exporter::constants($context); |
|
| 100 | 650 | $helpers = Exporter::helpers($context); |
|
| 101 | 650 | $bhelpers = Exporter::helpers($context, 'blockhelpers'); |
|
| 102 | 650 | $hbhelpers = Exporter::helpers($context, 'hbhelpers'); |
|
| 103 | 650 | $partials = implode(",\n", $context['partialCode']); |
|
| 104 | 650 | $debug = Runtime::DEBUG_ERROR_LOG; |
|
| 105 | 650 | $use = $context['flags']['standalone'] ? Exporter::runtime($context) : "use {$context['runtime']} as LR;"; |
|
| 106 | |||
| 107 | // Return generated PHP code string. |
||
| 108 | 650 | return "{$use}return function (\$in, \$options = null) { |
|
| 109 | \$cx = array( |
||
| 110 | 'flags' => array( |
||
| 111 | 'jstrue' => $flagJStrue, |
||
| 112 | 'jsobj' => $flagJSObj, |
||
| 113 | 'spvar' => $flagSPVar, |
||
| 114 | 'prop' => $flagProp, |
||
| 115 | 'method' => $flagMethod, |
||
| 116 | 'lambda' => $flagLambda, |
||
| 117 | 'mustlok' => $flagMustlok, |
||
| 118 | 'mustlam' => $flagMustlam, |
||
| 119 | 'echo' => $flagEcho, |
||
| 120 | 'partnc' => $flagPartNC, |
||
| 121 | 'knohlp' => $flagKnownHlp, |
||
| 122 | 'debug' => isset(\$options['debug']) ? \$options['debug'] : $debug, |
||
| 123 | ), |
||
| 124 | 'constants' => $constants, |
||
| 125 | 'helpers' => $helpers, |
||
| 126 | 'blockhelpers' => $bhelpers, |
||
| 127 | 'hbhelpers' => isset(\$options['helpers']) ? array_merge($hbhelpers, \$options['helpers']) : $hbhelpers, |
||
| 128 | 'partials' => array($partials), |
||
| 129 | 'scopes' => array(), |
||
| 130 | 'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in), |
||
| 131 | 'blparam' => array(), |
||
| 132 | 650 | 'runtime' => '{$context['runtime']}', |
|
| 133 | ); |
||
| 134 | 650 | {$context['renderex']} |
|
| 135 | 650 | {$context['ops']['op_start']}'$code'{$context['ops']['op_end']} |
|
| 136 | 650 | };"; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get function name for standalone or none standalone template. |
||
| 141 | * |
||
| 142 | * @param array<string,array|string|integer> $context Current context of compiler progress. |
||
| 143 | * @param string $name base function name |
||
| 144 | * @param string $tag original handlabars tag for debug |
||
| 145 | * |
||
| 146 | * @return string compiled Function name |
||
| 147 | * |
||
| 148 | * @expect 'LR::test(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test', '' |
||
| 149 | * @expect 'LR::test2(' when input array('flags' => array('standalone' => 0, 'debug' => 0), 'runtime' => 'Runtime'), 'test2', '' |
||
| 150 | * @expect "\$cx['funcs']['test3'](" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime'), 'test3', '' |
||
| 151 | * @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime'), 'test', 'abc' |
||
| 152 | */ |
||
| 153 | 586 | protected static function getFuncName(&$context, $name, $tag) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get string presentation of variables |
||
| 169 | * |
||
| 170 | * @param array<string,array|string|integer> $context current compile context |
||
| 171 | * @param array<array> $vn variable name array. |
||
| 172 | * @param array<string>|null $blockParams block param list |
||
| 173 | * |
||
| 174 | * @return array<string|array> variable names |
||
| 175 | * |
||
| 176 | * @expect array('array(array($in),array())', array('this')) when input array('flags'=>array('spvar'=>true)), array(null) |
||
| 177 | * @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null) |
||
| 178 | * @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null) |
||
| 179 | */ |
||
| 180 | 259 | protected static function getVariableNames(&$context, $vn, $blockParams = null) { |
|
| 181 | 259 | $vars = array(array(), array()); |
|
| 182 | 259 | $exps = array(); |
|
| 183 | 259 | foreach ($vn as $i => $v) { |
|
| 184 | 220 | $V = static::getVariableNameOrSubExpression($context, $v); |
|
| 185 | 220 | if (is_string($i)) { |
|
| 186 | 36 | $vars[1][] = "'$i'=>{$V[0]}"; |
|
| 187 | } else { |
||
| 188 | 206 | $vars[0][] = $V[0]; |
|
| 189 | } |
||
| 190 | 220 | $exps[] = $V[1]; |
|
| 191 | } |
||
| 192 | 259 | $bp = $blockParams ? (',array(' . Expression::listString($blockParams) . ')') : ''; |
|
| 193 | 259 | return array('array(array(' . implode(',', $vars[0]) . '),array(' . implode(',', $vars[1]) . ")$bp)", $exps); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get string presentation of a sub expression |
||
| 198 | * |
||
| 199 | * @param array<string,array|string|integer> $context current compile context |
||
| 200 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 201 | * |
||
| 202 | * @return array<string> code representing passed expression |
||
| 203 | */ |
||
| 204 | 41 | public static function compileSubExpression(&$context, $vars) { |
|
| 205 | 41 | $origSeperator = $context['ops']['seperator']; |
|
| 206 | 41 | $context['ops']['seperator'] = ''; |
|
| 207 | |||
| 208 | 41 | $ret = static::customHelper($context, $vars, true); |
|
| 209 | |||
| 210 | 41 | if (($ret === null) && $context['flags']['lambda']) { |
|
| 211 | 4 | $ret = static::compileVariable($context, $vars, true); |
|
| 212 | } |
||
| 213 | |||
| 214 | 41 | $context['ops']['seperator'] = $origSeperator; |
|
| 215 | |||
| 216 | 41 | return array($ret ? $ret : '', 'FIXME: $subExpression'); |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get string presentation of a subexpression or a variable |
||
| 221 | * |
||
| 222 | * @param array<array|string|integer> $context current compile context |
||
| 223 | * @param array<array|string|integer> $var variable parsed path |
||
| 224 | * |
||
| 225 | * @return array<string> variable names |
||
|
|
|||
| 226 | */ |
||
| 227 | 366 | protected static function getVariableNameOrSubExpression(&$context, $var) { |
|
| 228 | 366 | return Parser::isSubExp($var) ? static::compileSubExpression($context, $var[1]) : static::getVariableName($context, $var); |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get string presentation of a variable |
||
| 233 | * |
||
| 234 | * @param array<array|string|integer> $var variable parsed path |
||
| 235 | * @param array<array|string|integer> $context current compile context |
||
| 236 | * @param array<string>|null $lookup extra lookup string as valid PHP variable name |
||
| 237 | * |
||
| 238 | * @return array<string> variable names |
||
| 239 | * |
||
| 240 | * @expect array('$in', 'this') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(null) |
||
| 241 | * @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') |
||
| 242 | * @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') |
||
| 243 | * @expect array('true', 'true') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'true') |
||
| 244 | * @expect array('false', 'false') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, 'false') |
||
| 245 | * @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') |
||
| 246 | * @expect array('2', '2') when input array('flags'=>array('spvar'=>true,'debug'=>0,'prop'=>0,'method'=>0)), array(-1, '2') |
||
| 247 | * @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') |
||
| 248 | * @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') |
||
| 249 | * @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') |
||
| 250 | * @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') |
||
| 251 | * @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') |
||
| 252 | * @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"') |
||
| 253 | * @expect array('"a"', '"a"') when input array('flags'=>array('spvar'=>true,'debug'=>0)), array(-1, '"a"') |
||
| 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('((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') |
||
| 256 | * @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') |
||
| 257 | * @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') |
||
| 258 | * @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') |
||
| 259 | */ |
||
| 260 | 581 | protected static function getVariableName(&$context, $var, $lookup = null, $args = null) { |
|
| 261 | 581 | if (isset($var[0]) && ($var[0] === Parser::LITERAL)) { |
|
| 262 | 79 | if ($var[1] === "undefined") { |
|
| 263 | 2 | $var[1] = "null"; |
|
| 264 | } |
||
| 265 | 79 | return array($var[1], preg_replace('/\'(.*)\'/', '$1', $var[1])); |
|
| 266 | } |
||
| 267 | |||
| 268 | 542 | list($levels, $spvar, $var) = Expression::analyze($context, $var); |
|
| 269 | 542 | $exp = Expression::toString($levels, $spvar, $var); |
|
| 270 | 542 | $base = $spvar ? "\$cx['sp_vars']" : '$in'; |
|
| 271 | |||
| 272 | // change base when trace to parent |
||
| 273 | 542 | if ($levels > 0) { |
|
| 274 | 39 | if ($spvar) { |
|
| 275 | 2 | $base .= str_repeat("['_parent']", $levels); |
|
| 276 | } else { |
||
| 277 | 37 | $base = "\$cx['scopes'][count(\$cx['scopes'])-$levels]"; |
|
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | 542 | if ((count($var) == 0) || (($var[0] === null) && (count($var) == 1))) { |
|
| 282 | 134 | return array($base, $exp); |
|
| 283 | } |
||
| 284 | |||
| 285 | 485 | if ($var[0] === null) { |
|
| 286 | 1 | array_shift($var); |
|
| 287 | } |
||
| 288 | |||
| 289 | // To support recursive context lookup, instance properties + methods and lambdas |
||
| 290 | // the only way is using slower rendering time variable resolver. |
||
| 291 | 485 | if ($context['flags']['prop'] || $context['flags']['method'] || $context['flags']['mustlok'] || $context['flags']['mustlam'] || $context['flags']['lambda']) { |
|
| 292 | 386 | $L = $lookup ? ", $lookup[0]" : ''; |
|
| 293 | 386 | $A = $args ? ",$args[0]" : ''; |
|
| 294 | 386 | $E = $args ? ' ' . implode(' ', $args[1]) : ''; |
|
| 295 | 386 | 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"); |
|
| 296 | } |
||
| 297 | |||
| 298 | 100 | $n = Expression::arrayString($var); |
|
| 299 | 100 | array_pop($var); |
|
| 300 | 100 | $L = $lookup ? "[{$lookup[0]}]" : ''; |
|
| 301 | 100 | $p = $lookup ? $n : (count($var) ? Expression::arrayString($var) : ''); |
|
| 302 | |||
| 303 | 100 | return array("((isset($base$n$L) && is_array($base$p)) ? $base$n$L : " . ($context['flags']['debug'] ? (static::getFuncName($context, 'miss', '') . "\$cx, '$exp')") : 'null' ) . ')', $lookup ? "lookup $exp $lookup[1]" : $exp); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Return compiled PHP code for a handlebars token |
||
| 308 | * |
||
| 309 | * @param array<string,array|string|integer> $context current compile context |
||
| 310 | * @param array<string,array|boolean> $info parsed information |
||
| 311 | * |
||
| 312 | * @return string Return compiled code segment for the token |
||
| 313 | */ |
||
| 314 | 610 | protected static function compileToken(&$context, $info) { |
|
| 315 | 610 | list($raw, $vars, $token, $indent) = $info; |
|
| 316 | |||
| 317 | 610 | $context['tokens']['partialind'] = $indent; |
|
| 318 | 610 | $context['currentToken'] = $token; |
|
| 319 | |||
| 320 | // Do not touch the tag, keep it as is. |
||
| 321 | 610 | if ($raw === -1) { |
|
| 322 | return ".'" . Token::toString($token) . "'."; |
||
| 323 | } |
||
| 324 | |||
| 325 | 610 | if ($ret = static::operator($token[Token::POS_OP], $context, $vars)) { |
|
| 326 | 375 | return $ret; |
|
| 327 | } |
||
| 328 | |||
| 329 | 474 | if (isset($vars[0][0])) { |
|
| 330 | 442 | if ($ret = static::customHelper($context, $vars, $raw)) { |
|
| 331 | 112 | return $ret; |
|
| 332 | } |
||
| 333 | 337 | if ($vars[0][0] === 'else') { |
|
| 334 | 27 | return static::doElse($context, $vars); |
|
| 335 | } |
||
| 336 | 322 | if ($vars[0][0] === 'lookup') { |
|
| 337 | 2 | return static::compileLookup($context, $vars, $raw); |
|
| 338 | } |
||
| 339 | 320 | if ($vars[0][0] === 'log') { |
|
| 340 | 1 | return static::compileLog($context, $vars, $raw); |
|
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | 358 | return static::compileVariable($context, $vars, $raw); |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * handle partial |
||
| 349 | * |
||
| 350 | * @param array<string,array|string|integer> $context current compile context |
||
| 351 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 352 | * |
||
| 353 | * @return string Return compiled code segment for the partial |
||
| 354 | */ |
||
| 355 | 86 | public static function partial(&$context, $vars) { |
|
| 356 | // mustache spec: ignore missing partial |
||
| 357 | 86 | if (($context['usedFeature']['dynpartial'] === 0) && ($context['usedFeature']['inlpartial'] === 0) && !isset($context['usedPartial'][$vars[0][0]])) { |
|
| 358 | 1 | return $context['ops']['seperator']; |
|
| 359 | } |
||
| 360 | 85 | Parser::getBlockParams($vars); |
|
| 361 | 85 | $p = array_shift($vars); |
|
| 362 | 85 | if ($context['flags']['runpart']) { |
|
| 363 | 77 | if (!isset($vars[0])) { |
|
| 364 | 70 | $vars[0] = $context['flags']['partnc'] ? array(0, 'null') : array(); |
|
| 365 | } |
||
| 366 | 77 | $v = static::getVariableNames($context, $vars); |
|
| 367 | 77 | $tag = ">$p[0] " .implode(' ', $v[1]); |
|
| 368 | 77 | if (Parser::isSubExp($p)) { |
|
| 369 | 4 | list($p) = static::compileSubExpression($context, $p[1]); |
|
| 370 | } else { |
||
| 371 | 73 | $p = "'$p[0]'"; |
|
| 372 | } |
||
| 373 | 77 | $sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : ''; |
|
| 374 | 77 | return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0]$sp){$context['ops']['seperator']}"; |
|
| 375 | } |
||
| 376 | 8 | return "{$context['ops']['seperator']}'" . Partial::compileStatic($context, $p[0]) . "'{$context['ops']['seperator']}"; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * handle inline partial |
||
| 381 | * |
||
| 382 | * @param array<string,array|string|integer> $context current compile context |
||
| 383 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 384 | * |
||
| 385 | * @return string Return compiled code segment for the partial |
||
| 386 | */ |
||
| 387 | 10 | public static function inline(&$context, $vars) { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Return compiled PHP code for a handlebars inverted section begin token |
||
| 401 | * |
||
| 402 | * @param array<string,array|string|integer> $context current compile context |
||
| 403 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 404 | * |
||
| 405 | * @return string Return compiled code segment for the token |
||
| 406 | */ |
||
| 407 | 38 | protected static function invertedSection(&$context, $vars) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Return compiled PHP code for a handlebars block custom helper begin token |
||
| 414 | * |
||
| 415 | * @param array<string,array|string|integer> $context current compile context |
||
| 416 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 417 | * @param boolean $inverted the logic will be inverted |
||
| 418 | * |
||
| 419 | * @return string Return compiled code segment for the token |
||
| 420 | */ |
||
| 421 | 61 | protected static function blockCustomHelper(&$context, $vars, $inverted = false) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Return compiled PHP code for a handlebars block end token |
||
| 435 | * |
||
| 436 | * @param array<string,array|string|integer> $context current compile context |
||
| 437 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 438 | * @param string|null $matchop should also match to this operator |
||
| 439 | * |
||
| 440 | * @return string Return compiled code segment for the token |
||
| 441 | */ |
||
| 442 | 319 | protected static function blockEnd(&$context, &$vars, $matchop = NULL) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Return compiled PHP code for a handlebars block begin token |
||
| 481 | * |
||
| 482 | * @param array<string,array|string|integer> $context current compile context |
||
| 483 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 484 | * |
||
| 485 | * @return string Return compiled code segment for the token |
||
| 486 | */ |
||
| 487 | 235 | protected static function blockBegin(&$context, $vars) { |
|
| 507 | |||
| 508 | /** |
||
| 509 | * compile {{#foo}} token |
||
| 510 | * |
||
| 511 | * @param array<string,array|string|integer> $context current compile context |
||
| 512 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 513 | * @param boolean $isEach the section is #each |
||
| 514 | * |
||
| 515 | * @return string|null Return compiled code segment for the token |
||
| 516 | */ |
||
| 517 | 162 | protected static function section(&$context, $vars, $isEach = false) { |
|
| 538 | |||
| 539 | /** |
||
| 540 | * compile {{with}} token |
||
| 541 | * |
||
| 542 | * @param array<string,array|string|integer> $context current compile context |
||
| 543 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 544 | * |
||
| 545 | * @return string|null Return compiled code segment for the token |
||
| 546 | */ |
||
| 547 | 27 | protected static function with(&$context, $vars) { |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Return compiled PHP code for a handlebars custom helper token |
||
| 557 | * |
||
| 558 | * @param array<string,array|string|integer> $context current compile context |
||
| 559 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 560 | * @param boolean $raw is this {{{ token or not |
||
| 561 | * |
||
| 562 | * @return string|null Return compiled code segment for the token when the token is custom helper |
||
| 563 | */ |
||
| 564 | 450 | protected static function customHelper(&$context, $vars, $raw) { |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Return compiled PHP code for a handlebars else token |
||
| 579 | * |
||
| 580 | * @param array<string,array|string|integer> $context current compile context |
||
| 581 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 582 | * |
||
| 583 | * @return string Return compiled code segment for the token when the token is else |
||
| 584 | */ |
||
| 585 | 49 | protected static function doElse(&$context, $vars) { |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Return compiled PHP code for a handlebars log token |
||
| 598 | * |
||
| 599 | * @param array<string,array|string|integer> $context current compile context |
||
| 600 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 601 | * @param boolean $raw is this {{{ token or not |
||
| 602 | * |
||
| 603 | * @return string Return compiled code segment for the token |
||
| 604 | */ |
||
| 605 | 1 | protected static function compileLog(&$context, &$vars, $raw) { |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Return compiled PHP code for a handlebars lookup token |
||
| 613 | * |
||
| 614 | * @param array<string,array|string|integer> $context current compile context |
||
| 615 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 616 | * @param boolean $raw is this {{{ token or not |
||
| 617 | * |
||
| 618 | * @return string Return compiled code segment for the token |
||
| 619 | */ |
||
| 620 | 2 | protected static function compileLookup(&$context, &$vars, $raw) { |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Return compiled PHP code for a handlebars variable token |
||
| 632 | * |
||
| 633 | * @param array<string,array|string|integer> $context current compile context |
||
| 634 | * @param array<boolean|integer|string|array> $vars parsed arguments list |
||
| 635 | * @param boolean $raw is this {{{ token or not |
||
| 636 | * |
||
| 637 | * @return string Return compiled code segment for the token |
||
| 638 | */ |
||
| 639 | 361 | protected static function compileVariable(&$context, &$vars, $raw) { |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Add usage count to context |
||
| 655 | * |
||
| 656 | * @param array<string,array|string|integer> $context current context |
||
| 657 | * @param string $category ctegory name, can be one of: 'var', 'helpers', 'blockhelpers' |
||
| 658 | * @param string $name used name |
||
| 659 | * @param integer $count increment |
||
| 660 | * |
||
| 661 | * @expect 1 when input array('usedCount' => array('test' => array())), 'test', 'testname' |
||
| 662 | * @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname' |
||
| 663 | * @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3 |
||
| 664 | */ |
||
| 665 | 586 | protected static function addUsageCount(&$context, $category, $name, $count = 1) { |
|
| 671 | } |
||
| 672 | |||
| 673 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.