CommaSniff.php in Coder 8.3.x
File
coder_sniffer/Drupal/Sniffs/WhiteSpace/CommaSniff.php
View source
<?php
namespace Drupal\Sniffs\WhiteSpace;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class CommaSniff implements Sniff {
public function register() {
return [
T_COMMA,
];
}
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, ' ');
}
}
}
}