class TwigNodeVisitorCheckDeprecations

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Template/TwigNodeVisitorCheckDeprecations.php \Drupal\Core\Template\TwigNodeVisitorCheckDeprecations

Provides a Node Visitor to trigger errors if deprecated variables are used.

Every use of a named variable is tracked, and the used variable names are passed to TwigExtension::checkDeprecations at runtime for comparison against those in the 'deprecated' array in the template context.

Hierarchy

Expanded class hierarchy of TwigNodeVisitorCheckDeprecations

See also

\Drupal\Core\Template\TwigNodeCheckDeprecations

File

core/lib/Drupal/Core/Template/TwigNodeVisitorCheckDeprecations.php, line 21

Namespace

Drupal\Core\Template
View source
class TwigNodeVisitorCheckDeprecations implements NodeVisitorInterface {
    
    /**
     * The named variables used in the template from the context.
     */
    protected array $usedNames = [];
    
    /**
     * The named variables set within the template.
     */
    protected array $assignedNames = [];
    
    /**
     * {@inheritdoc}
     */
    public function enterNode(Node $node, Environment $env) : Node {
        if ($node instanceof ModuleNode) {
            $this->usedNames = [];
            $this->assignedNames = [];
        }
        elseif ($node instanceof AssignNameExpression) {
            // Setting a variable makes subsequent usage is safe.
            $this->assignedNames[$node->getAttribute('name')] = $node->getAttribute('name');
        }
        elseif ($node instanceof NameExpression) {
            // Track each usage of a variable, unless set within the template.
            $name = $node->getAttribute('name');
            if (!in_array($name, $this->assignedNames)) {
                $this->usedNames[$name] = $name;
            }
        }
        return $node;
    }
    
    /**
     * {@inheritdoc}
     */
    public function leaveNode(Node $node, Environment $env) : ?Node {
        // At the end of the template, check the used variables are not deprecated.
        if ($node instanceof ModuleNode) {
            if (!empty($this->usedNames)) {
                $checkNode = new Node([
                    new TwigNodeCheckDeprecations($this->usedNames),
                    $node->getNode('display_end'),
                ]);
                $node->setNode('display_end', $checkNode);
            }
        }
        return $node;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPriority() : int {
        // Just above the Optimizer, which is the normal last one.
        return 256;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TwigNodeVisitorCheckDeprecations::$assignedNames protected property The named variables set within the template.
TwigNodeVisitorCheckDeprecations::$usedNames protected property The named variables used in the template from the context.
TwigNodeVisitorCheckDeprecations::enterNode public function
TwigNodeVisitorCheckDeprecations::getPriority public function
TwigNodeVisitorCheckDeprecations::leaveNode public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.