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

Breadcrumb

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

class ClassCommentSniff

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

Hierarchy

  • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\ClassCommentSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ClassCommentSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php, line 23

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting
View source
class ClassCommentSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_CLASS,
        ];
    }
    
    //end register()
    
    /**
     * 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) {
        $tokens = $phpcsFile->getTokens();
        $find = [
            T_ABSTRACT => T_ABSTRACT,
            T_FINAL => T_FINAL,
            T_READONLY => T_READONLY,
            T_WHITESPACE => T_WHITESPACE,
        ];
        $previousContent = null;
        for ($commentEnd = $stackPtr - 1; $commentEnd >= 0; $commentEnd--) {
            if (isset($find[$tokens[$commentEnd]['code']]) === true) {
                continue;
            }
            if ($previousContent === null) {
                $previousContent = $commentEnd;
            }
            if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END && isset($tokens[$commentEnd]['attribute_opener']) === true) {
                $commentEnd = $tokens[$commentEnd]['attribute_opener'];
                continue;
            }
            break;
        }
        if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) {
            $class = $phpcsFile->getDeclarationName($stackPtr);
            $phpcsFile->addError('Missing doc comment for class %s', $stackPtr, 'Missing', [
                $class,
            ]);
            $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no');
            return;
        }
        $phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes');
        if ($tokens[$commentEnd]['code'] === T_COMMENT) {
            $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle');
            return;
        }
        if ($tokens[$previousContent]['line'] !== $tokens[$stackPtr]['line'] - 1) {
            $error = 'There must be no blank lines after the class comment';
            $phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
        }
        $commentStart = $tokens[$commentEnd]['comment_opener'];
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
            $error = '%s tag is not allowed in class comment';
            $data = [
                $tokens[$tag]['content'],
            ];
            $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ClassCommentSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
ClassCommentSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal