Skip to content

Commit

Permalink
minor #3509 Some code style fixes (dionisvl)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Some code style fixes

Fixes that:
- Type cast is unnecessary
- Unnecessary curly braces syntax for variable
- Operations priority might differ that except

Commits
-------

17c6371 Some code style fixes
  • Loading branch information
fabpot committed May 14, 2021
2 parents 491aa7d + 17c6371 commit 9e6e700
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ public function parsePrimaryExpression()
$this->parser->getStream()->next();
$node = new NameExpression($token->getValue(), $token->getLine());
break;
} elseif (isset($this->unaryOperators[$token->getValue()])) {
}

if (isset($this->unaryOperators[$token->getValue()])) {
$class = $this->unaryOperators[$token->getValue()]['class'];
if (!\in_array($class, [NegUnary::class, PosUnary::class])) {
throw new SyntaxError(sprintf('Unexpected unary operator "%s".', $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
Expand Down
5 changes: 2 additions & 3 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ function twig_slice(Environment $env, $item, $start, $length = null, $preserveKe

$item = (string) $item;

return (string) mb_substr($item, $start, $length, $env->getCharset());
return mb_substr($item, $start, $length, $env->getCharset());
}

/**
Expand Down Expand Up @@ -803,8 +803,8 @@ function twig_get_array_keys_filter($array)
$array = $array->getIterator();
}

$keys = [];
if ($array instanceof \Iterator) {
$keys = [];
$array->rewind();
while ($array->valid()) {
$keys[] = $array->key();
Expand All @@ -814,7 +814,6 @@ function twig_get_array_keys_filter($array)
return $keys;
}

$keys = [];
foreach ($array as $key => $item) {
$keys[] = $key;
}
Expand Down
1 change: 0 additions & 1 deletion src/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function setTemplate(string $name, string $template): void

public function getSourceContext(string $name): Source
{
$name = (string) $name;
if (!isset($this->templates[$name])) {
throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct($paths = [], string $rootPath = null)
*/
public function getPaths(string $namespace = self::MAIN_NAMESPACE): array
{
return isset($this->paths[$namespace]) ? $this->paths[$namespace] : [];
return $this->paths[$namespace] ?? [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Node/IncludeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(AbstractExpression $expr, ?AbstractExpression $varia
$nodes['variables'] = $variables;
}

parent::__construct($nodes, ['only' => (bool) $only, 'ignore_missing' => (bool) $ignoreMissing], $lineno, $tag);
parent::__construct($nodes, ['only' => $only, 'ignore_missing' => $ignoreMissing], $lineno, $tag);
}

public function compile(Compiler $compiler): void
Expand Down
2 changes: 1 addition & 1 deletion src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function getEscaperFilter(string $type, Node $node): FilterExpression
{
$line = $node->getTemplateLine();
$name = new ConstantExpression('escape', $line);
$args = new Node([new ConstantExpression((string) $type, $line), new ConstantExpression(null, $line), new ConstantExpression(true, $line)]);
$args = new Node([new ConstantExpression($type, $line), new ConstantExpression(null, $line), new ConstantExpression(true, $line)]);

return new FilterExpression($node, $name, $args, $line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function getBlockStack(): array

public function peekBlockStack()
{
return isset($this->blockStack[\count($this->blockStack) - 1]) ? $this->blockStack[\count($this->blockStack) - 1] : null;
return $this->blockStack[\count($this->blockStack) - 1] ?? null;
}

public function popBlockStack(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Profiler/Dumper/BlackfireDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function dump(Profile $profile): string
$str = <<<EOF
file-format: BlackfireProbe
cost-dimensions: wt mu pmu
request-start: {$start}
request-start: $start
EOF;

foreach ($data as $name => $values) {
$str .= "{$name}//{$values['ct']} {$values['wt']} {$values['mu']} {$values['pmu']}\n";
$str .= "$name//{$values['ct']} {$values['wt']} {$values['mu']} {$values['pmu']}\n";
}

return $str;
Expand Down
2 changes: 1 addition & 1 deletion src/Test/NodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function getEnvironment()

protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';
$line = $line > 0 ? "// line $line\n" : '';

return sprintf('%s($context["%s"] ?? null)', $line, $name);
}
Expand Down
3 changes: 1 addition & 2 deletions src/TokenParser/ForTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ public function parse(Token $token): Node
$keyTarget = $targets->getNode(0);
$keyTarget = new AssignNameExpression($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
$valueTarget = $targets->getNode(1);
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
} else {
$keyTarget = new AssignNameExpression('_key', $lineno);
$valueTarget = $targets->getNode(0);
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
}
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());

return new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, $lineno, $this->getTag());
}
Expand Down

0 comments on commit 9e6e700

Please sign in to comment.