You are here

public function DocCommentStarSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Commenting/DocCommentStarSniff.php \Drupal\Sniffs\Commenting\DocCommentStarSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Commenting/DocCommentStarSniff.php \Drupal\Sniffs\Commenting\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

File

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
}