You are here

public function UseLeadingBackslashSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php \Drupal\Sniffs\Classes\UseLeadingBackslashSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php \Drupal\Sniffs\Classes\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

File

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 !== null && $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, '');
    }
  }
}