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

Breadcrumb

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

function NodeTraverser::traverseArray

Same name in this branch
  1. 11.1.x vendor/phpstan/phpdoc-parser/src/Ast/NodeTraverser.php \PHPStan\PhpDocParser\Ast\NodeTraverser::traverseArray()

Recursively traverse array (usually of nodes).

Parameters

array $nodes Array to traverse:

Return value

array Result of traversal (may be original array or changed one)

2 calls to NodeTraverser::traverseArray()
NodeTraverser::traverse in vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php
Traverses an array of nodes using the registered visitors.
NodeTraverser::traverseNode in vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php
Recursively traverse a node.

File

vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php, line 175

Class

NodeTraverser

Namespace

PhpParser

Code

protected function traverseArray(array $nodes) : array {
    $doNodes = [];
    foreach ($nodes as $i => $node) {
        if ($node instanceof Node) {
            $traverseChildren = true;
            $visitorIndex = -1;
            foreach ($this->visitors as $visitorIndex => $visitor) {
                $return = $visitor->enterNode($node);
                if (null !== $return) {
                    if ($return instanceof Node) {
                        $this->ensureReplacementReasonable($node, $return);
                        $nodes[$i] = $node = $return;
                    }
                    elseif (\is_array($return)) {
                        $doNodes[] = [
                            $i,
                            $return,
                        ];
                        continue 2;
                    }
                    elseif (NodeVisitor::REMOVE_NODE === $return) {
                        $doNodes[] = [
                            $i,
                            [],
                        ];
                        continue 2;
                    }
                    elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
                        $traverseChildren = false;
                    }
                    elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
                        $traverseChildren = false;
                        break;
                    }
                    elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
                        $this->stopTraversal = true;
                        break 2;
                    }
                    elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
                        throw new \LogicException('REPLACE_WITH_NULL can not be used if the parent structure is an array');
                    }
                    else {
                        throw new \LogicException('enterNode() returned invalid value of type ' . gettype($return));
                    }
                }
            }
            if ($traverseChildren) {
                $this->traverseNode($node);
                if ($this->stopTraversal) {
                    break;
                }
            }
            for (; $visitorIndex >= 0; --$visitorIndex) {
                $visitor = $this->visitors[$visitorIndex];
                $return = $visitor->leaveNode($node);
                if (null !== $return) {
                    if ($return instanceof Node) {
                        $this->ensureReplacementReasonable($node, $return);
                        $nodes[$i] = $node = $return;
                    }
                    elseif (\is_array($return)) {
                        $doNodes[] = [
                            $i,
                            $return,
                        ];
                        break;
                    }
                    elseif (NodeVisitor::REMOVE_NODE === $return) {
                        $doNodes[] = [
                            $i,
                            [],
                        ];
                        break;
                    }
                    elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
                        $this->stopTraversal = true;
                        break 2;
                    }
                    elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
                        throw new \LogicException('REPLACE_WITH_NULL can not be used if the parent structure is an array');
                    }
                    else {
                        throw new \LogicException('leaveNode() returned invalid value of type ' . gettype($return));
                    }
                }
            }
        }
        elseif (\is_array($node)) {
            throw new \LogicException('Invalid node structure: Contains nested arrays');
        }
    }
    if (!empty($doNodes)) {
        while (list($i, $replace) = array_pop($doNodes)) {
            array_splice($nodes, $i, 1, $replace);
        }
    }
    return $nodes;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal