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

Breadcrumb

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

function DocCommentStarSniff::process

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

Overrides Sniff::process

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/DocCommentStarSniff.php, line 47

Class

DocCommentStarSniff
Checks that a doc comment block has a doc comment star on every line.

Namespace

Drupal\Sniffs\Commenting

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $lastLineChecked = $tokens[$stackPtr]['line'];
    for ($i = $stackPtr + 1; $i < $tokens[$stackPtr]['comment_closer'] - 1; $i++) {
        // We are only interested in the beginning of the line.
        if ($tokens[$i]['line'] === $lastLineChecked) {
            continue;
        }
        // The first token on the line must be a whitespace followed by a star.
        if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {
            if ($tokens[$i + 1]['code'] !== T_DOC_COMMENT_STAR) {
                $error = 'Doc comment star missing';
                $fix = $phpcsFile->addFixableError($error, $i, 'StarMissing');
                if ($fix === true) {
                    if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
                        $phpcsFile->fixer
                            ->replaceToken($i, str_repeat(' ', $tokens[$stackPtr]['column']) . "* \n");
                    }
                    else {
                        $phpcsFile->fixer
                            ->replaceToken($i, str_repeat(' ', $tokens[$stackPtr]['column']) . '* ');
                    }
                    // Ordering of lines might have changed - stop here. The
                    // fixer will restart the sniff if there are remaining fixes.
                    return;
                }
            }
        }
        else {
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
                $error = 'Doc comment star missing';
                $fix = $phpcsFile->addFixableError($error, $i, 'StarMissing');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->addContentBefore($i, str_repeat(' ', $tokens[$stackPtr]['column']) . '* ');
                }
            }
        }
        
        //end if
        $lastLineChecked = $tokens[$i]['line'];
    }
    
    //end for
}
RSS feed
Powered by Drupal