You are here

protected function FunctionCommentSniff::processThrows in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php \Drupal\Sniffs\Commenting\FunctionCommentSniff::processThrows()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php \Drupal\Sniffs\Commenting\FunctionCommentSniff::processThrows()

Process any throw tags that this function comment has.

Parameters

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

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

int $commentStart The position in the stack where the comment started.:

Return value

void

1 call to FunctionCommentSniff::processThrows()
FunctionCommentSniff::process in coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php
Processes this test, when one of its tokens is encountered.

File

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php, line 381

Class

FunctionCommentSniff
Parses and verifies the doc comments for functions. Largely copied from PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff.

Namespace

Drupal\Sniffs\Commenting

Code

protected function processThrows(File $phpcsFile, $stackPtr, $commentStart) {
  $tokens = $phpcsFile
    ->getTokens();
  foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
    if ($tokens[$tag]['content'] !== '@throws') {
      continue;
    }
    if ($tokens[$tag + 2]['code'] !== T_DOC_COMMENT_STRING) {
      $error = 'Exception type missing for @throws tag in function comment';
      $phpcsFile
        ->addError($error, $tag, 'InvalidThrows');
    }
    else {

      // Any strings until the next tag belong to this comment.
      if (isset($tokens[$commentStart]['comment_tags'][$pos + 1]) === true) {
        $end = $tokens[$commentStart]['comment_tags'][$pos + 1];
      }
      else {
        $end = $tokens[$commentStart]['comment_closer'];
      }
      $comment = '';
      $throwStart = null;
      for ($i = $tag + 3; $i < $end; $i++) {
        if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
          if ($throwStart === null) {
            $throwStart = $i;
          }
          $indent = 0;
          if ($tokens[$i - 1]['code'] === T_DOC_COMMENT_WHITESPACE) {
            $indent = strlen($tokens[$i - 1]['content']);
          }
          $comment .= ' ' . $tokens[$i]['content'];
          if ($indent < 3) {
            $error = 'Throws comment indentation must be 3 spaces, found %s spaces';
            $phpcsFile
              ->addError($error, $i, 'TrhowsCommentIndentation', array(
              $indent,
            ));
          }
        }
      }
      $comment = trim($comment);
      if ($comment === '') {
        if (str_word_count($tokens[$tag + 2]['content'], 0, '\\_') > 1) {
          $error = '@throws comment must be on the next line';
          $phpcsFile
            ->addError($error, $tag, 'ThrowsComment');
        }
        return;
      }

      // Starts with a capital letter and ends with a fullstop.
      $firstChar = $comment[0];
      if (strtoupper($firstChar) !== $firstChar) {
        $error = '@throws tag comment must start with a capital letter';
        $phpcsFile
          ->addError($error, $throwStart, 'ThrowsNotCapital');
      }
      $lastChar = substr($comment, -1);
      if (in_array($lastChar, array(
        '.',
        '!',
        '?',
      )) === false) {
        $error = '@throws tag comment must end with a full stop';
        $phpcsFile
          ->addError($error, $throwStart, 'ThrowsNoFullStop');
      }
    }

    //end if
  }

  //end foreach
}