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

Breadcrumb

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

function FileCommentSniff::process

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FileCommentSniff.php \Drupal\Sniffs\Commenting\FileCommentSniff::process()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FileCommentSniff::process()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

int|void

Overrides Sniff::process

1 method overrides FileCommentSniff::process()
ClassCommentSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php, line 93

Class

FileCommentSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Find the next non whitespace token.
    $commentStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    // Allow declare() statements at the top of the file.
    if ($tokens[$commentStart]['code'] === T_DECLARE) {
        $semicolon = $phpcsFile->findNext(T_SEMICOLON, $commentStart + 1);
        $commentStart = $phpcsFile->findNext(T_WHITESPACE, $semicolon + 1, null, true);
    }
    // Ignore vim header.
    if ($tokens[$commentStart]['code'] === T_COMMENT) {
        if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) {
            $commentStart = $phpcsFile->findNext(T_WHITESPACE, $commentStart + 1, null, true);
        }
    }
    $errorToken = $stackPtr + 1;
    if (isset($tokens[$errorToken]) === false) {
        $errorToken--;
    }
    if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
        // We are only interested if this is the first open tag.
        return $phpcsFile->numTokens;
    }
    else {
        if ($tokens[$commentStart]['code'] === T_COMMENT) {
            $error = 'You must use "/**" style comments for a file comment';
            $phpcsFile->addError($error, $errorToken, 'WrongStyle');
            $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes');
            return $phpcsFile->numTokens;
        }
        else {
            if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
                $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing');
                $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
                return $phpcsFile->numTokens;
            }
        }
    }
    $commentEnd = $tokens[$commentStart]['comment_closer'];
    for ($nextToken = $commentEnd + 1; $nextToken < $phpcsFile->numTokens; $nextToken++) {
        if ($tokens[$nextToken]['code'] === T_WHITESPACE) {
            continue;
        }
        if ($tokens[$nextToken]['code'] === T_ATTRIBUTE && isset($tokens[$nextToken]['attribute_closer']) === true) {
            $nextToken = $tokens[$nextToken]['attribute_closer'];
            continue;
        }
        break;
    }
    if ($nextToken === $phpcsFile->numTokens) {
        $nextToken--;
    }
    $ignore = [
        T_CLASS,
        T_INTERFACE,
        T_TRAIT,
        T_ENUM,
        T_FUNCTION,
        T_CLOSURE,
        T_PUBLIC,
        T_PRIVATE,
        T_PROTECTED,
        T_FINAL,
        T_STATIC,
        T_ABSTRACT,
        T_READONLY,
        T_CONST,
        T_PROPERTY,
    ];
    if (in_array($tokens[$nextToken]['code'], $ignore, true) === true) {
        $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing');
        $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
        return $phpcsFile->numTokens;
    }
    $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes');
    // Check the PHP Version, which should be in some text before the first tag.
    $found = false;
    for ($i = $commentStart + 1; $i < $commentEnd; $i++) {
        if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) {
            break;
        }
        else {
            if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING && strstr(strtolower($tokens[$i]['content']), 'php version') !== false) {
                $found = true;
                break;
            }
        }
    }
    if ($found === false) {
        $error = 'PHP version not specified';
        $phpcsFile->addWarning($error, $commentEnd, 'MissingVersion');
    }
    // Check each tag.
    $this->processTags($phpcsFile, $stackPtr, $commentStart);
    // Ignore the rest of the file.
    return $phpcsFile->numTokens;
}

API Navigation

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