-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathVariableOptimizerNodeVisitor.php
More file actions
56 lines (46 loc) · 1.33 KB
/
VariableOptimizerNodeVisitor.php
File metadata and controls
56 lines (46 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}
}