|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Ensures there are no spaces on increment / decrement statements or on +/- sign |
| 5 | + * operators or "!" boolean negators. |
| 6 | + */ |
| 7 | +class Symfony3Custom_Sniffs_WhiteSpace_UnaryOperatorSpacingSniff implements PHP_CodeSniffer_Sniff |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Returns an array of tokens this test wants to listen for. |
| 11 | + * |
| 12 | + * @return array |
| 13 | + */ |
| 14 | + public function register() |
| 15 | + { |
| 16 | + return array( |
| 17 | + T_DEC, |
| 18 | + T_INC, |
| 19 | + T_MINUS, |
| 20 | + T_PLUS, |
| 21 | + T_BOOLEAN_NOT, |
| 22 | + ); |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Processes this test, when one of its tokens is encountered. |
| 29 | + * |
| 30 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 31 | + * @param int $stackPtr The position of the current token in |
| 32 | + * the stack passed in $tokens. |
| 33 | + * |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 37 | + { |
| 38 | + $tokens = $phpcsFile->getTokens(); |
| 39 | + |
| 40 | + // Check decrement / increment. |
| 41 | + if ($tokens[$stackPtr]['code'] === T_DEC || $tokens[$stackPtr]['code'] === T_INC) { |
| 42 | + $modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$' || |
| 43 | + $tokens[($stackPtr + 1)]['content'] === ';'; |
| 44 | + |
| 45 | + if ($modifyLeft === true && $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
| 46 | + $error = 'There must not be a single space before a unary operator statement'; |
| 47 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncDecLeft'); |
| 48 | + |
| 49 | + if ($fix === true) { |
| 50 | + $phpcsFile->fixer->replaceToken($stackPtr - 1, ''); |
| 51 | + } |
| 52 | + |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if ($modifyLeft === false && substr($tokens[($stackPtr + 1)]['content'], 0, 1) !== '$') { |
| 57 | + $error = 'A unary operator statement must not be followed by a single space'; |
| 58 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncDecRight'); |
| 59 | + |
| 60 | + if ($fix === true) { |
| 61 | + $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); |
| 62 | + } |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Check "!" operator. |
| 69 | + if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) { |
| 70 | + $error = 'A unary operator statement must not be followed by a space'; |
| 71 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'BooleanNot'); |
| 72 | + |
| 73 | + if ($fix === true) { |
| 74 | + $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); |
| 75 | + } |
| 76 | + |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Find the last syntax item to determine if this is an unary operator. |
| 81 | + $lastSyntaxItem = $phpcsFile->findPrevious( |
| 82 | + array(T_WHITESPACE), |
| 83 | + $stackPtr - 1, |
| 84 | + ($tokens[$stackPtr]['column']) * -1, |
| 85 | + true, |
| 86 | + null, |
| 87 | + true |
| 88 | + ); |
| 89 | + $operatorSuffixAllowed = in_array( |
| 90 | + $tokens[$lastSyntaxItem]['code'], |
| 91 | + array( |
| 92 | + T_LNUMBER, |
| 93 | + T_DNUMBER, |
| 94 | + T_CLOSE_PARENTHESIS, |
| 95 | + T_CLOSE_CURLY_BRACKET, |
| 96 | + T_CLOSE_SQUARE_BRACKET, |
| 97 | + T_VARIABLE, |
| 98 | + T_STRING, |
| 99 | + ) |
| 100 | + ); |
| 101 | + |
| 102 | + // Check plus / minus value assignments or comparisons. |
| 103 | + if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS) { |
| 104 | + if ($operatorSuffixAllowed === false |
| 105 | + && $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE |
| 106 | + ) { |
| 107 | + $error = 'A unary operator statement must not be followed by a space'; |
| 108 | + $fix = $phpcsFile->addFixableError($error, $stackPtr); |
| 109 | + |
| 110 | + if ($fix === true) { |
| 111 | + $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | +} |
0 commit comments