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/Squiz/Sniffs/Commenting/ClassCommentSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\ClassCommentSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff extends \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff

Expanded class hierarchy of ClassCommentSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php, line 14

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting
View source
class ClassCommentSniff extends FileCommentSniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_CLASS,
            T_INTERFACE,
            T_TRAIT,
            T_ENUM,
        ];
    }
    
    //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();
        $type = strtolower($tokens[$stackPtr]['content']);
        $errorData = [
            $type,
        ];
        $find = [
            T_ABSTRACT => T_ABSTRACT,
            T_FINAL => T_FINAL,
            T_READONLY => T_READONLY,
            T_WHITESPACE => T_WHITESPACE,
        ];
        for ($commentEnd = $stackPtr - 1; $commentEnd >= 0; $commentEnd--) {
            if (isset($find[$tokens[$commentEnd]['code']]) === true) {
                continue;
            }
            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) {
            $errorData[] = $phpcsFile->getDeclarationName($stackPtr);
            $phpcsFile->addError('Missing doc comment for %s %s', $stackPtr, 'Missing', $errorData);
            $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 %s comment', $stackPtr, 'WrongStyle', $errorData);
            return;
        }
        // Check each tag.
        $this->processTags($phpcsFile, $stackPtr, $tokens[$commentEnd]['comment_opener']);
    }
    
    //end process()
    
    /**
     * Process the version tag.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param array                       $tags      The tokens for these tags.
     *
     * @return void
     */
    protected function processVersion($phpcsFile, array $tags) {
        $tokens = $phpcsFile->getTokens();
        foreach ($tags as $tag) {
            if ($tokens[$tag + 2]['code'] !== T_DOC_COMMENT_STRING) {
                // No content.
                continue;
            }
            $content = $tokens[$tag + 2]['content'];
            if (strstr($content, 'Release:') === false) {
                $error = 'Invalid version "%s" in doc comment; consider "Release: <package_version>" instead';
                $data = [
                    $content,
                ];
                $phpcsFile->addWarning($error, $tag, 'InvalidVersion', $data);
            }
        }
    }
    
    //end processVersion()

}

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 FileCommentSniff::process
ClassCommentSniff::processVersion protected function Process the version tag. Overrides FileCommentSniff::processVersion
ClassCommentSniff::register public function Returns an array of tokens this test wants to listen for. Overrides FileCommentSniff::register
FileCommentSniff::$tags protected property Tags in correct order and related info.
FileCommentSniff::processAuthor protected function Process the author tag(s) that this header comment has.
FileCommentSniff::processCategory protected function Process the category tag.
FileCommentSniff::processCopyright protected function Process the copyright tags.
FileCommentSniff::processLicense protected function Process the license tag.
FileCommentSniff::processPackage protected function Process the package tag.
FileCommentSniff::processSubpackage protected function Process the subpackage tag.
FileCommentSniff::processTags protected function Processes each required or optional tag.

API Navigation

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