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

Breadcrumb

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

class FunctionCommentSniff

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php \Drupal\Sniffs\Commenting\FunctionCommentSniff
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff
  3. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff extends \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff
      • class \PHP_CodeSniffer\Standards\MySource\Sniffs\Commenting\FunctionCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff

Expanded class hierarchy of FunctionCommentSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Commenting/FunctionCommentSniff.php, line 20

Namespace

PHP_CodeSniffer\Standards\MySource\Sniffs\Commenting
View source
class FunctionCommentSniff extends SquizFunctionCommentSniff {
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token
     *                                               in the stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr) {
        parent::process($phpcsFile, $stackPtr);
        $tokens = $phpcsFile->getTokens();
        $find = Tokens::$methodPrefixes;
        $find[] = T_WHITESPACE;
        $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
        if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
            return;
        }
        $commentStart = $tokens[$commentEnd]['comment_opener'];
        $hasApiTag = false;
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
            if ($tokens[$tag]['content'] === '@api') {
                if ($hasApiTag === true) {
                    // We've come across an API tag already, which means
                    // we were not the first tag in the API list.
                    $error = 'The @api tag must come first in the @api tag list in a function comment';
                    $phpcsFile->addError($error, $tag, 'ApiNotFirst');
                }
                $hasApiTag = true;
                // There needs to be a blank line before the @api tag.
                $prev = $phpcsFile->findPrevious([
                    T_DOC_COMMENT_STRING,
                    T_DOC_COMMENT_TAG,
                ], $tag - 1);
                if ($tokens[$prev]['line'] !== $tokens[$tag]['line'] - 2) {
                    $error = 'There must be one blank line before the @api tag in a function comment';
                    $phpcsFile->addError($error, $tag, 'ApiSpacing');
                }
            }
            else {
                if (substr($tokens[$tag]['content'], 0, 5) === '@api-') {
                    $hasApiTag = true;
                    $prev = $phpcsFile->findPrevious([
                        T_DOC_COMMENT_STRING,
                        T_DOC_COMMENT_TAG,
                    ], $tag - 1);
                    if ($tokens[$prev]['line'] !== $tokens[$tag]['line'] - 1) {
                        $error = 'There must be no blank line before the @%s tag in a function comment';
                        $data = [
                            $tokens[$tag]['content'],
                        ];
                        $phpcsFile->addError($error, $tag, 'ApiTagSpacing', $data);
                    }
                }
            }
            
            //end if
        }
        
        //end foreach
        if ($hasApiTag === true && substr($tokens[$tag]['content'], 0, 4) !== '@api') {
            // API tags must be the last tags in a function comment.
            $error = 'The @api tags must be the last tags in a function comment';
            $phpcsFile->addError($error, $commentEnd, 'ApiNotLast');
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FunctionCommentSniff::$minimumVisibility public property Disable the check for functions with a lower visibility than the value given.
FunctionCommentSniff::$phpVersion private property The current PHP version.
FunctionCommentSniff::$skipIfInheritdoc public property Whether to skip inheritdoc comments.
FunctionCommentSniff::$specialMethods public property Array of methods which do not require a return type.
FunctionCommentSniff::checkInheritdoc protected function Determines whether the whole comment is an inheritdoc comment.
FunctionCommentSniff::checkSpacingAfterParamName protected function Check the spacing after the name of a parameter.
FunctionCommentSniff::checkSpacingAfterParamType protected function Check the spacing after the type of a parameter.
FunctionCommentSniff::process public function Processes this test, when one of its tokens is encountered. Overrides FunctionCommentSniff::process
FunctionCommentSniff::processParams protected function Process the function parameter comments. Overrides FunctionCommentSniff::processParams
FunctionCommentSniff::processReturn protected function Process the return comment of this function comment. Overrides FunctionCommentSniff::processReturn
FunctionCommentSniff::processThrows protected function Process any throw tags that this function comment has. Overrides FunctionCommentSniff::processThrows
FunctionCommentSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal