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/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php \PHP_CodeSniffer\Standards\Squiz\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

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

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php, line 408

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';
                        // cspell:ignore TrhowsCommentIndentation
                        $phpcsFile->addError($error, $i, 'TrhowsCommentIndentation', [
                            $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 full stop.
            $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, [
                '.',
                '!',
                '?',
            ]) === false) {
                $error = '@throws tag comment must end with a full stop';
                $phpcsFile->addError($error, $throwStart, 'ThrowsNoFullStop');
            }
        }
        
        //end if
    }
    
    //end foreach
}
RSS feed
Powered by Drupal