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

Breadcrumb

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

function CommentsRegistry::findNodeForCommentsGroup

Finds the node to attach the given comments group

Parameters

array $group Comments group:

Return value

void

1 call to CommentsRegistry::findNodeForCommentsGroup()
CommentsRegistry::onEndParsing in vendor/mck89/peast/lib/Peast/Syntax/CommentsRegistry.php
Listener called when parsing process ends

File

vendor/mck89/peast/lib/Peast/Syntax/CommentsRegistry.php, line 203

Class

CommentsRegistry
Comments registry class. Internal class used to manage comments

Namespace

Peast\Syntax

Code

public function findNodeForCommentsGroup($group) {
    $next = $group["next"];
    $prev = $group["prev"];
    $comments = $group["comments"];
    $leading = true;
    
    //If the group of comments has a next token index that appears
    
    //in the map of start node indices, add the group to the
    
    //corresponding node's leading comments. This associates
    
    //comments that appear immediately before a node.
    
    //For example: /*comment*/ for (;;){}
    if (isset($this->nodesStartMap[$next])) {
        $nodes = $this->nodesStartMap[$next];
    }
    elseif (isset($this->nodesEndMap[$prev])) {
        $nodes = $this->nodesEndMap[$prev];
        $leading = false;
    }
    else {
        
        //Calculate comments group boundaries
        $start = $comments[0]->location->start
            ->getIndex();
        $end = $comments[count($comments) - 1]->location->end
            ->getIndex();
        $nodes = array();
        
        //Loop all the entries in the start index map
        foreach ($this->nodesStartMap as $idx => $ns) {
            
            //If the index is higher than the start index of the comments
            
            //group, stop
            if ($idx > $start) {
                break;
            }
            foreach ($ns as $node) {
                
                //Check if the comments group is inside node indices range
                if ($node->location->end
                    ->getIndex() >= $end) {
                    $nodes[] = $node;
                }
            }
        }
        
        //If comments can't be associated with any node, associate it as
        
        //leading comments of the program, this happens when the source is
        
        //empty
        if (!$nodes) {
            $firstNode = array_values($this->nodesStartMap);
            $nodes = array(
                $firstNode[0][0],
            );
        }
    }
    
    //If there are multiple possible nodes to associate the comments to,
    
    //find the shortest one
    if (count($nodes) > 1) {
        usort($nodes, array(
            $this,
            "compareNodesLength",
        ));
    }
    $this->associateComments($nodes[0], $comments, $leading);
}

API Navigation

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