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

Breadcrumb

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

class Comment

Same name in this branch
  1. 11.1.x vendor/egulias/email-validator/src/Parser/Comment.php \Egulias\EmailValidator\Parser\Comment
  2. 11.1.x vendor/egulias/email-validator/src/Warning/Comment.php \Egulias\EmailValidator\Warning\Comment
  3. 11.1.x vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/Comment.php \SlevomatCodingStandard\Helpers\Comment
  4. 11.1.x vendor/squizlabs/php_codesniffer/src/Tokenizers/Comment.php \PHP_CodeSniffer\Tokenizers\Comment
  5. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/Node/Comment.php \Peast\Syntax\Node\Comment
  6. 11.1.x core/modules/comment/src/Plugin/migrate/source/d6/Comment.php \Drupal\comment\Plugin\migrate\source\d6\Comment
  7. 11.1.x core/modules/comment/src/Plugin/migrate/source/d7/Comment.php \Drupal\comment\Plugin\migrate\source\d7\Comment
  8. 11.1.x core/modules/comment/src/Plugin/views/wizard/Comment.php \Drupal\comment\Plugin\views\wizard\Comment
  9. 11.1.x core/modules/comment/src/Entity/Comment.php \Drupal\comment\Entity\Comment

Hierarchy

  • class \PhpParser\Comment implements \PhpParser\JsonSerializable

Expanded class hierarchy of Comment

2 files declare their use of Comment
CommentAnnotatingVisitor.php in vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php
LineCountingVisitor.php in vendor/sebastian/lines-of-code/src/LineCountingVisitor.php
115 string references to 'Comment'
AuthorNameFormatter::isApplicable in core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php
Returns if the formatter can be used for the provided field.
comment.info.yml in core/modules/comment/comment.info.yml
core/modules/comment/comment.info.yml
comment.migrate_drupal.yml in core/modules/comment/migrations/state/comment.migrate_drupal.yml
core/modules/comment/migrations/state/comment.migrate_drupal.yml
comment.routing.yml in core/modules/comment/comment.routing.yml
core/modules/comment/comment.routing.yml
comment.type.comment.yml in core/profiles/standard/config/install/comment.type.comment.yml
core/profiles/standard/config/install/comment.type.comment.yml

... See full list

File

vendor/nikic/php-parser/lib/PhpParser/Comment.php, line 5

Namespace

PhpParser
View source
class Comment implements \JsonSerializable {
    protected string $text;
    protected int $startLine;
    protected int $startFilePos;
    protected int $startTokenPos;
    protected int $endLine;
    protected int $endFilePos;
    protected int $endTokenPos;
    
    /**
     * Constructs a comment node.
     *
     * @param string $text Comment text (including comment delimiters like /*)
     * @param int $startLine Line number the comment started on
     * @param int $startFilePos File offset the comment started on
     * @param int $startTokenPos Token offset the comment started on
     */
    public function __construct(string $text, int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1, int $endLine = -1, int $endFilePos = -1, int $endTokenPos = -1) {
        $this->text = $text;
        $this->startLine = $startLine;
        $this->startFilePos = $startFilePos;
        $this->startTokenPos = $startTokenPos;
        $this->endLine = $endLine;
        $this->endFilePos = $endFilePos;
        $this->endTokenPos = $endTokenPos;
    }
    
    /**
     * Gets the comment text.
     *
     * @return string The comment text (including comment delimiters like /*)
     */
    public function getText() : string {
        return $this->text;
    }
    
    /**
     * Gets the line number the comment started on.
     *
     * @return int Line number (or -1 if not available)
     * @phpstan-return -1|positive-int
     */
    public function getStartLine() : int {
        return $this->startLine;
    }
    
    /**
     * Gets the file offset the comment started on.
     *
     * @return int File offset (or -1 if not available)
     */
    public function getStartFilePos() : int {
        return $this->startFilePos;
    }
    
    /**
     * Gets the token offset the comment started on.
     *
     * @return int Token offset (or -1 if not available)
     */
    public function getStartTokenPos() : int {
        return $this->startTokenPos;
    }
    
    /**
     * Gets the line number the comment ends on.
     *
     * @return int Line number (or -1 if not available)
     * @phpstan-return -1|positive-int
     */
    public function getEndLine() : int {
        return $this->endLine;
    }
    
    /**
     * Gets the file offset the comment ends on.
     *
     * @return int File offset (or -1 if not available)
     */
    public function getEndFilePos() : int {
        return $this->endFilePos;
    }
    
    /**
     * Gets the token offset the comment ends on.
     *
     * @return int Token offset (or -1 if not available)
     */
    public function getEndTokenPos() : int {
        return $this->endTokenPos;
    }
    
    /**
     * Gets the comment text.
     *
     * @return string The comment text (including comment delimiters like /*)
     */
    public function __toString() : string {
        return $this->text;
    }
    
    /**
     * Gets the reformatted comment text.
     *
     * "Reformatted" here means that we try to clean up the whitespace at the
     * starts of the lines. This is necessary because we receive the comments
     * without leading whitespace on the first line, but with leading whitespace
     * on all subsequent lines.
     *
     * Additionally, this normalizes CRLF newlines to LF newlines.
     */
    public function getReformattedText() : string {
        $text = str_replace("\r\n", "\n", $this->text);
        $newlinePos = strpos($text, "\n");
        if (false === $newlinePos) {
            // Single line comments don't need further processing
            return $text;
        }
        if (preg_match('(^.*(?:\\n\\s+\\*.*)+$)', $text)) {
            // Multi line comment of the type
            //
            //     /*
            //      * Some text.
            //      * Some more text.
            //      */
            //
            // is handled by replacing the whitespace sequences before the * by a single space
            return preg_replace('(^\\s+\\*)m', ' *', $text);
        }
        if (preg_match('(^/\\*\\*?\\s*\\n)', $text) && preg_match('(\\n(\\s*)\\*/$)', $text, $matches)) {
            // Multi line comment of the type
            //
            //    /*
            //        Some text.
            //        Some more text.
            //    */
            //
            // is handled by removing the whitespace sequence on the line before the closing
            // */ on all lines. So if the last line is "    */", then "    " is removed at the
            // start of all lines.
            return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
        }
        if (preg_match('(^/\\*\\*?\\s*(?!\\s))', $text, $matches)) {
            // Multi line comment of the type
            //
            //     /* Some text.
            //        Some more text.
            //          Indented text.
            //        Even more text. */
            //
            // is handled by removing the difference between the shortest whitespace prefix on all
            // lines and the length of the "/* " opening sequence.
            $prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1));
            $removeLen = $prefixLen - strlen($matches[0]);
            return preg_replace('(^\\s{' . $removeLen . '})m', '', $text);
        }
        // No idea how to format this comment, so simply return as is
        return $text;
    }
    
    /**
     * Get length of shortest whitespace prefix (at the start of a line).
     *
     * If there is a line with no prefix whitespace, 0 is a valid return value.
     *
     * @param string $str String to check
     * @return int Length in characters. Tabs count as single characters.
     */
    private function getShortestWhitespacePrefixLen(string $str) : int {
        $lines = explode("\n", $str);
        $shortestPrefixLen = \PHP_INT_MAX;
        foreach ($lines as $line) {
            preg_match('(^\\s*)', $line, $matches);
            $prefixLen = strlen($matches[0]);
            if ($prefixLen < $shortestPrefixLen) {
                $shortestPrefixLen = $prefixLen;
            }
        }
        return $shortestPrefixLen;
    }
    
    /**
     * @return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
     */
    public function jsonSerialize() : array {
        // Technically not a node, but we make it look like one anyway
        $type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
        return [
            'nodeType' => $type,
            'text' => $this->text,
            // TODO: Rename these to include "start".
'line' => $this->startLine,
            'filePos' => $this->startFilePos,
            'tokenPos' => $this->startTokenPos,
            'endLine' => $this->endLine,
            'endFilePos' => $this->endFilePos,
            'endTokenPos' => $this->endTokenPos,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary
Comment::$endFilePos protected property
Comment::$endLine protected property
Comment::$endTokenPos protected property
Comment::$startFilePos protected property
Comment::$startLine protected property
Comment::$startTokenPos protected property
Comment::$text protected property
Comment::getEndFilePos public function Gets the file offset the comment ends on.
Comment::getEndLine public function Gets the line number the comment ends on.
Comment::getEndTokenPos public function Gets the token offset the comment ends on.
Comment::getReformattedText public function Gets the reformatted comment text.
Comment::getShortestWhitespacePrefixLen private function Get length of shortest whitespace prefix (at the start of a line).
Comment::getStartFilePos public function Gets the file offset the comment started on.
Comment::getStartLine public function Gets the line number the comment started on.
Comment::getStartTokenPos public function Gets the token offset the comment started on.
Comment::getText public function Gets the comment text.
Comment::jsonSerialize public function
Comment::__construct public function Constructs a comment node.
Comment::__toString public function Gets the comment text.

API Navigation

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