You are here

CloseBracketSpacingSniff.php in Coder 7.2

File

coder_sniffer/Drupal/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php
View source
<?php

/**
 * Drupal_Sniffs_WhiteSpace_CloseBracketSpacingSniff.
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */

/**
 * Checks that there is no white space before a closing bracket, for ")" and "}".
 * Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class Drupal_Sniffs_WhiteSpace_CloseBracketSpacingSniff implements PHP_CodeSniffer_Sniff {

  /**
   * A list of tokenizers this sniff supports.
   *
   * @var array
   */
  public $supportedTokenizers = array(
    'PHP',
    'JS',
  );

  /**
   * Returns an array of tokens this test wants to listen for.
   *
   * @return array
   */
  public function register() {
    return array(
      T_CLOSE_CURLY_BRACKET,
      T_CLOSE_PARENTHESIS,
    );
  }

  //end register()

  /**
   * Processes this test, when one of its tokens is encountered.
   *
   * @param PHP_CodeSniffer_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(PHP_CodeSniffer_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(PHP_CodeSniffer_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"';
        $phpcsFile
          ->addError($error, $stackPtr - 1, 'ClosingWhitespace', array(
          $tokens[$stackPtr]['content'],
        ));
      }
    }
  }

}

//end class

Classes

Namesort descending Description
Drupal_Sniffs_WhiteSpace_CloseBracketSpacingSniff Checks that there is no white space before a closing bracket, for ")" and "}". Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.