Skip to content

Commit

Permalink
do php-cs-fix, now follow PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
zordius committed Apr 7, 2019
1 parent 1537a07 commit ea0b89f
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
.phpunit.result.cache
.php_cs.cache
vendor/
build/result/
78 changes: 50 additions & 28 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Compiler extends Validator
*
* @return string|null generated PHP code
*/
public static function compileTemplate(&$context, $template) {
public static function compileTemplate(&$context, $template)
{
array_unshift($context['parsed'], array());
Validator::verify($context, $template);
static::$lastParsed = $context['parsed'];
Expand Down Expand Up @@ -79,7 +80,8 @@ public static function compileTemplate(&$context, $template) {
*
* @return string Composed PHP code
*/
public static function composePHPRender($context, $code) {
public static function composePHPRender($context, $code)
{
$flagJStrue = Expression::boolString($context['flags']['jstrue']);
$flagJSObj = Expression::boolString($context['flags']['jsobj']);
$flagJSLen = Expression::boolString($context['flags']['jslen']);
Expand Down Expand Up @@ -155,7 +157,8 @@ public static function composePHPRender($context, $code) {
* @expect "lala_abctest3(" when input array('flags' => array('standalone' => 1, 'debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 0, 'funcprefix' => 'lala_abc'), 'test3', ''
* @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('standalone' => 0, 'debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc'
*/
protected static function getFuncName(&$context, $name, $tag) {
protected static function getFuncName(&$context, $name, $tag)
{
static::addUsageCount($context, 'runtime', $name);

if ($context['flags']['debug'] && ($name != 'miss')) {
Expand All @@ -182,7 +185,8 @@ protected static function getFuncName(&$context, $name, $tag) {
* @expect array('array(array($in,$in),array())', array('this', 'this')) when input array('flags'=>array('spvar'=>true)), array(null, null)
* @expect array('array(array(),array(\'a\'=>$in))', array('this')) when input array('flags'=>array('spvar'=>true)), array('a' => null)
*/
protected static function getVariableNames(&$context, $vn, $blockParams = null) {
protected static function getVariableNames(&$context, $vn, $blockParams = null)
{
$vars = array(array(), array());
$exps = array();
foreach ($vn as $i => $v) {
Expand All @@ -206,7 +210,8 @@ protected static function getVariableNames(&$context, $vn, $blockParams = null)
*
* @return array<string> code representing passed expression
*/
public static function compileSubExpression(&$context, $vars) {
public static function compileSubExpression(&$context, $vars)
{
$ret = static::customHelper($context, $vars, true, true, true);

if (($ret === null) && $context['flags']['lambda']) {
Expand All @@ -224,7 +229,8 @@ public static function compileSubExpression(&$context, $vars) {
*
* @return array<string> variable names
*/
protected static function getVariableNameOrSubExpression(&$context, $var) {
protected static function getVariableNameOrSubExpression(&$context, $var)
{
return Parser::isSubExp($var) ? static::compileSubExpression($context, $var[1]) : static::getVariableName($context, $var);
}

Expand Down Expand Up @@ -257,7 +263,8 @@ protected static function getVariableNameOrSubExpression(&$context, $var) {
* @expect array('(($inary && 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')
* @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')
*/
protected static function getVariableName(&$context, $var, $lookup = null, $args = null) {
protected static function getVariableName(&$context, $var, $lookup = null, $args = null)
{
if (isset($var[0]) && ($var[0] === Parser::LITERAL)) {
if ($var[1] === "undefined") {
$var[1] = "null";
Expand Down Expand Up @@ -328,7 +335,7 @@ protected static function getVariableName(&$context, $var, $lookup = null, $args
}
}

return array("($check ? $base$n$L : $lenStart" . ($context['flags']['debug'] ? (static::getFuncName($context, 'miss', '') . "\$cx, '$exp')") : 'null' ) . ")$lenEnd", $lookup ? "lookup $exp $lookup[1]" : $exp);
return array("($check ? $base$n$L : $lenStart" . ($context['flags']['debug'] ? (static::getFuncName($context, 'miss', '') . "\$cx, '$exp')") : 'null') . ")$lenEnd", $lookup ? "lookup $exp $lookup[1]" : $exp);
}

/**
Expand All @@ -339,7 +346,8 @@ protected static function getVariableName(&$context, $var, $lookup = null, $args
*
* @return string Return compiled code segment for the token
*/
protected static function compileToken(&$context, $info) {
protected static function compileToken(&$context, $info)
{
list($raw, $vars, $token, $indent) = $info;

$context['tokens']['partialind'] = $indent;
Expand Down Expand Up @@ -375,7 +383,8 @@ protected static function compileToken(&$context, $info) {
*
* @return string Return compiled code segment for the partial
*/
public static function partial(&$context, $vars) {
public static function partial(&$context, $vars)
{
Parser::getBlockParams($vars);
$pid = Parser::getPartialBlock($vars);
$p = array_shift($vars);
Expand Down Expand Up @@ -404,7 +413,8 @@ public static function partial(&$context, $vars) {
*
* @return string Return compiled code segment for the partial
*/
public static function inline(&$context, $vars) {
public static function inline(&$context, $vars)
{
Parser::getBlockParams($vars);
list($code) = array_shift($vars);
$p = array_shift($vars);
Expand All @@ -424,7 +434,8 @@ public static function inline(&$context, $vars) {
*
* @return string Return compiled code segment for the token
*/
protected static function invertedSection(&$context, $vars) {
protected static function invertedSection(&$context, $vars)
{
$v = static::getVariableName($context, $vars[0]);
return "{$context['ops']['cnd_start']}(" . static::getFuncName($context, 'isec', '^' . $v[1]) . "\$cx, {$v[0]})){$context['ops']['cnd_then']}";
}
Expand All @@ -438,7 +449,8 @@ protected static function invertedSection(&$context, $vars) {
*
* @return string Return compiled code segment for the token
*/
protected static function blockCustomHelper(&$context, $vars, $inverted = false) {
protected static function blockCustomHelper(&$context, $vars, $inverted = false)
{
$bp = Parser::getBlockParams($vars);
$ch = array_shift($vars);
$inverted = $inverted ? 'true' : 'false';
Expand All @@ -457,7 +469,8 @@ protected static function blockCustomHelper(&$context, $vars, $inverted = false)
*
* @return string Return compiled code segment for the token
*/
protected static function blockEnd(&$context, &$vars, $matchop = NULL) {
protected static function blockEnd(&$context, &$vars, $matchop = null)
{
$pop = $context['stack'][count($context['stack']) - 1];

switch (isset($context['helpers'][$context['currentToken'][Token::POS_INNERTAG]]) ? 'skip' : $context['currentToken'][Token::POS_INNERTAG]) {
Expand All @@ -482,7 +495,7 @@ protected static function blockEnd(&$context, &$vars, $matchop = NULL) {
return "{$context['ops']['f_end']}}){$context['ops']['seperator']}";
}

switch($pop) {
switch ($pop) {
case '#':
return "{$context['ops']['f_end']}}){$context['ops']['seperator']}";
case '^':
Expand All @@ -498,7 +511,8 @@ protected static function blockEnd(&$context, &$vars, $matchop = NULL) {
*
* @return string Return compiled code segment for the token
*/
protected static function blockBegin(&$context, $vars) {
protected static function blockBegin(&$context, $vars)
{
$v = isset($vars[1]) ? static::getVariableNameOrSubExpression($context, $vars[1]) : array(null, array());
if (!$context['flags']['nohbh']) {
switch (isset($vars[0][0]) ? $vars[0][0] : null) {
Expand Down Expand Up @@ -528,7 +542,8 @@ protected static function blockBegin(&$context, $vars) {
*
* @return string|null Return compiled code segment for the token
*/
protected static function section(&$context, $vars, $isEach = false) {
protected static function section(&$context, $vars, $isEach = false)
{
$bs = 'null';
$be = '';
if ($isEach) {
Expand All @@ -555,7 +570,8 @@ protected static function section(&$context, $vars, $isEach = false) {
*
* @return string|null Return compiled code segment for the token
*/
protected static function with(&$context, $vars) {
protected static function with(&$context, $vars)
{
$v = isset($vars[1]) ? static::getVariableNameOrSubExpression($context, $vars[1]) : array(null, array());
$bp = Parser::getBlockParams($vars);
$bs = $bp ? ('array(' . Expression::listString($bp) . ')') : 'null';
Expand All @@ -574,7 +590,8 @@ protected static function with(&$context, $vars) {
*
* @return string|null Return compiled code segment for the token when the token is custom helper
*/
protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false) {
protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp = false)
{
if (count($vars[0]) > 1) {
return;
}
Expand Down Expand Up @@ -605,13 +622,14 @@ protected static function customHelper(&$context, $vars, $raw, $nosep, $subExp =
*
* @return string Return compiled code segment for the token when the token is else
*/
protected static function doElse(&$context, $vars) {
protected static function doElse(&$context, $vars)
{
$v = $context['stack'][count($context['stack']) - 2];

if ((($v === '[if]') && !isset($context['helpers']['if'])) ||
(($v === '[unless]') && !isset($context['helpers']['unless']))) {
$context['stack'][] = ':';
return "{$context['ops']['cnd_else']}";
$context['stack'][] = ':';
return "{$context['ops']['cnd_else']}";
}

return "{$context['ops']['f_end']}}, function(\$cx, \$in) {{$context['ops']['array_check']}{$context['ops']['f_start']}";
Expand All @@ -626,7 +644,8 @@ protected static function doElse(&$context, $vars) {
*
* @return string Return compiled code segment for the token
*/
protected static function compileLog(&$context, &$vars, $raw) {
protected static function compileLog(&$context, &$vars, $raw)
{
array_shift($vars);
$v = static::getVariableNames($context, $vars);
return $context['ops']['seperator'] . static::getFuncName($context, 'lo', $v[1]) . "\$cx, {$v[0]}){$context['ops']['seperator']}";
Expand All @@ -642,7 +661,8 @@ protected static function compileLog(&$context, &$vars, $raw) {
*
* @return string Return compiled code segment for the token
*/
protected static function compileLookup(&$context, &$vars, $raw, $nosep = false) {
protected static function compileLookup(&$context, &$vars, $raw, $nosep = false)
{
$v2 = static::getVariableName($context, $vars[2]);
$v = static::getVariableName($context, $vars[1], $v2);
$sep = $nosep ? '' : $context['ops']['seperator'];
Expand All @@ -666,7 +686,8 @@ protected static function compileLookup(&$context, &$vars, $raw, $nosep = false)
*
* @return string Return compiled code segment for the token
*/
protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep) {
protected static function compileOutput(&$context, $variable, $expression, $raw, $nosep)
{
$sep = $nosep ? '' : $context['ops']['seperator'];
if ($context['flags']['hbesc'] || $context['flags']['jsobj'] || $context['flags']['jstrue'] || $context['flags']['debug'] || $nosep) {
return $sep . static::getFuncName($context, $raw ? 'raw' : $context['ops']['enc'], $expression) . "\$cx, $variable)$sep";
Expand All @@ -685,7 +706,8 @@ protected static function compileOutput(&$context, $variable, $expression, $raw,
*
* @return string Return compiled code segment for the token
*/
protected static function compileVariable(&$context, &$vars, $raw, $nosep) {
protected static function compileVariable(&$context, &$vars, $raw, $nosep)
{
if ($context['flags']['lambda']) {
$V = array_shift($vars);
$v = static::getVariableName($context, $V, null, count($vars) ? static::getVariableNames($context, $vars) : array('0',array('')));
Expand All @@ -707,11 +729,11 @@ protected static function compileVariable(&$context, &$vars, $raw, $nosep) {
* @expect 3 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname'
* @expect 5 when input array('usedCount' => array('test' => array('testname' => 2))), 'test', 'testname', 3
*/
protected static function addUsageCount(&$context, $category, $name, $count = 1) {
protected static function addUsageCount(&$context, $category, $name, $count = 1)
{
if (!isset($context['usedCount'][$category][$name])) {
$context['usedCount'][$category][$name] = 0;
}
return ($context['usedCount'][$category][$name] += $count);
}
}

10 changes: 6 additions & 4 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Context extends Flags
*
* @return array<string,array|string|integer> Context from options
*/
public static function create($options) {
public static function create($options)
{
if (!is_array($options)) {
$options = array();
}
Expand Down Expand Up @@ -194,7 +195,8 @@ public static function create($options) {
* @expect array('flags' => array('exhlp' => 1), 'helpers' => array('\\LightnCandy\\Runtime::raw' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('\\LightnCandy\\Runtime::raw'))
* @expect array('flags' => array('exhlp' => 1), 'helpers' => array('test' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('test' => '\\LightnCandy\\Runtime::raw'))
*/
protected static function updateHelperTable(&$context, $options, $tname = 'helpers') {
protected static function updateHelperTable(&$context, $options, $tname = 'helpers')
{
if (isset($options[$tname]) && is_array($options[$tname])) {
foreach ($options[$tname] as $name => $func) {
$tn = is_int($name) ? $func : $name;
Expand Down Expand Up @@ -223,7 +225,8 @@ protected static function updateHelperTable(&$context, $options, $tname = 'helpe
* @param array<string,array|string|integer> $context master context
* @param array<string,array|string|integer> $tmp another context will be overwrited into master context
*/
public static function merge(&$context, $tmp) {
public static function merge(&$context, $tmp)
{
$context['error'] = $tmp['error'];
$context['helpers'] = $tmp['helpers'];
$context['partials'] = $tmp['partials'];
Expand All @@ -234,4 +237,3 @@ public static function merge(&$context, $tmp) {
$context['usedPartial'] = $tmp['usedPartial'];
}
}

10 changes: 6 additions & 4 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Encoder
* @expect 'a,' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false)
* @expect 'a,false' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false)
*/
public static function raw($cx, $v, $ex = 0) {
public static function raw($cx, $v, $ex = 0)
{
if ($ex) {
return $v;
}
Expand Down Expand Up @@ -95,7 +96,8 @@ public static function raw($cx, $v, $ex = 0) {
* @expect 'a&amp;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
* @expect 'a&#039;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
*/
public static function enc($cx, $var) {
public static function enc($cx, $var)
{
return htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8');
}

Expand All @@ -112,8 +114,8 @@ public static function enc($cx, $var) {
* @expect 'a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
* @expect '&#x60;a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b'
*/
public static function encq($cx, $var) {
public static function encq($cx, $var)
{
return str_replace(array('=', '`', '&#039;'), array('&#x3D;', '&#x60;', '&#x27;'), htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'));
}
}

Loading

0 comments on commit ea0b89f

Please sign in to comment.