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

Breadcrumb

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

function ValidClassNameSniff::process

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

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

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.:

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/Squiz/Sniffs/Classes/ValidClassNameSniff.php, line 46

Class

ValidClassNameSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr]['scope_opener']) === false) {
        $error = 'Possible parse error: %s missing opening or closing brace';
        $data = [
            $tokens[$stackPtr]['content'],
        ];
        $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
        return;
    }
    // Determine the name of the class or interface. Note that we cannot
    // simply look for the first T_STRING because a class name
    // starting with the number will be multiple tokens.
    $opener = $tokens[$stackPtr]['scope_opener'];
    $nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true);
    $nameEnd = $phpcsFile->findNext([
        T_WHITESPACE,
        T_COLON,
    ], $nameStart, $opener);
    if ($nameEnd === false) {
        $name = $tokens[$nameStart]['content'];
    }
    else {
        $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
    }
    // Check for PascalCase format.
    $valid = Common::isCamelCaps($name, true, true, false);
    if ($valid === false) {
        $type = ucfirst($tokens[$stackPtr]['content']);
        $error = '%s name "%s" is not in PascalCase format';
        $data = [
            $type,
            $name,
        ];
        $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
        $phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'no');
    }
    else {
        $phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'yes');
    }
}
RSS feed
Powered by Drupal