public function SpaceUnaryOperatorSniff::process in Coder 8.2
Same name and namespace in other branches
- 8.3 coder_sniffer/Drupal/Sniffs/Formatting/SpaceUnaryOperatorSniff.php \Drupal\Sniffs\Formatting\SpaceUnaryOperatorSniff::process()
- 8.3.x coder_sniffer/Drupal/Sniffs/Formatting/SpaceUnaryOperatorSniff.php \Drupal\Sniffs\Formatting\SpaceUnaryOperatorSniff::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/ Formatting/ SpaceUnaryOperatorSniff.php, line 57
Class
- SpaceUnaryOperatorSniff
- \PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceUnaryOperatorSniff.
Namespace
Drupal\Sniffs\FormattingCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
// Check decrement / increment.
if ($tokens[$stackPtr]['code'] === T_DEC || $tokens[$stackPtr]['code'] === T_INC) {
$previous = $phpcsFile
->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
$modifyLeft = in_array($tokens[$previous]['code'], array(
T_VARIABLE,
T_CLOSE_SQUARE_BRACKET,
T_CLOSE_PARENTHESIS,
T_STRING,
));
if ($modifyLeft === true && $tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
$error = 'There must not be a single space before a unary operator statement';
$fix = $phpcsFile
->addFixableError($error, $stackPtr, 'IncDecLeft');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($stackPtr - 1, '');
}
return;
}
if ($modifyLeft === false && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
$error = 'A unary operator statement must not be followed by a single space';
$fix = $phpcsFile
->addFixableError($error, $stackPtr, 'IncDecRight');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($stackPtr + 1, '');
}
return;
}
}
//end if
// Check "!" operator.
if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
$error = 'A unary operator statement must not be followed by a space';
$fix = $phpcsFile
->addFixableError($error, $stackPtr, 'BooleanNot');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($stackPtr + 1, '');
}
return;
}
// Find the last syntax item to determine if this is an unary operator.
$lastSyntaxItem = $phpcsFile
->findPrevious(array(
T_WHITESPACE,
), $stackPtr - 1, $tokens[$stackPtr]['column'] * -1, true, null, true);
$operatorSuffixAllowed = in_array($tokens[$lastSyntaxItem]['code'], array(
T_LNUMBER,
T_DNUMBER,
T_CLOSE_PARENTHESIS,
T_CLOSE_CURLY_BRACKET,
T_CLOSE_SQUARE_BRACKET,
T_CLOSE_SHORT_ARRAY,
T_VARIABLE,
T_STRING,
));
// Check plus / minus value assignments or comparisons.
if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS) {
if ($operatorSuffixAllowed === false && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
$error = 'A unary operator statement must not be followed by a space';
$fix = $phpcsFile
->addFixableError($error, $stackPtr, 'PlusMinus');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($stackPtr + 1, '');
}
}
}
}