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

Breadcrumb

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

function InlineCommentSniff::process

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

File

vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Commenting/InlineCommentSniff.php, line 40

Class

InlineCommentSniff

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$stackPtr]['content'][0] === '#') {
        $phpcsFile->recordMetric($stackPtr, 'Inline comment style', '# ...');
        $error = 'Perl-style comments are not allowed. Use "// Comment."';
        $error .= ' or "/* comment */" instead.';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle');
        if ($fix === true) {
            $newComment = ltrim($tokens[$stackPtr]['content'], '# ');
            $newComment = '// ' . $newComment;
            $phpcsFile->fixer
                ->replaceToken($stackPtr, $newComment);
        }
    }
    else {
        if ($tokens[$stackPtr]['content'][0] === '/' && $tokens[$stackPtr]['content'][1] === '/') {
            $phpcsFile->recordMetric($stackPtr, 'Inline comment style', '// ...');
        }
        else {
            if ($tokens[$stackPtr]['content'][0] === '/' && $tokens[$stackPtr]['content'][1] === '*') {
                $phpcsFile->recordMetric($stackPtr, 'Inline comment style', '/* ... */');
            }
        }
    }
}
RSS feed
Powered by Drupal