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

Breadcrumb

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

function FunctionCommentSniff::process

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php \Drupal\Sniffs\Commenting\FunctionCommentSniff::process()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Commenting/FunctionCommentSniff.php \PHP_CodeSniffer\Standards\MySource\Sniffs\Commenting\FunctionCommentSniff::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

void

Overrides Sniff::process

2 calls to FunctionCommentSniff::process()
FunctionCommentSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Commenting/FunctionCommentSniff.php
Processes this test, when one of its tokens is encountered.
FunctionCommentSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Commenting/FunctionCommentSniff.php
Processes this test, when one of its tokens is encountered.
1 method overrides FunctionCommentSniff::process()
FunctionCommentSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/MySource/Sniffs/Commenting/FunctionCommentSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php, line 60

Class

FunctionCommentSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting

Code

public function process(File $phpcsFile, $stackPtr) {
    $scopeModifier = $phpcsFile->getMethodProperties($stackPtr)['scope'];
    if ($scopeModifier === 'protected' && $this->minimumVisibility === 'public' || $scopeModifier === 'private' && ($this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected')) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $ignore = Tokens::$methodPrefixes;
    $ignore[T_WHITESPACE] = T_WHITESPACE;
    for ($commentEnd = $stackPtr - 1; $commentEnd >= 0; $commentEnd--) {
        if (isset($ignore[$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_COMMENT) {
        // Inline comments might just be closing comments for
        // control structures or functions instead of function comments
        // using the wrong comment type. If there is other code on the line,
        // assume they relate to that code.
        $prev = $phpcsFile->findPrevious($ignore, $commentEnd - 1, null, true);
        if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
            $commentEnd = $prev;
        }
    }
    if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) {
        $function = $phpcsFile->getDeclarationName($stackPtr);
        $phpcsFile->addError('Missing doc comment for function %s()', $stackPtr, 'Missing', [
            $function,
        ]);
        $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no');
        return;
    }
    else {
        $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes');
    }
    if ($tokens[$commentEnd]['code'] === T_COMMENT) {
        $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle');
        return;
    }
    if ($tokens[$commentEnd]['line'] !== $tokens[$stackPtr]['line'] - 1) {
        for ($i = $commentEnd + 1; $i < $stackPtr; $i++) {
            if ($tokens[$i]['column'] !== 1) {
                continue;
            }
            if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['line'] !== $tokens[$i + 1]['line']) {
                $error = 'There must be no blank lines after the function comment';
                $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter');
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->beginChangeset();
                    while ($i < $stackPtr && $tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['line'] !== $tokens[$i + 1]['line']) {
                        $phpcsFile->fixer
                            ->replaceToken($i++, '');
                    }
                    $phpcsFile->fixer
                        ->endChangeset();
                }
                break;
            }
        }
        
        //end for
    }
    
    //end if
    $commentStart = $tokens[$commentEnd]['comment_opener'];
    foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
        if ($tokens[$tag]['content'] === '@see') {
            // Make sure the tag isn't empty.
            $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
            if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
                $error = 'Content missing for @see tag in function comment';
                $phpcsFile->addError($error, $tag, 'EmptySees');
            }
        }
    }
    $this->processReturn($phpcsFile, $stackPtr, $commentStart);
    $this->processThrows($phpcsFile, $stackPtr, $commentStart);
    $this->processParams($phpcsFile, $stackPtr, $commentStart);
}

API Navigation

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