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

Breadcrumb

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

class InlineCommentSniff

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

Hierarchy

  • class \PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\InlineCommentSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of InlineCommentSniff

File

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

Namespace

PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting
View source
class InlineCommentSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_COMMENT,
        ];
    }
    
    //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();
        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', '/* ... */');
                }
            }
        }
    }
    
    //end process()

}

Members

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