You are here

class UseLeadingBackslashSniff in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php \Drupal\Sniffs\Classes\UseLeadingBackslashSniff
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php \Drupal\Sniffs\Classes\UseLeadingBackslashSniff

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

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

Expanded class hierarchy of UseLeadingBackslashSniff

File

coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php, line 23

Namespace

Drupal\Sniffs\Classes
View source
class UseLeadingBackslashSniff implements Sniff {

  /**
   * Returns an array of tokens this test wants to listen for.
   *
   * @return array<int|string>
   */
  public function register() {
    return [
      T_USE,
    ];
  }

  //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();

    // 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, '');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UseLeadingBackslashSniff::process public function Processes this test, when one of its tokens is encountered.
UseLeadingBackslashSniff::register public function Returns an array of tokens this test wants to listen for.