Skip to content

Commit

Permalink
Remove unused variables and unreachable code
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 8, 2023
1 parent 2d37866 commit 6d715e2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ public function loadTemplate(string $cls, string $name, int $index = null): Temp
$this->cache->load($key);
}

$source = null;
if (!class_exists($cls, false)) {
$source = $this->getLoader()->getSourceContext($name);
$content = $this->compileSource($source);
Expand Down
4 changes: 0 additions & 4 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,6 @@ public function parseSubscriptExpression($node)
}

if ($node instanceof NameExpression && null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))) {
if (!$arg instanceof ConstantExpression) {
throw new SyntaxError(sprintf('Dynamic macro names are not supported (called on "%s").', $node->getAttribute('name')), $token->getLine(), $stream->getSourceContext());
}

$name = $arg->getAttribute('value');

$node = new MethodCallExpression($node, 'macro_'.$name, $arguments, $lineno);
Expand Down
3 changes: 1 addition & 2 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ function twig_random(Environment $env, $values = null, $max = null)
}
} else {
$min = $values;
$max = $max;
}

return mt_rand((int) $min, (int) $max);
Expand Down Expand Up @@ -669,7 +668,7 @@ function twig_slice(Environment $env, $item, $start, $length = null, $preserveKe
return \array_slice($item, $start, $length, $preserveKeys);
}

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

/**
Expand Down
1 change: 0 additions & 1 deletion src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Node implements \Countable, \IteratorAggregate
protected $lineno;
protected $tag;

private $name;
private $sourceContext;

/**
Expand Down
9 changes: 4 additions & 5 deletions src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function enterNode(Node $node, Environment $env): Node
} elseif ($node instanceof AutoEscapeNode) {
$this->statusStack[] = $node->getAttribute('value');
} elseif ($node instanceof BlockNode) {
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
$this->statusStack[] = $this->blocks[$node->getAttribute('name')] ?? $this->needEscaping();
} elseif ($node instanceof ImportNode) {
$this->safeVars[] = $node->getNode('var')->getAttribute('name');
}
Expand All @@ -73,7 +73,7 @@ public function leaveNode(Node $node, Environment $env): ?Node
$this->blocks = [];
} elseif ($node instanceof FilterExpression) {
return $this->preEscapeFilterNode($node, $env);
} elseif ($node instanceof PrintNode && false !== $type = $this->needEscaping($env)) {
} elseif ($node instanceof PrintNode && false !== $type = $this->needEscaping()) {
$expression = $node->getNode('expr');
if ($expression instanceof ConditionalExpression && $this->shouldUnwrapConditional($expression, $env, $type)) {
return new DoNode($this->unwrapConditional($expression, $env, $type), $expression->getTemplateLine());
Expand All @@ -85,7 +85,7 @@ public function leaveNode(Node $node, Environment $env): ?Node
if ($node instanceof AutoEscapeNode || $node instanceof BlockNode) {
array_pop($this->statusStack);
} elseif ($node instanceof BlockReferenceNode) {
$this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
$this->blocks[$node->getAttribute('name')] = $this->needEscaping();
}

return $node;
Expand Down Expand Up @@ -183,12 +183,11 @@ private function isSafeFor(string $type, Node $expression, Environment $env): bo
return \in_array($type, $safe) || \in_array('all', $safe);
}

private function needEscaping(Environment $env)
private function needEscaping()
{
if (\count($this->statusStack)) {
return $this->statusStack[\count($this->statusStack) - 1];
}

return $this->defaultStrategy ? $this->defaultStrategy : false;
}

Expand Down
16 changes: 8 additions & 8 deletions src/NodeVisitor/OptimizerNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(int $optimizers = -1)
public function enterNode(Node $node, Environment $env): Node
{
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
$this->enterOptimizeFor($node, $env);
$this->enterOptimizeFor($node);
}

return $node;
Expand All @@ -72,14 +72,14 @@ public function enterNode(Node $node, Environment $env): Node
public function leaveNode(Node $node, Environment $env): ?Node
{
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
$this->leaveOptimizeFor($node, $env);
$this->leaveOptimizeFor($node);
}

if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
$node = $this->optimizeRawFilter($node, $env);
$node = $this->optimizeRawFilter($node);
}

$node = $this->optimizePrintNode($node, $env);
$node = $this->optimizePrintNode($node);

return $node;
}
Expand All @@ -91,7 +91,7 @@ public function leaveNode(Node $node, Environment $env): ?Node
*
* * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
*/
private function optimizePrintNode(Node $node, Environment $env): Node
private function optimizePrintNode(Node $node): Node
{
if (!$node instanceof PrintNode) {
return $node;
Expand All @@ -113,7 +113,7 @@ private function optimizePrintNode(Node $node, Environment $env): Node
/**
* Removes "raw" filters.
*/
private function optimizeRawFilter(Node $node, Environment $env): Node
private function optimizeRawFilter(Node $node): Node
{
if ($node instanceof FilterExpression && 'raw' == $node->getNode('filter')->getAttribute('value')) {
return $node->getNode('node');
Expand All @@ -125,7 +125,7 @@ private function optimizeRawFilter(Node $node, Environment $env): Node
/**
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
*/
private function enterOptimizeFor(Node $node, Environment $env): void
private function enterOptimizeFor(Node $node): void
{
if ($node instanceof ForNode) {
// disable the loop variable by default
Expand Down Expand Up @@ -189,7 +189,7 @@ private function enterOptimizeFor(Node $node, Environment $env): void
/**
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
*/
private function leaveOptimizeFor(Node $node, Environment $env): void
private function leaveOptimizeFor(Node $node): void
{
if ($node instanceof ForNode) {
array_shift($this->loops);
Expand Down

0 comments on commit 6d715e2

Please sign in to comment.