@param array $nodes

@return array
505033
private function addImplicitReturn(array $nodes)
    {
        // If nodes is empty, it can't have a return value.
        if (empty($nodes)) {
            return [new Return_(NoReturnValue::create())];
        }

        $last = \end($nodes);

        // Special case a few types of statements to add an implicit return
        // value (even though they technically don't have any return value)
        // because showing a return value in these instances is useful and not
        // very surprising.
        if ($last instanceof If_) {
            $last->stmts = $this->addImplicitReturn($last->stmts);

            foreach ($last->elseifs as $elseif) {
                $elseif->stmts = $this->addImplicitReturn($elseif->stmts);
            }

            if ($last->else) {
                $last->else->stmts = $this->addImplicitReturn($last->else->stmts);
            }
        } elseif ($last instanceof Switch_) {
            foreach ($last->cases as $case) {
                // only add an implicit return to cases which end in break
                $caseLast = \end($case->stmts);
                if ($caseLast instanceof Break_) {
                    $case->stmts = $this->addImplicitReturn(\array_slice($case->stmts, 0, -1));
                    $case->stmts[] = $caseLast;
                }
            }
        } elseif ($last instanceof Expr && !($last instanceof Exit_)) {
            // @codeCoverageIgnoreStart
            $nodes[\count($nodes) - 1] = new Return_($last, [
                'startLine' => $last->getLine(),
                'endLine'   => $last->getLine(),
            ]);
        // @codeCoverageIgnoreEnd
        } elseif ($last instanceof Expression && !($last->expr instanceof Exit_)) {
            // For PHP Parser 4.x
            $nodes[\count($nodes) - 1] = new Return_($last->expr, [
                'startLine' => $last->getLine(),
                'endLine'   => $last->getLine(),
            ]);
        } elseif ($last instanceof Namespace_) {
            $last->stmts = $this->addImplicitReturn($last->stmts);
        }

        // Return a "no return value" for all non-expression statements, so that
        // PsySH can suppress the `null` that `eval()` returns otherwise.
        //
        // Note that statements special cased above (if/elseif/else, switch)
        // _might_ implicitly return a value before this catch-all return is
        // reached.
        //
        // We're not adding a fallback return after namespace statements,
        // because code outside namespace statements doesn't really work, and
        // there's already an implicit return in the namespace statement anyway.
        if (self::isNonExpressionStmt($last)) {
            $nodes[] = new Return_(NoReturnValue::create());
        }

        return $nodes;
    }
1079791
private function parseElementNodes($children, $parentName = null)
    {
        $result = [];

        foreach ($children as $child) {
            $prefix = $child->prefix ?: null;
            $oname  = $this->normalizeKey($child->nodeName);
            $name   = $this->prefixKey($oname, $prefix);

            if (isset($result[$name])) {
                $this->parseSetResultNodes($child, $name, $result);
                continue;
            }

            $this->parseUnsetResultNodes($child, $name, $oname, $parentName, $result, $prefix);
        }

        return $result;
    }
488063
public function getGroups($node) {

    $groupcontents = GroupContent::loadByEntity($node);

    $groups = [];
    // Only react if it is actually posted inside a group.
    if (!empty($groupcontents)) {
      foreach ($groupcontents as $groupcontent) {
        /* @var \Drupal\group\Entity\GroupContent $groupcontent */
        $group = $groupcontent->getGroup();
        /* @var \Drupal\group\Entity\Group $group */
        $groups[] = $group;
      }
    }

    return $groups;
  }
1334303
public function mapTree(array $nodes, array $mappedNodes = []) {
        if (!$mappedNodes) {
            return $nodes;
        }

        $tree = [];
        $pk = $this->getRepository()->getPrimaryKey();

        foreach ($nodes as $node) {
            $id = $node[$pk];

            if (isset($mappedNodes[$id])) {
                $node[$this->getConfig('treeField') ?: 'Nodes'] = $this->mapTree($mappedNodes[$id], $mappedNodes);
            }

            $tree[] = $node;
        }

        return $tree;
    }
369236
protected function _processMethods($nodes)
    {
        foreach ($nodes as $child) {
            if (!$child->processable) {
                continue;
            }
            if ($child->type !== 'function' || !$child->isMethod || isset($child->visibility['abstract'])) {
                continue;
            }
            $namespace = $child->namespace ? $child->namespace->name . '\\' : '';
            $class = $namespace . $child->parent->name;

            if (!$this->processable($class, $child->name)) {
                continue;
            }
            $params = $child->argsToParams();
            $args = $params ? "[{$params}] + " : '';
            $before = new NodeDef("return \\Lead\Filter\\Filters::run(isset(\$this) ? \$this : get_called_class(), __FUNCTION__, " . $args . "func_get_args(), ", 'code');
            $before->parent = $child;
            $before->processable = false;
            $before->namespace = $child->namespace;

            $comma = count($child->args) ? ', ' : '';

            $closure = new FunctionDef("function (\$next{$comma}" . $params . ") {");
            $closure->parent = $child;
            $closure->namespace = $child->namespace;
            $closure->isClosure = true;
            $closure->tree = $child->tree;
            foreach($closure->tree as $item) {
                $item->parent = $closure;
            }
            $closure->close = '});';

            $child->tree = [];
            $child->tree[] = $before;
            $child->tree[] = $closure;
        }
    }
1123196
protected function getBlocks($nodes) {
    // Each completed block.
    $blocks = array();
    // Container for any top-level inline elements.
    $block = '';
    // Add a completed block to $blocks.
    $add_block = function($string = NULL) use(&$blocks, &$block) {
      if (preg_match('/\S/u', $block)) {
        $blocks[] = trim($block, " \t");
      }
      if (preg_match('/\S/u', $string)) {
        $blocks[] = rtrim($string, " \t");
      }
      $block = '';
    };
    /** @var \DOMNode $node */
    foreach ($nodes as $node) {
      switch ($node->nodeType) {

        case XML_ELEMENT_NODE:
          /** @var \DOMElement $node */
          switch ($node->nodeName) {

            // Explicitly handle these elements.

            case 'p':
            case 'pre':
              $add_block($this->getBlock($node));
              break;

            case 'h1':
              $add_block('# ' . $this->getBlock($node));
              break;

            case 'h2':
              $add_block('## ' . $this->getBlock($node));
              break;

            case 'h3':
              $add_block('### ' . $this->getBlock($node));
              break;

            case 'h4':
              $add_block('#### ' . $this->getBlock($node));
              break;

            case 'h5':
              $add_block('##### ' . $this->getBlock($node));
              break;

            case 'h6':
              $add_block('###### ' . $this->getBlock($node));
              break;

            case 'hr':
              $add_block('***');
              break;

            case 'ul':
            case 'ol':
            case 'dl':
              $add_block($this->getBlockList($node));
              break;

            // Other block-level elements.
            case 'article':
            case 'aside':
            case 'blockquote':
            case 'dd':
            case 'div':
            case 'fieldset':
            case 'figcaption':
            case 'figure':
            case 'footer':
            case 'form':
            case 'header':
            case 'hgroup':
            case 'main':
            case 'nav':
            case 'output':
            case 'section':
              // Recursively handle any descendant elements we care about.
              foreach ($this->getBlocks($node->childNodes) as $b) {
                $add_block($b);
              }
              break;

            // Unsupported.
            case 'canvas':
            case 'noscript':
            case 'script':
            case 'table':
            case 'tfoot':
            case 'video':
              break;

            // Treat as inline element.
            default:
              // Append series of inline elements together, as if they were
              // inside a block-level element.
              $block .= $this->getBlock($node);
              break;

          }
          break;

        case XML_TEXT_NODE:
          /** @var \DOMText $node */
          $block .= self::escape($node->textContent);
          break;

      }
    }

    $add_block();
    return $blocks;
  }
659168
protected function toNewXmlStructure($nodes)
    {
        $tab = "\t";
        $br = PHP_EOL;
        $xml = '<?xml version="1.0" encoding="UTF-8"?><document type="MelisUserRights" author="MelisTechnology" version="2.0">' . $br;
        unset($nodes['@attributes']);

        $newToolParentNode = 'meliscore_leftmenu';

        foreach ($nodes as $keyNode => $node) {

            if ($newToolParentNode !== $keyNode) {
                $xml .= "<$keyNode>$br";
                if (isset($node['id'])) {
                    if (is_array($node['id'])) {
                        foreach ($node['id'] as $item) {
                            $xml .= "$tab<id>$item</id>$br";
                        }
                    } else {
                        $xml .= "$tab<id>$node[id]</id>$br";
                    }

                }
                $xml .= "</$keyNode>$br";
            } else {
                $xml .= "<$newToolParentNode>$br";
                foreach ($node as $parentKeyNode => $tools) {

                    if (is_array($tools)) {
                        $xml .= "$tab<$parentKeyNode>$br";
                        if (isset($tools['id'])) {
                            $tools = array_map(function ($a) use ($tab, $br) {
                                return "$tab$tab<id>$a</id>";
                            }, $tools['id']);

                            $toolItems = implode($br, $tools) . $br;
                            $xml .= "$toolItems";
                        }

                        $xml .= "$tab</$parentKeyNode>$br";
                    } else {
                        $xml .= "$tab<$parentKeyNode>";
                        $xml .= $tools;
                        $xml .= "</$parentKeyNode>$br";
                    }

                }
                $xml .= "</$newToolParentNode>";

            }
        }
        $xml .= $br . '</document>';

        return $xml;
    }
691476
public function after($nodes) {
    foreach ($this->nodes as $i => $node) {
      $node->after($i === 0 ? $nodes : clone $nodes);
    }
    return $this;
  }
242157
protected function getQueryNodes(AbstractNode $node): array
    {
        $methodPostfix = $node->findChildrenOfType('MethodPostfix');
        $classOrInterfaceReference = $node->findChildrenOfType('ClassOrInterfaceReference');

        return array_merge($methodPostfix, $classOrInterfaceReference);
    }
484619
protected static function sortUnique($nodes) {
    $sort = [];
    $detached = [];
    foreach ($nodes as $node) {
      $key = $node->sortKey();
      if ($key[0] === '~') {
        $detached[] = $node;
      }
      else {
        $sort[$key] = $node;
      }
    }
    ksort($sort, SORT_NATURAL);
    return array_merge(array_values($sort), $detached);
  }
320438
public function buildOptionsArray($node, $nodesArray)
    {
        $item           = [];
        $item['ref']    = $node['ref'];
        $item['label']  = $node['label'];
        $item['link']   = $node['link'];
        $item['target'] = $node['target'];
        $item['pos']    = $node['pos'];
        $item['parent'] = $node['parent'];

        $nodesArray[]   = $item;
        if (array_key_exists('children', $node)) {
            foreach ($node['children'] as $child) {
                $nodesArray = $this->buildOptionsArray($child, $nodesArray);
            }
        }
        return $nodesArray;
    }
1119413
protected function parseValues(\SimpleXMLElement $nodes)
    {
        $values = [];

        foreach ($nodes as $node) {
            if (\count($node) > 0) {
                if (\count($node->value) > 0) {
                    $value = $this->parseValues($node->value);
                } elseif (\count($node->constraint) > 0) {
                    $value = $this->parseConstraints($node->constraint);
                } else {
                    $value = [];
                }
            } else {
                $value = trim($node);
            }

            if (isset($node['key'])) {
                $values[(string) $node['key']] = $value;
            } else {
                $values[] = $value;
            }
        }

        return $values;
    }
1041254
public function setNodes(ArrayCollection $nodes)
    {
        $finalNodes = array();

        foreach ($nodes as $node) {
            if ($this->checkChildNode($node)) {
                $node->setParent($this);
                $finalNodes[] = $node;
            }
        }

        $this->nodes = $finalNodes;

        return $this;
    }
1017940
protected function nodeToDictionary( Node $node )
    {
        $result = $node->attributes;

        $containerKeys = [];

        foreach ($node->children as $child) {
            $arrayChild = $this->nodeToDictionary($child);

            $childName = $child->name;
            $childValue = $arrayChild;

            if (sizeof($child->children) == 0) {
                $childValue = $child->text;
            }

            if (isset( $result[ $childName ] )) {
                if (!in_array($childName, $containerKeys)) {
                    $result[ $childName ] = [$result[ $childName ]];
                    $containerKeys[] = $childName;
                }

                $result[ $childName ][] = $childValue;
            } else {
                $result[ $childName ] = $childValue;
            }
        }

        return $result;
    }
729993
private function collectNodes(NodeInterface $node, array $visited_nodes): array
    {
        if (in_array($node, $visited_nodes, true)) {
            return $visited_nodes;
        }

        $visited_nodes[] = $node;
        foreach ($node->getChildren() as $child) {
            $visited_nodes = $this->collectNodes($child, $visited_nodes);
        }

        return $visited_nodes;
    }
970958
public static function getRootPageIdsFromSites(array $sites): array
    {
        $rootPageIds = [];
        foreach ($sites as $site) {
            $rootPageIds[] = (int)$site->getRootPageId();
        }

        return $rootPageIds;
    }
1143480
public function discardNodes(array $nodes)
    {
        $discardedNodeIdentifiers = [];
        foreach ($nodes as $node) {
            $this->doDiscardNode($node, $discardedNodeIdentifiers);
        }
    }