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

Breadcrumb

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

function UseLeadingBackslashSniff::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/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php, line 48

Class

UseLeadingBackslashSniff
Use statements to import classes must not begin with "\".

Namespace

Drupal\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Only check use statements in the global scope.
    if (empty($tokens[$stackPtr]['conditions']) === false) {
        return;
    }
    $startPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
    if ($startPtr !== false && $tokens[$startPtr]['code'] === T_NS_SEPARATOR) {
        $error = 'When importing a class with "use", do not include a leading \\';
        $fix = $phpcsFile->addFixableError($error, $startPtr, 'SeparatorStart');
        if ($fix === true) {
            $phpcsFile->fixer
                ->replaceToken($startPtr, '');
        }
    }
}
RSS feed
Powered by Drupal