Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/NodeVisitor/VariableOptimizerNodeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig\NodeVisitor;

use Twig\Environment;
use Twig\Node\Expression\NameExpression;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\TypesNode;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
final class VariableOptimizerNodeVisitor implements NodeVisitorInterface
{
private array $types = [];

public function enterNode(Node $node, Environment $env): Node
{
if ($node instanceof TypesNode) {
$this->types = array_merge($this->types, $node->getAttribute('mapping'));
}

// A NameExpression is always defined if the variable is typed and not optional
if ($node instanceof NameExpression && isset($this->types[$node->getAttribute('name')]) && !$this->types[$node->getAttribute('name')]['optional']) {
$node->setAttribute('always_defined', true);
}

return $node;
}

public function leaveNode(Node $node, Environment $env): ?Node
{
if ($node instanceof ModuleNode) {
$this->types = [];
}

return $node;
}

public function getPriority(): int
{
return 255;
}
}
51 changes: 51 additions & 0 deletions tests/NodeVisitor/VariableOptimizerNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Twig\Tests\NodeVisitor;

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\NodeTraverser;
use Twig\NodeVisitor\VariableOptimizerNodeVisitor;
use Twig\Source;

class VariableOptimizerNodeVisitorTest extends TestCase
{
/**
* @dataProvider getTypedVariablesData
*/
public function testTypedVariables(string $template, string $expected)
{
$env = new Environment(new ArrayLoader(), ['autoescape' => false]);
$node = $env->parse($env->tokenize(new Source($template, 'index')));
$traverser = new NodeTraverser($env, [new VariableOptimizerNodeVisitor()]);
$node = $traverser->traverse($node);

$this->assertSame($expected, $this->stripCode($env->compile($node->getNode('body'))));
}

public function getTypedVariablesData()
{
yield 'regular' => ['{{ foo }}', 'yield ($context["foo"] ?? null);'];
yield 'typed_optional' => ['{% types { foo?: "string" } %}{{ foo }}', 'yield ($context["foo"] ?? null);'];
yield 'typed' => ['{% types { foo: "string" } %}{{ foo }}', 'yield $context["foo"];'];

yield 'is_defined_test' => ['{{ foo is defined }}', 'yield array_key_exists("foo", $context);'];
yield 'is_defined_test_typed_optional' => ['{% types { foo?: "string" } %}{{ foo is defined }}', 'yield array_key_exists("foo", $context);'];
yield 'is_defined_test_typed' => ['{% types { foo: "string" } %}{{ foo is defined }}', 'yield true;'];
}

private function stripCode(string $code): string
{
return trim(preg_replace("{^//.*\n}", '', $code));
}
}
Loading