You are here

public function CloseBracketSpacingSniff::process in Coder 8.2

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

Class

CloseBracketSpacingSniff
Checks that there is no white space before a closing bracket, for ")" and "}". Square Brackets are handled by \PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayBracketSpacingSniff.

Namespace

Drupal\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();

  // Ignore curly brackets in javascript files.
  if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET && $phpcsFile->tokenizerType === 'JS') {
    return;
  }
  if (isset($tokens[$stackPtr - 1]) === true && $tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
    $before = $phpcsFile
      ->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
    if ($before !== false && $tokens[$stackPtr]['line'] === $tokens[$before]['line']) {
      $error = 'There should be no white space before a closing "%s"';
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr - 1, 'ClosingWhitespace', array(
        $tokens[$stackPtr]['content'],
      ));
      if ($fix === true) {
        $phpcsFile->fixer
          ->replaceToken($stackPtr - 1, '');
      }
    }
  }
}