You are here

class OpenBracketSpacingSniff in Coder 8.3

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

Checks that there is no white space after an opening bracket, for "(" and "{". Square Brackets are handled by \PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayBracketSpacingSniff.

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

Hierarchy

Expanded class hierarchy of OpenBracketSpacingSniff

File

coder_sniffer/Drupal/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php, line 24

Namespace

Drupal\Sniffs\WhiteSpace
View source
class OpenBracketSpacingSniff implements Sniff {

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

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

    // Ignore curly brackets in javascript files.
    if ($tokens[$stackPtr]['code'] === T_OPEN_CURLY_BRACKET && $phpcsFile->tokenizerType === 'JS') {
      return;
    }
    if (isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE && strpos($tokens[$stackPtr + 1]['content'], $phpcsFile->eolChar) === false && isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 2]['code'] !== T_CLOSE_TAG) {
      $error = 'There should be no white space after an opening "%s"';
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr + 1, 'OpeningWhitespace', [
        $tokens[$stackPtr]['content'],
      ]);
      if ($fix === true) {
        $phpcsFile->fixer
          ->replaceToken($stackPtr + 1, '');
      }
    }
  }

}

Members

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