class RemoveCheckToStringNodeVisitor

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

Defines a TwigNodeVisitor that replaces CheckToStringNodes.

Twig 3.14.1 resulted in a performance regression in Drupal due to checking if __toString is an allowed method on objects. __toString is allowed on all objects when Drupal's default SandboxPolicy is active. Therefore, Twig's SandboxExtension checks are unnecessary.

Hierarchy

Expanded class hierarchy of RemoveCheckToStringNodeVisitor

File

core/lib/Drupal/Core/Template/RemoveCheckToStringNodeVisitor.php, line 20

Namespace

Drupal\Core\Template
View source
final class RemoveCheckToStringNodeVisitor implements NodeVisitorInterface {
    
    /**
     * {@inheritdoc}
     */
    public function enterNode(Node $node, Environment $env) : Node {
        if ($node instanceof CheckToStringNode) {
            // Replace CheckToStringNode with the faster equivalent, __toString is an
            // allowed method so any checking of __toString on a per-object basis is
            // performance overhead.
            $new = new TwigSimpleCheckToStringNode($node->getNode('expr'));
            // @todo https://www.drupal.org/project/drupal/issues/3488584 Update for
            //   Twig 4 as the spread attribute has been removed there.
            if ($node->hasAttribute('spread')) {
                $new->setAttribute('spread', $node->getAttribute('spread'));
            }
            return $new;
        }
        return $node;
    }
    
    /**
     * {@inheritdoc}
     */
    public function leaveNode(Node $node, Environment $env) : ?Node {
        return $node;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPriority() {
        // Runs after sandbox visitor.
        return 1;
    }

}

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