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

Breadcrumb

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

function FunctionCommentSniff::processThrows

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php \Drupal\Sniffs\Commenting\FunctionCommentSniff::processThrows()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php \PHP_CodeSniffer\Standards\PEAR\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

Overrides FunctionCommentSniff::processThrows

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php, line 205

Class

FunctionCommentSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting

Code

protected function processThrows(File $phpcsFile, $stackPtr, $commentStart) {
    $tokens = $phpcsFile->getTokens();
    if ($this->skipIfInheritdoc === true) {
        if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
            return;
        }
    }
    foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
        if ($tokens[$tag]['content'] !== '@throws') {
            continue;
        }
        $exception = null;
        $comment = null;
        if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
            $matches = [];
            preg_match('/([^\\s]+)(?:\\s+(.*))?/', $tokens[$tag + 2]['content'], $matches);
            $exception = $matches[1];
            if (isset($matches[2]) === true && trim($matches[2]) !== '') {
                $comment = $matches[2];
            }
        }
        if ($exception === null) {
            $error = 'Exception type and comment missing for @throws tag in function comment';
            $phpcsFile->addError($error, $tag, 'InvalidThrows');
        }
        else {
            if ($comment === null) {
                $error = 'Comment missing for @throws tag in function comment';
                $phpcsFile->addError($error, $tag, 'EmptyThrows');
            }
            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'];
                }
                for ($i = $tag + 3; $i < $end; $i++) {
                    if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
                        $comment .= ' ' . $tokens[$i]['content'];
                    }
                }
                $comment = trim($comment);
                // 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, $tag + 2, 'ThrowsNotCapital');
                }
                $lastChar = substr($comment, -1);
                if ($lastChar !== '.') {
                    $error = '@throws tag comment must end with a full stop';
                    $phpcsFile->addError($error, $tag + 2, 'ThrowsNoFullStop');
                }
            }
        }
        
        //end if
    }
    
    //end foreach
}
RSS feed
Powered by Drupal