minor #3888 Remove unused variables and unreachable code (GromNaN)

This PR was merged into the 3.x branch.

Discussion
----------

Remove unused variables and unreachable code

I did some archaeology to find the origin of each useless line of code. Details in comments.

Commits
-------

6d715e20 Remove unused variables and unreachable code
This commit is contained in:
Fabien Potencier
2023-10-20 17:39:04 +02:00
6 changed files with 13 additions and 21 deletions
-1
View File
@@ -345,7 +345,6 @@ class Environment
$this->cache->load($key);
}
$source = null;
if (!class_exists($cls, false)) {
$source = $this->getLoader()->getSourceContext($name);
$content = $this->compileSource($source);
-4
View File
@@ -506,10 +506,6 @@ class ExpressionParser
}
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);
+1 -2
View File
@@ -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);
@@ -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());
}
/**
-1
View File
@@ -27,7 +27,6 @@ class Node implements \Countable, \IteratorAggregate
protected $lineno;
protected $tag;
private $name;
private $sourceContext;
/**
+4 -5
View File
@@ -57,7 +57,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
} 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');
}
@@ -73,7 +73,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
$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());
@@ -85,7 +85,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
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;
@@ -183,12 +183,11 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
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;
}
+8 -8
View File
@@ -63,7 +63,7 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
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;
@@ -72,14 +72,14 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
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;
}
@@ -91,7 +91,7 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
*
* * "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;
@@ -113,7 +113,7 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
/**
* 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');
@@ -125,7 +125,7 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
/**
* 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
@@ -189,7 +189,7 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
/**
* 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);