You are here

class CommaSniff in Coder 8.3

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

\Drupal\Sniffs\WhiteSpace\CommaSniff.

Checks that there is one space after a comma.

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

Hierarchy

  • class \Drupal\Sniffs\WhiteSpace\CommaSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of CommaSniff

File

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

Namespace

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

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

  //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();
    if (isset($tokens[$stackPtr + 1]) === false) {
      return;
    }
    if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE && $tokens[$stackPtr + 1]['code'] !== T_COMMA && $tokens[$stackPtr + 1]['code'] !== T_CLOSE_PARENTHESIS) {
      $error = 'Expected one space after the comma, 0 found';
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'NoSpace');
      if ($fix === true) {
        $phpcsFile->fixer
          ->addContent($stackPtr, ' ');
      }
      return;
    }
    if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE && isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 2]['line'] === $tokens[$stackPtr + 1]['line'] && $tokens[$stackPtr + 1]['content'] !== ' ') {
      $error = 'Expected one space after the comma, %s found';
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'TooManySpaces', [
        strlen($tokens[$stackPtr + 1]['content']),
      ]);
      if ($fix === true) {
        $phpcsFile->fixer
          ->replaceToken($stackPtr + 1, ' ');
      }
    }
  }

}

Members

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