Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SafeAnalysisNodeVisitor.php

function SafeAnalysisNodeVisitor::leaveNode

Overrides NodeVisitorInterface::leaveNode

File

vendor/twig/twig/src/NodeVisitor/SafeAnalysisNodeVisitor.php, line 88

Class

SafeAnalysisNodeVisitor
@internal

Namespace

Twig\NodeVisitor

Code

public function leaveNode(Node $node, Environment $env) : ?Node {
    if ($node instanceof ConstantExpression) {
        // constants are marked safe for all
        $this->setSafe($node, [
            'all',
        ]);
    }
    elseif ($node instanceof BlockReferenceExpression) {
        // blocks are safe by definition
        $this->setSafe($node, [
            'all',
        ]);
    }
    elseif ($node instanceof ParentExpression) {
        // parent block is safe by definition
        $this->setSafe($node, [
            'all',
        ]);
    }
    elseif ($node instanceof ConditionalExpression) {
        // intersect safeness of both operands
        $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
        $this->setSafe($node, $safe);
    }
    elseif ($node instanceof FilterExpression) {
        // filter expression is safe when the filter is safe
        if ($node->hasAttribute('twig_callable')) {
            $filter = $node->getAttribute('twig_callable');
        }
        else {
            // legacy
            $filter = $env->getFilter($node->getAttribute('name'));
        }
        if ($filter) {
            $safe = $filter->getSafe($node->getNode('arguments'));
            if (null === $safe) {
                trigger_deprecation('twig/twig', '3.16', 'The "%s::getSafe()" method should not return "null" anymore, return "[]" instead.', $filter::class);
                $safe = [];
            }
            if (!$safe) {
                $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
            }
            $this->setSafe($node, $safe);
        }
    }
    elseif ($node instanceof FunctionExpression) {
        // function expression is safe when the function is safe
        if ($node->hasAttribute('twig_callable')) {
            $function = $node->getAttribute('twig_callable');
        }
        else {
            // legacy
            $function = $env->getFunction($node->getAttribute('name'));
        }
        if ($function) {
            $safe = $function->getSafe($node->getNode('arguments'));
            if (null === $safe) {
                trigger_deprecation('twig/twig', '3.16', 'The "%s::getSafe()" method should not return "null" anymore, return "[]" instead.', $function::class);
                $safe = [];
            }
            $this->setSafe($node, $safe);
        }
    }
    elseif ($node instanceof MethodCallExpression || $node instanceof MacroReferenceExpression) {
        // all macro calls are safe
        $this->setSafe($node, [
            'all',
        ]);
    }
    elseif ($node instanceof GetAttrExpression && $node->getNode('node') instanceof NameExpression) {
        $name = $node->getNode('node')
            ->getAttribute('name');
        if (\in_array($name, $this->safeVars)) {
            $this->setSafe($node, [
                'all',
            ]);
        }
    }
    return $node;
}
RSS feed
Powered by Drupal