Skip to content

Commit

Permalink
Code style fix on Twig tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Jul 14, 2018
1 parent ea83b46 commit dd75ce5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 53 deletions.
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Twig/Node/TwigNodeMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TwigNodeMarkdown extends \Twig_Node implements \Twig_NodeOutputInterface
{
public function __construct(\Twig_Node $body, $lineno, $tag = 'markdown')
{
parent::__construct(array('body' => $body), array(), $lineno, $tag);
parent::__construct(['body' => $body], [], $lineno, $tag);
}
/**
* Compiles the node to PHP.
Expand Down
14 changes: 5 additions & 9 deletions system/src/Grav/Common/Twig/Node/TwigNodeSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ public function compile(\Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("switch (")
->write('switch (')
->subcompile($this->getNode('value'))
->raw(") {\n")
->indent();

foreach ($this->getNode('cases') as $case)
{
if (!$case->hasNode('body'))
{
foreach ($this->getNode('cases') as $case) {
if (!$case->hasNode('body')) {
continue;
}

foreach ($case->getNode('values') as $value)
{
foreach ($case->getNode('values') as $value) {
$compiler
->write('case ')
->subcompile($value)
Expand All @@ -53,8 +50,7 @@ public function compile(\Twig_Compiler $compiler)
->write("}\n");
}

if ($this->hasNode('default') && $this->getNode('default') !== null)
{
if ($this->hasNode('default') && $this->getNode('default') !== null) {
$compiler
->write("default:\n")
->write("{\n")
Expand Down
73 changes: 30 additions & 43 deletions system/src/Grav/Common/Twig/TokenParser/TwigTokenParserSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function parse(\Twig_Token $token)
$stream->expect(\Twig_Token::BLOCK_END_TYPE);

// There can be some whitespace between the {% switch %} and first {% case %} tag.
while ($stream->getCurrent()->getType() == \Twig_Token::TEXT_TYPE && trim($stream->getCurrent()->getValue()) == '')
{
while ($stream->getCurrent()->getType() === \Twig_Token::TEXT_TYPE && trim($stream->getCurrent()->getValue()) === '') {
$stream->next();
}

Expand All @@ -47,56 +46,45 @@ public function parse(\Twig_Token $token)
$expressionParser = $this->parser->getExpressionParser();

$default = null;
$cases = array();
$cases = [];
$end = false;

while (!$end)
{
while (!$end) {
$next = $stream->next();

switch ($next->getValue())
{
switch ($next->getValue()) {
case 'case':
{
$values = array();

while (true)
{
$values[] = $expressionParser->parsePrimaryExpression();
// Multiple allowed values?
if ($stream->test(\Twig_Token::OPERATOR_TYPE, 'or'))
{
$stream->next();
}
else
{
break;
}
$values = [];

while (true) {
$values[] = $expressionParser->parsePrimaryExpression();
// Multiple allowed values?
if ($stream->test(\Twig_Token::OPERATOR_TYPE, 'or')) {
$stream->next();
} else {
break;
}

$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideIfFork'));
$cases[] = new \Twig_Node(array(
'values' => new \Twig_Node($values),
'body' => $body
));
break;
}

$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideIfFork'));
$cases[] = new \Twig_Node([
'values' => new \Twig_Node($values),
'body' => $body
]);
break;

case 'default':
{
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$default = $this->parser->subparse(array($this, 'decideIfEnd'));
break;
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$default = $this->parser->subparse(array($this, 'decideIfEnd'));
break;

case 'endswitch':
{
$end = true;
break;
}
$end = true;
break;

default:
{
throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line %d)', $lineno), -1);
}
throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line %d)', $lineno), -1);
}
}

Expand Down Expand Up @@ -127,7 +115,6 @@ public function decideIfEnd(\Twig_Token $token)
return $token->test(array('endswitch'));
}


/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit dd75ce5

Please sign in to comment.