@@ -15,65 +15,65 @@ |
||
| 15 | 15 | class PropertyLabelSpacingSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['JS']; |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['JS']; |
|
| 24 | 24 | |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Returns an array of tokens this test wants to listen for. |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function register() |
|
| 32 | - { |
|
| 33 | - return [ |
|
| 34 | - T_PROPERTY, |
|
| 35 | - T_LABEL, |
|
| 36 | - ]; |
|
| 26 | + /** |
|
| 27 | + * Returns an array of tokens this test wants to listen for. |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function register() |
|
| 32 | + { |
|
| 33 | + return [ |
|
| 34 | + T_PROPERTY, |
|
| 35 | + T_LABEL, |
|
| 36 | + ]; |
|
| 37 | 37 | |
| 38 | - }//end register() |
|
| 38 | + }//end register() |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Processes this test, when one of its tokens is encountered. |
|
| 43 | - * |
|
| 44 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 45 | - * @param int $stackPtr The position of the current token |
|
| 46 | - * in the stack passed in $tokens. |
|
| 47 | - * |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function process(File $phpcsFile, $stackPtr) |
|
| 51 | - { |
|
| 52 | - $tokens = $phpcsFile->getTokens(); |
|
| 41 | + /** |
|
| 42 | + * Processes this test, when one of its tokens is encountered. |
|
| 43 | + * |
|
| 44 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 45 | + * @param int $stackPtr The position of the current token |
|
| 46 | + * in the stack passed in $tokens. |
|
| 47 | + * |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function process(File $phpcsFile, $stackPtr) |
|
| 51 | + { |
|
| 52 | + $tokens = $phpcsFile->getTokens(); |
|
| 53 | 53 | |
| 54 | - $colon = $phpcsFile->findNext(T_COLON, ($stackPtr + 1)); |
|
| 54 | + $colon = $phpcsFile->findNext(T_COLON, ($stackPtr + 1)); |
|
| 55 | 55 | |
| 56 | - if ($colon !== ($stackPtr + 1)) { |
|
| 57 | - $error = 'There must be no space before the colon in a property/label declaration'; |
|
| 58 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); |
|
| 59 | - if ($fix === true) { |
|
| 60 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 61 | - } |
|
| 62 | - } |
|
| 56 | + if ($colon !== ($stackPtr + 1)) { |
|
| 57 | + $error = 'There must be no space before the colon in a property/label declaration'; |
|
| 58 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); |
|
| 59 | + if ($fix === true) { |
|
| 60 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - if ($tokens[($colon + 1)]['code'] !== T_WHITESPACE || $tokens[($colon + 1)]['content'] !== ' ') { |
|
| 65 | - $error = 'There must be a single space after the colon in a property/label declaration'; |
|
| 66 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'After'); |
|
| 67 | - if ($fix === true) { |
|
| 68 | - if ($tokens[($colon + 1)]['code'] === T_WHITESPACE) { |
|
| 69 | - $phpcsFile->fixer->replaceToken(($colon + 1), ' '); |
|
| 70 | - } else { |
|
| 71 | - $phpcsFile->fixer->addContent($colon, ' '); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 64 | + if ($tokens[($colon + 1)]['code'] !== T_WHITESPACE || $tokens[($colon + 1)]['content'] !== ' ') { |
|
| 65 | + $error = 'There must be a single space after the colon in a property/label declaration'; |
|
| 66 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'After'); |
|
| 67 | + if ($fix === true) { |
|
| 68 | + if ($tokens[($colon + 1)]['code'] === T_WHITESPACE) { |
|
| 69 | + $phpcsFile->fixer->replaceToken(($colon + 1), ' '); |
|
| 70 | + } else { |
|
| 71 | + $phpcsFile->fixer->addContent($colon, ' '); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - }//end process() |
|
| 76 | + }//end process() |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | 79 | }//end class |
@@ -17,49 +17,49 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Returns an array of tokens this test wants to listen for. |
|
| 22 | - * |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function register() |
|
| 26 | - { |
|
| 27 | - return Tokens::$castTokens; |
|
| 20 | + /** |
|
| 21 | + * Returns an array of tokens this test wants to listen for. |
|
| 22 | + * |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function register() |
|
| 26 | + { |
|
| 27 | + return Tokens::$castTokens; |
|
| 28 | 28 | |
| 29 | - }//end register() |
|
| 29 | + }//end register() |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Processes this test, when one of its tokens is encountered. |
|
| 34 | - * |
|
| 35 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | - * @param int $stackPtr The position of the current token in the |
|
| 37 | - * stack passed in $tokens. |
|
| 38 | - * |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - public function process(File $phpcsFile, $stackPtr) |
|
| 42 | - { |
|
| 43 | - $tokens = $phpcsFile->getTokens(); |
|
| 32 | + /** |
|
| 33 | + * Processes this test, when one of its tokens is encountered. |
|
| 34 | + * |
|
| 35 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | + * @param int $stackPtr The position of the current token in the |
|
| 37 | + * stack passed in $tokens. |
|
| 38 | + * |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + public function process(File $phpcsFile, $stackPtr) |
|
| 42 | + { |
|
| 43 | + $tokens = $phpcsFile->getTokens(); |
|
| 44 | 44 | |
| 45 | - $content = $tokens[$stackPtr]['content']; |
|
| 46 | - $expected = str_replace(' ', '', $content); |
|
| 47 | - $expected = str_replace("\t", '', $expected); |
|
| 45 | + $content = $tokens[$stackPtr]['content']; |
|
| 46 | + $expected = str_replace(' ', '', $content); |
|
| 47 | + $expected = str_replace("\t", '', $expected); |
|
| 48 | 48 | |
| 49 | - if ($content !== $expected) { |
|
| 50 | - $error = 'Cast statements must not contain whitespace; expected "%s" but found "%s"'; |
|
| 51 | - $data = [ |
|
| 52 | - $expected, |
|
| 53 | - $content, |
|
| 54 | - ]; |
|
| 49 | + if ($content !== $expected) { |
|
| 50 | + $error = 'Cast statements must not contain whitespace; expected "%s" but found "%s"'; |
|
| 51 | + $data = [ |
|
| 52 | + $expected, |
|
| 53 | + $content, |
|
| 54 | + ]; |
|
| 55 | 55 | |
| 56 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContainsWhiteSpace', $data); |
|
| 57 | - if ($fix === true) { |
|
| 58 | - $phpcsFile->fixer->replaceToken($stackPtr, $expected); |
|
| 59 | - } |
|
| 60 | - } |
|
| 56 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContainsWhiteSpace', $data); |
|
| 57 | + if ($fix === true) { |
|
| 58 | + $phpcsFile->fixer->replaceToken($stackPtr, $expected); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - }//end process() |
|
| 62 | + }//end process() |
|
| 63 | 63 | |
| 64 | 64 | |
| 65 | 65 | }//end class |
@@ -78,7 +78,7 @@ |
||
| 78 | 78 | $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'], |
| 79 | 79 | $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'], |
| 80 | 80 | ]; |
| 81 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data); |
|
| 81 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data); |
|
| 82 | 82 | if ($fix === true) { |
| 83 | 83 | $phpcsFile->fixer->addContent($stackPtr, ' '); |
| 84 | 84 | } |
@@ -17,73 +17,73 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Returns an array of tokens this test wants to listen for. |
|
| 22 | - * |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function register() |
|
| 26 | - { |
|
| 27 | - return [ |
|
| 28 | - T_ECHO, |
|
| 29 | - T_PRINT, |
|
| 30 | - T_RETURN, |
|
| 31 | - T_INCLUDE, |
|
| 32 | - T_INCLUDE_ONCE, |
|
| 33 | - T_REQUIRE, |
|
| 34 | - T_REQUIRE_ONCE, |
|
| 35 | - T_NEW, |
|
| 36 | - ]; |
|
| 20 | + /** |
|
| 21 | + * Returns an array of tokens this test wants to listen for. |
|
| 22 | + * |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function register() |
|
| 26 | + { |
|
| 27 | + return [ |
|
| 28 | + T_ECHO, |
|
| 29 | + T_PRINT, |
|
| 30 | + T_RETURN, |
|
| 31 | + T_INCLUDE, |
|
| 32 | + T_INCLUDE_ONCE, |
|
| 33 | + T_REQUIRE, |
|
| 34 | + T_REQUIRE_ONCE, |
|
| 35 | + T_NEW, |
|
| 36 | + ]; |
|
| 37 | 37 | |
| 38 | - }//end register() |
|
| 38 | + }//end register() |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Processes this test, when one of its tokens is encountered. |
|
| 43 | - * |
|
| 44 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 45 | - * @param int $stackPtr The position of the current token in |
|
| 46 | - * the stack passed in $tokens. |
|
| 47 | - * |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function process(File $phpcsFile, $stackPtr) |
|
| 51 | - { |
|
| 52 | - $tokens = $phpcsFile->getTokens(); |
|
| 41 | + /** |
|
| 42 | + * Processes this test, when one of its tokens is encountered. |
|
| 43 | + * |
|
| 44 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 45 | + * @param int $stackPtr The position of the current token in |
|
| 46 | + * the stack passed in $tokens. |
|
| 47 | + * |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function process(File $phpcsFile, $stackPtr) |
|
| 51 | + { |
|
| 52 | + $tokens = $phpcsFile->getTokens(); |
|
| 53 | 53 | |
| 54 | - if (isset($tokens[($stackPtr + 1)]) === false) { |
|
| 55 | - // Skip if there is no next token. |
|
| 56 | - return; |
|
| 57 | - } |
|
| 54 | + if (isset($tokens[($stackPtr + 1)]) === false) { |
|
| 55 | + // Skip if there is no next token. |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) { |
|
| 60 | - // No content for this language construct. |
|
| 61 | - return; |
|
| 62 | - } |
|
| 59 | + if ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) { |
|
| 60 | + // No content for this language construct. |
|
| 61 | + return; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
| 65 | - $content = $tokens[($stackPtr + 1)]['content']; |
|
| 66 | - if ($content !== ' ') { |
|
| 67 | - $error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"'; |
|
| 68 | - $data = [Util\Common::prepareForOutput($content)]; |
|
| 69 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data); |
|
| 70 | - if ($fix === true) { |
|
| 71 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } else if ($tokens[($stackPtr + 1)]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 75 | - $error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"'; |
|
| 76 | - $data = [ |
|
| 77 | - $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'], |
|
| 78 | - $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'], |
|
| 79 | - ]; |
|
| 80 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data); |
|
| 81 | - if ($fix === true) { |
|
| 82 | - $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 83 | - } |
|
| 84 | - }//end if |
|
| 64 | + if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
|
| 65 | + $content = $tokens[($stackPtr + 1)]['content']; |
|
| 66 | + if ($content !== ' ') { |
|
| 67 | + $error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"'; |
|
| 68 | + $data = [Util\Common::prepareForOutput($content)]; |
|
| 69 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data); |
|
| 70 | + if ($fix === true) { |
|
| 71 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } else if ($tokens[($stackPtr + 1)]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 75 | + $error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"'; |
|
| 76 | + $data = [ |
|
| 77 | + $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'], |
|
| 78 | + $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'], |
|
| 79 | + ]; |
|
| 80 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data); |
|
| 81 | + if ($fix === true) { |
|
| 82 | + $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 83 | + } |
|
| 84 | + }//end if |
|
| 85 | 85 | |
| 86 | - }//end process() |
|
| 86 | + }//end process() |
|
| 87 | 87 | |
| 88 | 88 | |
| 89 | 89 | }//end class |
@@ -17,80 +17,80 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Returns an array of tokens this test wants to listen for. |
|
| 22 | - * |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function register() |
|
| 26 | - { |
|
| 27 | - return [ |
|
| 28 | - T_OPEN_SQUARE_BRACKET, |
|
| 29 | - T_CLOSE_SQUARE_BRACKET, |
|
| 30 | - ]; |
|
| 20 | + /** |
|
| 21 | + * Returns an array of tokens this test wants to listen for. |
|
| 22 | + * |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function register() |
|
| 26 | + { |
|
| 27 | + return [ |
|
| 28 | + T_OPEN_SQUARE_BRACKET, |
|
| 29 | + T_CLOSE_SQUARE_BRACKET, |
|
| 30 | + ]; |
|
| 31 | 31 | |
| 32 | - }//end register() |
|
| 32 | + }//end register() |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Processes this sniff, when one of its tokens is encountered. |
|
| 37 | - * |
|
| 38 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. |
|
| 39 | - * @param int $stackPtr The position of the current token in the |
|
| 40 | - * stack passed in $tokens. |
|
| 41 | - * |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function process(File $phpcsFile, $stackPtr) |
|
| 45 | - { |
|
| 46 | - $tokens = $phpcsFile->getTokens(); |
|
| 35 | + /** |
|
| 36 | + * Processes this sniff, when one of its tokens is encountered. |
|
| 37 | + * |
|
| 38 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked. |
|
| 39 | + * @param int $stackPtr The position of the current token in the |
|
| 40 | + * stack passed in $tokens. |
|
| 41 | + * |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function process(File $phpcsFile, $stackPtr) |
|
| 45 | + { |
|
| 46 | + $tokens = $phpcsFile->getTokens(); |
|
| 47 | 47 | |
| 48 | - if (($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET |
|
| 49 | - && isset($tokens[$stackPtr]['bracket_closer']) === false) |
|
| 50 | - || ($tokens[$stackPtr]['code'] === T_CLOSE_SQUARE_BRACKET |
|
| 51 | - && isset($tokens[$stackPtr]['bracket_opener']) === false) |
|
| 52 | - ) { |
|
| 53 | - // Bow out for parse error/during live coding. |
|
| 54 | - return; |
|
| 55 | - } |
|
| 48 | + if (($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET |
|
| 49 | + && isset($tokens[$stackPtr]['bracket_closer']) === false) |
|
| 50 | + || ($tokens[$stackPtr]['code'] === T_CLOSE_SQUARE_BRACKET |
|
| 51 | + && isset($tokens[$stackPtr]['bracket_opener']) === false) |
|
| 52 | + ) { |
|
| 53 | + // Bow out for parse error/during live coding. |
|
| 54 | + return; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - // Square brackets can not have a space before them. |
|
| 58 | - $prevType = $tokens[($stackPtr - 1)]['code']; |
|
| 59 | - if ($prevType === T_WHITESPACE) { |
|
| 60 | - $nonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 2), null, true); |
|
| 61 | - $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
|
| 62 | - $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
|
| 63 | - $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
|
| 64 | - $data = [ |
|
| 65 | - $expected, |
|
| 66 | - $found, |
|
| 67 | - ]; |
|
| 68 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
| 69 | - if ($fix === true) { |
|
| 70 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 71 | - } |
|
| 72 | - } |
|
| 57 | + // Square brackets can not have a space before them. |
|
| 58 | + $prevType = $tokens[($stackPtr - 1)]['code']; |
|
| 59 | + if ($prevType === T_WHITESPACE) { |
|
| 60 | + $nonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 2), null, true); |
|
| 61 | + $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
|
| 62 | + $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
|
| 63 | + $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
|
| 64 | + $data = [ |
|
| 65 | + $expected, |
|
| 66 | + $found, |
|
| 67 | + ]; |
|
| 68 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
| 69 | + if ($fix === true) { |
|
| 70 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - // Open square brackets can't ever have spaces after them. |
|
| 75 | - if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
| 76 | - $nextType = $tokens[($stackPtr + 1)]['code']; |
|
| 77 | - if ($nextType === T_WHITESPACE) { |
|
| 78 | - $nonSpace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
| 79 | - $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content']; |
|
| 80 | - $found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1)); |
|
| 81 | - $error = 'Space found after square bracket; expected "%s" but found "%s"'; |
|
| 82 | - $data = [ |
|
| 83 | - $expected, |
|
| 84 | - $found, |
|
| 85 | - ]; |
|
| 86 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
| 87 | - if ($fix === true) { |
|
| 88 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 74 | + // Open square brackets can't ever have spaces after them. |
|
| 75 | + if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
|
| 76 | + $nextType = $tokens[($stackPtr + 1)]['code']; |
|
| 77 | + if ($nextType === T_WHITESPACE) { |
|
| 78 | + $nonSpace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 2), null, true); |
|
| 79 | + $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content']; |
|
| 80 | + $found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1)); |
|
| 81 | + $error = 'Space found after square bracket; expected "%s" but found "%s"'; |
|
| 82 | + $data = [ |
|
| 83 | + $expected, |
|
| 84 | + $found, |
|
| 85 | + ]; |
|
| 86 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
| 87 | + if ($fix === true) { |
|
| 88 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - }//end process() |
|
| 93 | + }//end process() |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | 96 | }//end class |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | $expected, |
| 66 | 66 | $found, |
| 67 | 67 | ]; |
| 68 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
| 68 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
|
| 69 | 69 | if ($fix === true) { |
| 70 | 70 | $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
| 71 | 71 | } |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | $expected, |
| 84 | 84 | $found, |
| 85 | 85 | ]; |
| 86 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
| 86 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data); |
|
| 87 | 87 | if ($fix === true) { |
| 88 | 88 | $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
| 89 | 89 | } |
@@ -16,129 +16,129 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Returns an array of tokens this test wants to listen for. |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function register() |
|
| 25 | - { |
|
| 26 | - return [ |
|
| 27 | - T_CONSTANT_ENCAPSED_STRING, |
|
| 28 | - T_DOUBLE_QUOTED_STRING, |
|
| 29 | - ]; |
|
| 30 | - |
|
| 31 | - }//end register() |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Processes this test, when one of its tokens is encountered. |
|
| 36 | - * |
|
| 37 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 38 | - * @param int $stackPtr The position of the current token |
|
| 39 | - * in the stack passed in $tokens. |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function process(File $phpcsFile, $stackPtr) |
|
| 44 | - { |
|
| 45 | - $tokens = $phpcsFile->getTokens(); |
|
| 46 | - |
|
| 47 | - // If tabs are being converted to spaces by the tokeniser, the |
|
| 48 | - // original content should be used instead of the converted content. |
|
| 49 | - if (isset($tokens[$stackPtr]['orig_content']) === true) { |
|
| 50 | - $workingString = $tokens[$stackPtr]['orig_content']; |
|
| 51 | - } else { |
|
| 52 | - $workingString = $tokens[$stackPtr]['content']; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $lastStringToken = $stackPtr; |
|
| 56 | - |
|
| 57 | - $i = ($stackPtr + 1); |
|
| 58 | - if (isset($tokens[$i]) === true) { |
|
| 59 | - while ($i < $phpcsFile->numTokens |
|
| 60 | - && $tokens[$i]['code'] === $tokens[$stackPtr]['code'] |
|
| 61 | - ) { |
|
| 62 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
| 63 | - $workingString .= $tokens[$i]['orig_content']; |
|
| 64 | - } else { |
|
| 65 | - $workingString .= $tokens[$i]['content']; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $lastStringToken = $i; |
|
| 69 | - $i++; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $skipTo = ($lastStringToken + 1); |
|
| 74 | - |
|
| 75 | - // Check if it's a double quoted string. |
|
| 76 | - if ($workingString[0] !== '"' || substr($workingString, -1) !== '"') { |
|
| 77 | - return $skipTo; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // The use of variables in double quoted strings is not allowed. |
|
| 81 | - if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) { |
|
| 82 | - $stringTokens = token_get_all('<?php '.$workingString); |
|
| 83 | - foreach ($stringTokens as $token) { |
|
| 84 | - if (is_array($token) === true && $token[0] === T_VARIABLE) { |
|
| 85 | - $error = 'Variable "%s" not allowed in double quoted string; use concatenation instead'; |
|
| 86 | - $data = [$token[1]]; |
|
| 87 | - $phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $skipTo; |
|
| 92 | - }//end if |
|
| 93 | - |
|
| 94 | - $allowedChars = [ |
|
| 95 | - '\0', |
|
| 96 | - '\1', |
|
| 97 | - '\2', |
|
| 98 | - '\3', |
|
| 99 | - '\4', |
|
| 100 | - '\5', |
|
| 101 | - '\6', |
|
| 102 | - '\7', |
|
| 103 | - '\n', |
|
| 104 | - '\r', |
|
| 105 | - '\f', |
|
| 106 | - '\t', |
|
| 107 | - '\v', |
|
| 108 | - '\x', |
|
| 109 | - '\b', |
|
| 110 | - '\e', |
|
| 111 | - '\u', |
|
| 112 | - '\'', |
|
| 113 | - ]; |
|
| 114 | - |
|
| 115 | - foreach ($allowedChars as $testChar) { |
|
| 116 | - if (strpos($workingString, $testChar) !== false) { |
|
| 117 | - return $skipTo; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $error = 'String %s does not require double quotes; use single quotes instead'; |
|
| 122 | - $data = [str_replace(["\r", "\n"], ['\r', '\n'], $workingString)]; |
|
| 123 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data); |
|
| 124 | - |
|
| 125 | - if ($fix === true) { |
|
| 126 | - $phpcsFile->fixer->beginChangeset(); |
|
| 127 | - $innerContent = substr($workingString, 1, -1); |
|
| 128 | - $innerContent = str_replace('\"', '"', $innerContent); |
|
| 129 | - $innerContent = str_replace('\\$', '$', $innerContent); |
|
| 130 | - $phpcsFile->fixer->replaceToken($stackPtr, "'$innerContent'"); |
|
| 131 | - while ($lastStringToken !== $stackPtr) { |
|
| 132 | - $phpcsFile->fixer->replaceToken($lastStringToken, ''); |
|
| 133 | - $lastStringToken--; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $phpcsFile->fixer->endChangeset(); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $skipTo; |
|
| 140 | - |
|
| 141 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * Returns an array of tokens this test wants to listen for. |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function register() |
|
| 25 | + { |
|
| 26 | + return [ |
|
| 27 | + T_CONSTANT_ENCAPSED_STRING, |
|
| 28 | + T_DOUBLE_QUOTED_STRING, |
|
| 29 | + ]; |
|
| 30 | + |
|
| 31 | + }//end register() |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Processes this test, when one of its tokens is encountered. |
|
| 36 | + * |
|
| 37 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 38 | + * @param int $stackPtr The position of the current token |
|
| 39 | + * in the stack passed in $tokens. |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function process(File $phpcsFile, $stackPtr) |
|
| 44 | + { |
|
| 45 | + $tokens = $phpcsFile->getTokens(); |
|
| 46 | + |
|
| 47 | + // If tabs are being converted to spaces by the tokeniser, the |
|
| 48 | + // original content should be used instead of the converted content. |
|
| 49 | + if (isset($tokens[$stackPtr]['orig_content']) === true) { |
|
| 50 | + $workingString = $tokens[$stackPtr]['orig_content']; |
|
| 51 | + } else { |
|
| 52 | + $workingString = $tokens[$stackPtr]['content']; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $lastStringToken = $stackPtr; |
|
| 56 | + |
|
| 57 | + $i = ($stackPtr + 1); |
|
| 58 | + if (isset($tokens[$i]) === true) { |
|
| 59 | + while ($i < $phpcsFile->numTokens |
|
| 60 | + && $tokens[$i]['code'] === $tokens[$stackPtr]['code'] |
|
| 61 | + ) { |
|
| 62 | + if (isset($tokens[$i]['orig_content']) === true) { |
|
| 63 | + $workingString .= $tokens[$i]['orig_content']; |
|
| 64 | + } else { |
|
| 65 | + $workingString .= $tokens[$i]['content']; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $lastStringToken = $i; |
|
| 69 | + $i++; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $skipTo = ($lastStringToken + 1); |
|
| 74 | + |
|
| 75 | + // Check if it's a double quoted string. |
|
| 76 | + if ($workingString[0] !== '"' || substr($workingString, -1) !== '"') { |
|
| 77 | + return $skipTo; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // The use of variables in double quoted strings is not allowed. |
|
| 81 | + if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) { |
|
| 82 | + $stringTokens = token_get_all('<?php '.$workingString); |
|
| 83 | + foreach ($stringTokens as $token) { |
|
| 84 | + if (is_array($token) === true && $token[0] === T_VARIABLE) { |
|
| 85 | + $error = 'Variable "%s" not allowed in double quoted string; use concatenation instead'; |
|
| 86 | + $data = [$token[1]]; |
|
| 87 | + $phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $skipTo; |
|
| 92 | + }//end if |
|
| 93 | + |
|
| 94 | + $allowedChars = [ |
|
| 95 | + '\0', |
|
| 96 | + '\1', |
|
| 97 | + '\2', |
|
| 98 | + '\3', |
|
| 99 | + '\4', |
|
| 100 | + '\5', |
|
| 101 | + '\6', |
|
| 102 | + '\7', |
|
| 103 | + '\n', |
|
| 104 | + '\r', |
|
| 105 | + '\f', |
|
| 106 | + '\t', |
|
| 107 | + '\v', |
|
| 108 | + '\x', |
|
| 109 | + '\b', |
|
| 110 | + '\e', |
|
| 111 | + '\u', |
|
| 112 | + '\'', |
|
| 113 | + ]; |
|
| 114 | + |
|
| 115 | + foreach ($allowedChars as $testChar) { |
|
| 116 | + if (strpos($workingString, $testChar) !== false) { |
|
| 117 | + return $skipTo; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $error = 'String %s does not require double quotes; use single quotes instead'; |
|
| 122 | + $data = [str_replace(["\r", "\n"], ['\r', '\n'], $workingString)]; |
|
| 123 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data); |
|
| 124 | + |
|
| 125 | + if ($fix === true) { |
|
| 126 | + $phpcsFile->fixer->beginChangeset(); |
|
| 127 | + $innerContent = substr($workingString, 1, -1); |
|
| 128 | + $innerContent = str_replace('\"', '"', $innerContent); |
|
| 129 | + $innerContent = str_replace('\\$', '$', $innerContent); |
|
| 130 | + $phpcsFile->fixer->replaceToken($stackPtr, "'$innerContent'"); |
|
| 131 | + while ($lastStringToken !== $stackPtr) { |
|
| 132 | + $phpcsFile->fixer->replaceToken($lastStringToken, ''); |
|
| 133 | + $lastStringToken--; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $phpcsFile->fixer->endChangeset(); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $skipTo; |
|
| 140 | + |
|
| 141 | + }//end process() |
|
| 142 | 142 | |
| 143 | 143 | |
| 144 | 144 | }//end class |
@@ -17,72 +17,72 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Returns an array of tokens this test wants to listen for. |
|
| 22 | - * |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function register() |
|
| 26 | - { |
|
| 27 | - return [T_ECHO]; |
|
| 28 | - |
|
| 29 | - }//end register() |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Processes this test, when one of its tokens is encountered. |
|
| 34 | - * |
|
| 35 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | - * @param int $stackPtr The position of the current token in the |
|
| 37 | - * stack passed in $tokens. |
|
| 38 | - * |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - public function process(File $phpcsFile, $stackPtr) |
|
| 42 | - { |
|
| 43 | - $tokens = $phpcsFile->getTokens(); |
|
| 44 | - |
|
| 45 | - $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 46 | - // If the first non-whitespace token is not an opening parenthesis, then we are not concerned. |
|
| 47 | - if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 48 | - $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 49 | - return; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $end = $phpcsFile->findNext([T_SEMICOLON, T_CLOSE_TAG], $stackPtr, null, false); |
|
| 53 | - |
|
| 54 | - // If the token before the semi-colon is not a closing parenthesis, then we are not concerned. |
|
| 55 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($end - 1), null, true); |
|
| 56 | - if ($tokens[$prev]['code'] !== T_CLOSE_PARENTHESIS) { |
|
| 57 | - $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 58 | - return; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // If the parenthesis don't match, then we are not concerned. |
|
| 62 | - if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) { |
|
| 63 | - $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes'); |
|
| 68 | - |
|
| 69 | - if (($phpcsFile->findNext(Tokens::$operators, $stackPtr, $end, false)) === false) { |
|
| 70 | - // There are no arithmetic operators in this. |
|
| 71 | - $error = 'Echoed strings should not be bracketed'; |
|
| 72 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'HasBracket'); |
|
| 73 | - if ($fix === true) { |
|
| 74 | - $phpcsFile->fixer->beginChangeset(); |
|
| 75 | - $phpcsFile->fixer->replaceToken($firstContent, ''); |
|
| 76 | - if ($tokens[($firstContent - 1)]['code'] !== T_WHITESPACE) { |
|
| 77 | - $phpcsFile->fixer->addContent(($firstContent - 1), ' '); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $phpcsFile->fixer->replaceToken($prev, ''); |
|
| 81 | - $phpcsFile->fixer->endChangeset(); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - }//end process() |
|
| 20 | + /** |
|
| 21 | + * Returns an array of tokens this test wants to listen for. |
|
| 22 | + * |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function register() |
|
| 26 | + { |
|
| 27 | + return [T_ECHO]; |
|
| 28 | + |
|
| 29 | + }//end register() |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Processes this test, when one of its tokens is encountered. |
|
| 34 | + * |
|
| 35 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 36 | + * @param int $stackPtr The position of the current token in the |
|
| 37 | + * stack passed in $tokens. |
|
| 38 | + * |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + public function process(File $phpcsFile, $stackPtr) |
|
| 42 | + { |
|
| 43 | + $tokens = $phpcsFile->getTokens(); |
|
| 44 | + |
|
| 45 | + $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 46 | + // If the first non-whitespace token is not an opening parenthesis, then we are not concerned. |
|
| 47 | + if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) { |
|
| 48 | + $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 49 | + return; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $end = $phpcsFile->findNext([T_SEMICOLON, T_CLOSE_TAG], $stackPtr, null, false); |
|
| 53 | + |
|
| 54 | + // If the token before the semi-colon is not a closing parenthesis, then we are not concerned. |
|
| 55 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($end - 1), null, true); |
|
| 56 | + if ($tokens[$prev]['code'] !== T_CLOSE_PARENTHESIS) { |
|
| 57 | + $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // If the parenthesis don't match, then we are not concerned. |
|
| 62 | + if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) { |
|
| 63 | + $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no'); |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes'); |
|
| 68 | + |
|
| 69 | + if (($phpcsFile->findNext(Tokens::$operators, $stackPtr, $end, false)) === false) { |
|
| 70 | + // There are no arithmetic operators in this. |
|
| 71 | + $error = 'Echoed strings should not be bracketed'; |
|
| 72 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'HasBracket'); |
|
| 73 | + if ($fix === true) { |
|
| 74 | + $phpcsFile->fixer->beginChangeset(); |
|
| 75 | + $phpcsFile->fixer->replaceToken($firstContent, ''); |
|
| 76 | + if ($tokens[($firstContent - 1)]['code'] !== T_WHITESPACE) { |
|
| 77 | + $phpcsFile->fixer->addContent(($firstContent - 1), ' '); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $phpcsFile->fixer->replaceToken($prev, ''); |
|
| 81 | + $phpcsFile->fixer->endChangeset(); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + }//end process() |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | }//end class |
@@ -16,289 +16,289 @@ |
||
| 16 | 16 | class SwitchDeclarationSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = [ |
|
| 25 | - 'PHP', |
|
| 26 | - 'JS', |
|
| 27 | - ]; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The number of spaces code should be indented. |
|
| 31 | - * |
|
| 32 | - * @var integer |
|
| 33 | - */ |
|
| 34 | - public $indent = 4; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns an array of tokens this test wants to listen for. |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function register() |
|
| 43 | - { |
|
| 44 | - return [T_SWITCH]; |
|
| 45 | - |
|
| 46 | - }//end register() |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token in the |
|
| 54 | - * stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 61 | - |
|
| 62 | - // We can't process SWITCH statements unless we know where they start and end. |
|
| 63 | - if (isset($tokens[$stackPtr]['scope_opener']) === false |
|
| 64 | - || isset($tokens[$stackPtr]['scope_closer']) === false |
|
| 65 | - ) { |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $switch = $tokens[$stackPtr]; |
|
| 70 | - $nextCase = $stackPtr; |
|
| 71 | - $caseAlignment = ($switch['column'] + $this->indent); |
|
| 72 | - $caseCount = 0; |
|
| 73 | - $foundDefault = false; |
|
| 74 | - |
|
| 75 | - while (($nextCase = $phpcsFile->findNext([T_CASE, T_DEFAULT, T_SWITCH], ($nextCase + 1), $switch['scope_closer'])) !== false) { |
|
| 76 | - // Skip nested SWITCH statements; they are handled on their own. |
|
| 77 | - if ($tokens[$nextCase]['code'] === T_SWITCH) { |
|
| 78 | - $nextCase = $tokens[$nextCase]['scope_closer']; |
|
| 79 | - continue; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ($tokens[$nextCase]['code'] === T_DEFAULT) { |
|
| 83 | - $type = 'Default'; |
|
| 84 | - $foundDefault = true; |
|
| 85 | - } else { |
|
| 86 | - $type = 'Case'; |
|
| 87 | - $caseCount++; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { |
|
| 91 | - $expected = strtolower($tokens[$nextCase]['content']); |
|
| 92 | - $error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 93 | - $data = [ |
|
| 94 | - $expected, |
|
| 95 | - $tokens[$nextCase]['content'], |
|
| 96 | - ]; |
|
| 97 | - |
|
| 98 | - $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data); |
|
| 99 | - if ($fix === true) { |
|
| 100 | - $phpcsFile->fixer->replaceToken($nextCase, $expected); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if ($tokens[$nextCase]['column'] !== $caseAlignment) { |
|
| 105 | - $error = strtoupper($type).' keyword must be indented '.$this->indent.' spaces from SWITCH keyword'; |
|
| 106 | - $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'Indent'); |
|
| 107 | - |
|
| 108 | - if ($fix === true) { |
|
| 109 | - $padding = str_repeat(' ', ($caseAlignment - 1)); |
|
| 110 | - if ($tokens[$nextCase]['column'] === 1 |
|
| 111 | - || $tokens[($nextCase - 1)]['code'] !== T_WHITESPACE |
|
| 112 | - ) { |
|
| 113 | - $phpcsFile->fixer->addContentBefore($nextCase, $padding); |
|
| 114 | - } else { |
|
| 115 | - $phpcsFile->fixer->replaceToken(($nextCase - 1), $padding); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if ($type === 'Case' |
|
| 121 | - && ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE' |
|
| 122 | - || $tokens[($nextCase + 1)]['content'] !== ' ') |
|
| 123 | - ) { |
|
| 124 | - $error = 'CASE keyword must be followed by a single space'; |
|
| 125 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); |
|
| 126 | - if ($fix === true) { |
|
| 127 | - if ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE') { |
|
| 128 | - $phpcsFile->fixer->addContent($nextCase, ' '); |
|
| 129 | - } else { |
|
| 130 | - $phpcsFile->fixer->replaceToken(($nextCase + 1), ' '); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if (isset($tokens[$nextCase]['scope_opener']) === false) { |
|
| 136 | - $error = 'Possible parse error: CASE missing opening colon'; |
|
| 137 | - $phpcsFile->addWarning($error, $nextCase, 'MissingColon'); |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $opener = $tokens[$nextCase]['scope_opener']; |
|
| 142 | - if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') { |
|
| 143 | - $error = 'There must be no space before the colon in a '.strtoupper($type).' statement'; |
|
| 144 | - $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.$type); |
|
| 145 | - if ($fix === true) { |
|
| 146 | - $phpcsFile->fixer->replaceToken(($opener - 1), ''); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $nextBreak = $tokens[$nextCase]['scope_closer']; |
|
| 151 | - if ($tokens[$nextBreak]['code'] === T_BREAK |
|
| 152 | - || $tokens[$nextBreak]['code'] === T_RETURN |
|
| 153 | - || $tokens[$nextBreak]['code'] === T_CONTINUE |
|
| 154 | - || $tokens[$nextBreak]['code'] === T_THROW |
|
| 155 | - || $tokens[$nextBreak]['code'] === T_EXIT |
|
| 156 | - ) { |
|
| 157 | - if ($tokens[$nextBreak]['scope_condition'] === $nextCase) { |
|
| 158 | - // Only need to check a couple of things once, even if the |
|
| 159 | - // break is shared between multiple case statements, or even |
|
| 160 | - // the default case. |
|
| 161 | - if ($tokens[$nextBreak]['column'] !== $caseAlignment) { |
|
| 162 | - $error = 'Case breaking statement must be indented '.$this->indent.' spaces from SWITCH keyword'; |
|
| 163 | - $fix = $phpcsFile->addFixableError($error, $nextBreak, 'BreakIndent'); |
|
| 164 | - |
|
| 165 | - if ($fix === true) { |
|
| 166 | - $padding = str_repeat(' ', ($caseAlignment - 1)); |
|
| 167 | - if ($tokens[$nextBreak]['column'] === 1 |
|
| 168 | - || $tokens[($nextBreak - 1)]['code'] !== T_WHITESPACE |
|
| 169 | - ) { |
|
| 170 | - $phpcsFile->fixer->addContentBefore($nextBreak, $padding); |
|
| 171 | - } else { |
|
| 172 | - $phpcsFile->fixer->replaceToken(($nextBreak - 1), $padding); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextBreak - 1), $stackPtr, true); |
|
| 178 | - if ($tokens[$prev]['line'] !== ($tokens[$nextBreak]['line'] - 1)) { |
|
| 179 | - $error = 'Blank lines are not allowed before case breaking statements'; |
|
| 180 | - $phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak'); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $nextLine = $tokens[$tokens[$stackPtr]['scope_closer']]['line']; |
|
| 184 | - $semicolon = $phpcsFile->findEndOfStatement($nextBreak); |
|
| 185 | - for ($i = ($semicolon + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { |
|
| 186 | - if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 187 | - $nextLine = $tokens[$i]['line']; |
|
| 188 | - break; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if ($type === 'Case') { |
|
| 193 | - // Ensure the BREAK statement is followed by |
|
| 194 | - // a single blank line, or the end switch brace. |
|
| 195 | - if ($nextLine !== ($tokens[$semicolon]['line'] + 2) && $i !== $tokens[$stackPtr]['scope_closer']) { |
|
| 196 | - $error = 'Case breaking statements must be followed by a single blank line'; |
|
| 197 | - $fix = $phpcsFile->addFixableError($error, $nextBreak, 'SpacingAfterBreak'); |
|
| 198 | - if ($fix === true) { |
|
| 199 | - $phpcsFile->fixer->beginChangeset(); |
|
| 200 | - for ($i = ($semicolon + 1); $i <= $tokens[$stackPtr]['scope_closer']; $i++) { |
|
| 201 | - if ($tokens[$i]['line'] === $nextLine) { |
|
| 202 | - $phpcsFile->fixer->addNewlineBefore($i); |
|
| 203 | - break; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($tokens[$i]['line'] === $tokens[$semicolon]['line']) { |
|
| 207 | - continue; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $phpcsFile->fixer->endChangeset(); |
|
| 214 | - } |
|
| 215 | - }//end if |
|
| 216 | - } else { |
|
| 217 | - // Ensure the BREAK statement is not followed by a blank line. |
|
| 218 | - if ($nextLine !== ($tokens[$semicolon]['line'] + 1)) { |
|
| 219 | - $error = 'Blank lines are not allowed after the DEFAULT case\'s breaking statement'; |
|
| 220 | - $phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak'); |
|
| 221 | - } |
|
| 222 | - }//end if |
|
| 223 | - |
|
| 224 | - $caseLine = $tokens[$nextCase]['line']; |
|
| 225 | - $nextLine = $tokens[$nextBreak]['line']; |
|
| 226 | - for ($i = ($opener + 1); $i < $nextBreak; $i++) { |
|
| 227 | - if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 228 | - $nextLine = $tokens[$i]['line']; |
|
| 229 | - break; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - if ($nextLine !== ($caseLine + 1)) { |
|
| 234 | - $error = 'Blank lines are not allowed after '.strtoupper($type).' statements'; |
|
| 235 | - $phpcsFile->addError($error, $nextCase, 'SpacingAfter'.$type); |
|
| 236 | - } |
|
| 237 | - }//end if |
|
| 238 | - |
|
| 239 | - if ($tokens[$nextBreak]['code'] === T_BREAK) { |
|
| 240 | - if ($type === 'Case') { |
|
| 241 | - // Ensure empty CASE statements are not allowed. |
|
| 242 | - // They must have some code content in them. A comment is not enough. |
|
| 243 | - // But count RETURN statements as valid content if they also |
|
| 244 | - // happen to close the CASE statement. |
|
| 245 | - $foundContent = false; |
|
| 246 | - for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) { |
|
| 247 | - if ($tokens[$i]['code'] === T_CASE) { |
|
| 248 | - $i = $tokens[$i]['scope_opener']; |
|
| 249 | - continue; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false) { |
|
| 253 | - $foundContent = true; |
|
| 254 | - break; |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ($foundContent === false) { |
|
| 259 | - $error = 'Empty CASE statements are not allowed'; |
|
| 260 | - $phpcsFile->addError($error, $nextCase, 'EmptyCase'); |
|
| 261 | - } |
|
| 262 | - } else { |
|
| 263 | - // Ensure empty DEFAULT statements are not allowed. |
|
| 264 | - // They must (at least) have a comment describing why |
|
| 265 | - // the default case is being ignored. |
|
| 266 | - $foundContent = false; |
|
| 267 | - for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) { |
|
| 268 | - if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 269 | - $foundContent = true; |
|
| 270 | - break; |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ($foundContent === false) { |
|
| 275 | - $error = 'Comment required for empty DEFAULT case'; |
|
| 276 | - $phpcsFile->addError($error, $nextCase, 'EmptyDefault'); |
|
| 277 | - } |
|
| 278 | - }//end if |
|
| 279 | - }//end if |
|
| 280 | - } else if ($type === 'Default') { |
|
| 281 | - $error = 'DEFAULT case must have a breaking statement'; |
|
| 282 | - $phpcsFile->addError($error, $nextCase, 'DefaultNoBreak'); |
|
| 283 | - }//end if |
|
| 284 | - }//end while |
|
| 285 | - |
|
| 286 | - if ($foundDefault === false) { |
|
| 287 | - $error = 'All SWITCH statements must contain a DEFAULT case'; |
|
| 288 | - $phpcsFile->addError($error, $stackPtr, 'MissingDefault'); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) { |
|
| 292 | - $error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword'; |
|
| 293 | - $phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign'); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - if ($caseCount === 0) { |
|
| 297 | - $error = 'SWITCH statements must contain at least one CASE statement'; |
|
| 298 | - $phpcsFile->addError($error, $stackPtr, 'MissingCase'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = [ |
|
| 25 | + 'PHP', |
|
| 26 | + 'JS', |
|
| 27 | + ]; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The number of spaces code should be indented. |
|
| 31 | + * |
|
| 32 | + * @var integer |
|
| 33 | + */ |
|
| 34 | + public $indent = 4; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns an array of tokens this test wants to listen for. |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function register() |
|
| 43 | + { |
|
| 44 | + return [T_SWITCH]; |
|
| 45 | + |
|
| 46 | + }//end register() |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token in the |
|
| 54 | + * stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | + |
|
| 62 | + // We can't process SWITCH statements unless we know where they start and end. |
|
| 63 | + if (isset($tokens[$stackPtr]['scope_opener']) === false |
|
| 64 | + || isset($tokens[$stackPtr]['scope_closer']) === false |
|
| 65 | + ) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $switch = $tokens[$stackPtr]; |
|
| 70 | + $nextCase = $stackPtr; |
|
| 71 | + $caseAlignment = ($switch['column'] + $this->indent); |
|
| 72 | + $caseCount = 0; |
|
| 73 | + $foundDefault = false; |
|
| 74 | + |
|
| 75 | + while (($nextCase = $phpcsFile->findNext([T_CASE, T_DEFAULT, T_SWITCH], ($nextCase + 1), $switch['scope_closer'])) !== false) { |
|
| 76 | + // Skip nested SWITCH statements; they are handled on their own. |
|
| 77 | + if ($tokens[$nextCase]['code'] === T_SWITCH) { |
|
| 78 | + $nextCase = $tokens[$nextCase]['scope_closer']; |
|
| 79 | + continue; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ($tokens[$nextCase]['code'] === T_DEFAULT) { |
|
| 83 | + $type = 'Default'; |
|
| 84 | + $foundDefault = true; |
|
| 85 | + } else { |
|
| 86 | + $type = 'Case'; |
|
| 87 | + $caseCount++; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { |
|
| 91 | + $expected = strtolower($tokens[$nextCase]['content']); |
|
| 92 | + $error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 93 | + $data = [ |
|
| 94 | + $expected, |
|
| 95 | + $tokens[$nextCase]['content'], |
|
| 96 | + ]; |
|
| 97 | + |
|
| 98 | + $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data); |
|
| 99 | + if ($fix === true) { |
|
| 100 | + $phpcsFile->fixer->replaceToken($nextCase, $expected); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if ($tokens[$nextCase]['column'] !== $caseAlignment) { |
|
| 105 | + $error = strtoupper($type).' keyword must be indented '.$this->indent.' spaces from SWITCH keyword'; |
|
| 106 | + $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'Indent'); |
|
| 107 | + |
|
| 108 | + if ($fix === true) { |
|
| 109 | + $padding = str_repeat(' ', ($caseAlignment - 1)); |
|
| 110 | + if ($tokens[$nextCase]['column'] === 1 |
|
| 111 | + || $tokens[($nextCase - 1)]['code'] !== T_WHITESPACE |
|
| 112 | + ) { |
|
| 113 | + $phpcsFile->fixer->addContentBefore($nextCase, $padding); |
|
| 114 | + } else { |
|
| 115 | + $phpcsFile->fixer->replaceToken(($nextCase - 1), $padding); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if ($type === 'Case' |
|
| 121 | + && ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE' |
|
| 122 | + || $tokens[($nextCase + 1)]['content'] !== ' ') |
|
| 123 | + ) { |
|
| 124 | + $error = 'CASE keyword must be followed by a single space'; |
|
| 125 | + $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); |
|
| 126 | + if ($fix === true) { |
|
| 127 | + if ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE') { |
|
| 128 | + $phpcsFile->fixer->addContent($nextCase, ' '); |
|
| 129 | + } else { |
|
| 130 | + $phpcsFile->fixer->replaceToken(($nextCase + 1), ' '); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if (isset($tokens[$nextCase]['scope_opener']) === false) { |
|
| 136 | + $error = 'Possible parse error: CASE missing opening colon'; |
|
| 137 | + $phpcsFile->addWarning($error, $nextCase, 'MissingColon'); |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $opener = $tokens[$nextCase]['scope_opener']; |
|
| 142 | + if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') { |
|
| 143 | + $error = 'There must be no space before the colon in a '.strtoupper($type).' statement'; |
|
| 144 | + $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.$type); |
|
| 145 | + if ($fix === true) { |
|
| 146 | + $phpcsFile->fixer->replaceToken(($opener - 1), ''); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $nextBreak = $tokens[$nextCase]['scope_closer']; |
|
| 151 | + if ($tokens[$nextBreak]['code'] === T_BREAK |
|
| 152 | + || $tokens[$nextBreak]['code'] === T_RETURN |
|
| 153 | + || $tokens[$nextBreak]['code'] === T_CONTINUE |
|
| 154 | + || $tokens[$nextBreak]['code'] === T_THROW |
|
| 155 | + || $tokens[$nextBreak]['code'] === T_EXIT |
|
| 156 | + ) { |
|
| 157 | + if ($tokens[$nextBreak]['scope_condition'] === $nextCase) { |
|
| 158 | + // Only need to check a couple of things once, even if the |
|
| 159 | + // break is shared between multiple case statements, or even |
|
| 160 | + // the default case. |
|
| 161 | + if ($tokens[$nextBreak]['column'] !== $caseAlignment) { |
|
| 162 | + $error = 'Case breaking statement must be indented '.$this->indent.' spaces from SWITCH keyword'; |
|
| 163 | + $fix = $phpcsFile->addFixableError($error, $nextBreak, 'BreakIndent'); |
|
| 164 | + |
|
| 165 | + if ($fix === true) { |
|
| 166 | + $padding = str_repeat(' ', ($caseAlignment - 1)); |
|
| 167 | + if ($tokens[$nextBreak]['column'] === 1 |
|
| 168 | + || $tokens[($nextBreak - 1)]['code'] !== T_WHITESPACE |
|
| 169 | + ) { |
|
| 170 | + $phpcsFile->fixer->addContentBefore($nextBreak, $padding); |
|
| 171 | + } else { |
|
| 172 | + $phpcsFile->fixer->replaceToken(($nextBreak - 1), $padding); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextBreak - 1), $stackPtr, true); |
|
| 178 | + if ($tokens[$prev]['line'] !== ($tokens[$nextBreak]['line'] - 1)) { |
|
| 179 | + $error = 'Blank lines are not allowed before case breaking statements'; |
|
| 180 | + $phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak'); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $nextLine = $tokens[$tokens[$stackPtr]['scope_closer']]['line']; |
|
| 184 | + $semicolon = $phpcsFile->findEndOfStatement($nextBreak); |
|
| 185 | + for ($i = ($semicolon + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { |
|
| 186 | + if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 187 | + $nextLine = $tokens[$i]['line']; |
|
| 188 | + break; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if ($type === 'Case') { |
|
| 193 | + // Ensure the BREAK statement is followed by |
|
| 194 | + // a single blank line, or the end switch brace. |
|
| 195 | + if ($nextLine !== ($tokens[$semicolon]['line'] + 2) && $i !== $tokens[$stackPtr]['scope_closer']) { |
|
| 196 | + $error = 'Case breaking statements must be followed by a single blank line'; |
|
| 197 | + $fix = $phpcsFile->addFixableError($error, $nextBreak, 'SpacingAfterBreak'); |
|
| 198 | + if ($fix === true) { |
|
| 199 | + $phpcsFile->fixer->beginChangeset(); |
|
| 200 | + for ($i = ($semicolon + 1); $i <= $tokens[$stackPtr]['scope_closer']; $i++) { |
|
| 201 | + if ($tokens[$i]['line'] === $nextLine) { |
|
| 202 | + $phpcsFile->fixer->addNewlineBefore($i); |
|
| 203 | + break; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($tokens[$i]['line'] === $tokens[$semicolon]['line']) { |
|
| 207 | + continue; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $phpcsFile->fixer->endChangeset(); |
|
| 214 | + } |
|
| 215 | + }//end if |
|
| 216 | + } else { |
|
| 217 | + // Ensure the BREAK statement is not followed by a blank line. |
|
| 218 | + if ($nextLine !== ($tokens[$semicolon]['line'] + 1)) { |
|
| 219 | + $error = 'Blank lines are not allowed after the DEFAULT case\'s breaking statement'; |
|
| 220 | + $phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak'); |
|
| 221 | + } |
|
| 222 | + }//end if |
|
| 223 | + |
|
| 224 | + $caseLine = $tokens[$nextCase]['line']; |
|
| 225 | + $nextLine = $tokens[$nextBreak]['line']; |
|
| 226 | + for ($i = ($opener + 1); $i < $nextBreak; $i++) { |
|
| 227 | + if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 228 | + $nextLine = $tokens[$i]['line']; |
|
| 229 | + break; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + if ($nextLine !== ($caseLine + 1)) { |
|
| 234 | + $error = 'Blank lines are not allowed after '.strtoupper($type).' statements'; |
|
| 235 | + $phpcsFile->addError($error, $nextCase, 'SpacingAfter'.$type); |
|
| 236 | + } |
|
| 237 | + }//end if |
|
| 238 | + |
|
| 239 | + if ($tokens[$nextBreak]['code'] === T_BREAK) { |
|
| 240 | + if ($type === 'Case') { |
|
| 241 | + // Ensure empty CASE statements are not allowed. |
|
| 242 | + // They must have some code content in them. A comment is not enough. |
|
| 243 | + // But count RETURN statements as valid content if they also |
|
| 244 | + // happen to close the CASE statement. |
|
| 245 | + $foundContent = false; |
|
| 246 | + for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) { |
|
| 247 | + if ($tokens[$i]['code'] === T_CASE) { |
|
| 248 | + $i = $tokens[$i]['scope_opener']; |
|
| 249 | + continue; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false) { |
|
| 253 | + $foundContent = true; |
|
| 254 | + break; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ($foundContent === false) { |
|
| 259 | + $error = 'Empty CASE statements are not allowed'; |
|
| 260 | + $phpcsFile->addError($error, $nextCase, 'EmptyCase'); |
|
| 261 | + } |
|
| 262 | + } else { |
|
| 263 | + // Ensure empty DEFAULT statements are not allowed. |
|
| 264 | + // They must (at least) have a comment describing why |
|
| 265 | + // the default case is being ignored. |
|
| 266 | + $foundContent = false; |
|
| 267 | + for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) { |
|
| 268 | + if ($tokens[$i]['type'] !== 'T_WHITESPACE') { |
|
| 269 | + $foundContent = true; |
|
| 270 | + break; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ($foundContent === false) { |
|
| 275 | + $error = 'Comment required for empty DEFAULT case'; |
|
| 276 | + $phpcsFile->addError($error, $nextCase, 'EmptyDefault'); |
|
| 277 | + } |
|
| 278 | + }//end if |
|
| 279 | + }//end if |
|
| 280 | + } else if ($type === 'Default') { |
|
| 281 | + $error = 'DEFAULT case must have a breaking statement'; |
|
| 282 | + $phpcsFile->addError($error, $nextCase, 'DefaultNoBreak'); |
|
| 283 | + }//end if |
|
| 284 | + }//end while |
|
| 285 | + |
|
| 286 | + if ($foundDefault === false) { |
|
| 287 | + $error = 'All SWITCH statements must contain a DEFAULT case'; |
|
| 288 | + $phpcsFile->addError($error, $stackPtr, 'MissingDefault'); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) { |
|
| 292 | + $error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword'; |
|
| 293 | + $phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign'); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + if ($caseCount === 0) { |
|
| 297 | + $error = 'SWITCH statements must contain at least one CASE statement'; |
|
| 298 | + $phpcsFile->addError($error, $stackPtr, 'MissingCase'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + }//end process() |
|
| 302 | 302 | |
| 303 | 303 | |
| 304 | 304 | }//end class |
@@ -16,36 +16,36 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Returns an array of tokens this test wants to listen for. |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function register() |
|
| 25 | - { |
|
| 26 | - return [T_ELSEIF]; |
|
| 27 | - |
|
| 28 | - }//end register() |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Processes this test, when one of its tokens is encountered. |
|
| 33 | - * |
|
| 34 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | - * @param int $stackPtr The position of the current token in the |
|
| 36 | - * stack passed in $tokens. |
|
| 37 | - * |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public function process(File $phpcsFile, $stackPtr) |
|
| 41 | - { |
|
| 42 | - $error = 'Usage of ELSEIF not allowed; use ELSE IF instead'; |
|
| 43 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed'); |
|
| 44 | - if ($fix === true) { |
|
| 45 | - $phpcsFile->fixer->replaceToken($stackPtr, 'else if'); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * Returns an array of tokens this test wants to listen for. |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function register() |
|
| 25 | + { |
|
| 26 | + return [T_ELSEIF]; |
|
| 27 | + |
|
| 28 | + }//end register() |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Processes this test, when one of its tokens is encountered. |
|
| 33 | + * |
|
| 34 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 35 | + * @param int $stackPtr The position of the current token in the |
|
| 36 | + * stack passed in $tokens. |
|
| 37 | + * |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public function process(File $phpcsFile, $stackPtr) |
|
| 41 | + { |
|
| 42 | + $error = 'Usage of ELSEIF not allowed; use ELSE IF instead'; |
|
| 43 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed'); |
|
| 44 | + if ($fix === true) { |
|
| 45 | + $phpcsFile->fixer->replaceToken($stackPtr, 'else if'); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + }//end process() |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | 51 | }//end class |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | $this->requiredSpacesAfterOpen, |
| 92 | 92 | $spaceAfterOpen, |
| 93 | 93 | ]; |
| 94 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
| 94 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
| 95 | 95 | if ($fix === true) { |
| 96 | 96 | $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
| 97 | 97 | if ($spaceAfterOpen === 0) { |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | $this->requiredSpacesBeforeClose, |
| 122 | 122 | $spaceBeforeClose, |
| 123 | 123 | ]; |
| 124 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data); |
|
| 124 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data); |
|
| 125 | 125 | if ($fix === true) { |
| 126 | 126 | $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
| 127 | 127 | if ($spaceBeforeClose === 0) { |
@@ -15,222 +15,222 @@ |
||
| 15 | 15 | class ForEachLoopDeclarationSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * How many spaces should follow the opening bracket. |
|
| 20 | - * |
|
| 21 | - * @var integer |
|
| 22 | - */ |
|
| 23 | - public $requiredSpacesAfterOpen = 0; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * How many spaces should precede the closing bracket. |
|
| 27 | - * |
|
| 28 | - * @var integer |
|
| 29 | - */ |
|
| 30 | - public $requiredSpacesBeforeClose = 0; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Returns an array of tokens this test wants to listen for. |
|
| 35 | - * |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 38 | - public function register() |
|
| 39 | - { |
|
| 40 | - return [T_FOREACH]; |
|
| 41 | - |
|
| 42 | - }//end register() |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Processes this test, when one of its tokens is encountered. |
|
| 47 | - * |
|
| 48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | - * @param int $stackPtr The position of the current token in the |
|
| 50 | - * stack passed in $tokens. |
|
| 51 | - * |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function process(File $phpcsFile, $stackPtr) |
|
| 55 | - { |
|
| 56 | - $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
|
| 57 | - $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
|
| 58 | - $tokens = $phpcsFile->getTokens(); |
|
| 59 | - |
|
| 60 | - $openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr); |
|
| 61 | - if ($openingBracket === false) { |
|
| 62 | - $error = 'Possible parse error: FOREACH has no opening parenthesis'; |
|
| 63 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingOpenParenthesis'); |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - if (isset($tokens[$openingBracket]['parenthesis_closer']) === false) { |
|
| 68 | - $error = 'Possible parse error: FOREACH has no closing parenthesis'; |
|
| 69 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingCloseParenthesis'); |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $closingBracket = $tokens[$openingBracket]['parenthesis_closer']; |
|
| 74 | - |
|
| 75 | - if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 76 | - $error = 'Space found after opening bracket of FOREACH loop'; |
|
| 77 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen'); |
|
| 78 | - if ($fix === true) { |
|
| 79 | - $phpcsFile->fixer->replaceToken(($openingBracket + 1), ''); |
|
| 80 | - } |
|
| 81 | - } else if ($this->requiredSpacesAfterOpen > 0) { |
|
| 82 | - $spaceAfterOpen = 0; |
|
| 83 | - if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 84 | - $spaceAfterOpen = $tokens[($openingBracket + 1)]['length']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { |
|
| 88 | - $error = 'Expected %s spaces after opening bracket; %s found'; |
|
| 89 | - $data = [ |
|
| 90 | - $this->requiredSpacesAfterOpen, |
|
| 91 | - $spaceAfterOpen, |
|
| 92 | - ]; |
|
| 93 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
| 94 | - if ($fix === true) { |
|
| 95 | - $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
|
| 96 | - if ($spaceAfterOpen === 0) { |
|
| 97 | - $phpcsFile->fixer->addContent($openingBracket, $padding); |
|
| 98 | - } else { |
|
| 99 | - $phpcsFile->fixer->replaceToken(($openingBracket + 1), $padding); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - }//end if |
|
| 104 | - |
|
| 105 | - if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 106 | - $error = 'Space found before closing bracket of FOREACH loop'; |
|
| 107 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose'); |
|
| 108 | - if ($fix === true) { |
|
| 109 | - $phpcsFile->fixer->replaceToken(($closingBracket - 1), ''); |
|
| 110 | - } |
|
| 111 | - } else if ($this->requiredSpacesBeforeClose > 0) { |
|
| 112 | - $spaceBeforeClose = 0; |
|
| 113 | - if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 114 | - $spaceBeforeClose = $tokens[($closingBracket - 1)]['length']; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) { |
|
| 118 | - $error = 'Expected %s spaces before closing bracket; %s found'; |
|
| 119 | - $data = [ |
|
| 120 | - $this->requiredSpacesBeforeClose, |
|
| 121 | - $spaceBeforeClose, |
|
| 122 | - ]; |
|
| 123 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data); |
|
| 124 | - if ($fix === true) { |
|
| 125 | - $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
|
| 126 | - if ($spaceBeforeClose === 0) { |
|
| 127 | - $phpcsFile->fixer->addContentBefore($closingBracket, $padding); |
|
| 128 | - } else { |
|
| 129 | - $phpcsFile->fixer->replaceToken(($closingBracket - 1), $padding); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - }//end if |
|
| 134 | - |
|
| 135 | - $asToken = $phpcsFile->findNext(T_AS, $openingBracket); |
|
| 136 | - if ($asToken === false) { |
|
| 137 | - $error = 'Possible parse error: FOREACH has no AS statement'; |
|
| 138 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingAs'); |
|
| 139 | - return; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $content = $tokens[$asToken]['content']; |
|
| 143 | - if ($content !== strtolower($content)) { |
|
| 144 | - $expected = strtolower($content); |
|
| 145 | - $error = 'AS keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 146 | - $data = [ |
|
| 147 | - $expected, |
|
| 148 | - $content, |
|
| 149 | - ]; |
|
| 150 | - |
|
| 151 | - $fix = $phpcsFile->addFixableError($error, $asToken, 'AsNotLower', $data); |
|
| 152 | - if ($fix === true) { |
|
| 153 | - $phpcsFile->fixer->replaceToken($asToken, $expected); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $asToken, $closingBracket); |
|
| 158 | - |
|
| 159 | - if ($doubleArrow !== false) { |
|
| 160 | - if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) { |
|
| 161 | - $error = 'Expected 1 space before "=>"; 0 found'; |
|
| 162 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeArrow'); |
|
| 163 | - if ($fix === true) { |
|
| 164 | - $phpcsFile->fixer->addContentBefore($doubleArrow, ' '); |
|
| 165 | - } |
|
| 166 | - } else { |
|
| 167 | - if ($tokens[($doubleArrow - 1)]['length'] !== 1) { |
|
| 168 | - $spaces = $tokens[($doubleArrow - 1)]['length']; |
|
| 169 | - $error = 'Expected 1 space before "=>"; %s found'; |
|
| 170 | - $data = [$spaces]; |
|
| 171 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeArrow', $data); |
|
| 172 | - if ($fix === true) { |
|
| 173 | - $phpcsFile->fixer->replaceToken(($doubleArrow - 1), ' '); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) { |
|
| 179 | - $error = 'Expected 1 space after "=>"; 0 found'; |
|
| 180 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterArrow'); |
|
| 181 | - if ($fix === true) { |
|
| 182 | - $phpcsFile->fixer->addContent($doubleArrow, ' '); |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - if ($tokens[($doubleArrow + 1)]['length'] !== 1) { |
|
| 186 | - $spaces = $tokens[($doubleArrow + 1)]['length']; |
|
| 187 | - $error = 'Expected 1 space after "=>"; %s found'; |
|
| 188 | - $data = [$spaces]; |
|
| 189 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterArrow', $data); |
|
| 190 | - if ($fix === true) { |
|
| 191 | - $phpcsFile->fixer->replaceToken(($doubleArrow + 1), ' '); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - }//end if |
|
| 196 | - |
|
| 197 | - if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) { |
|
| 198 | - $error = 'Expected 1 space before "as"; 0 found'; |
|
| 199 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAs'); |
|
| 200 | - if ($fix === true) { |
|
| 201 | - $phpcsFile->fixer->addContentBefore($asToken, ' '); |
|
| 202 | - } |
|
| 203 | - } else { |
|
| 204 | - if ($tokens[($asToken - 1)]['length'] !== 1) { |
|
| 205 | - $spaces = $tokens[($asToken - 1)]['length']; |
|
| 206 | - $error = 'Expected 1 space before "as"; %s found'; |
|
| 207 | - $data = [$spaces]; |
|
| 208 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAs', $data); |
|
| 209 | - if ($fix === true) { |
|
| 210 | - $phpcsFile->fixer->replaceToken(($asToken - 1), ' '); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) { |
|
| 216 | - $error = 'Expected 1 space after "as"; 0 found'; |
|
| 217 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAs'); |
|
| 218 | - if ($fix === true) { |
|
| 219 | - $phpcsFile->fixer->addContent($asToken, ' '); |
|
| 220 | - } |
|
| 221 | - } else { |
|
| 222 | - if ($tokens[($asToken + 1)]['length'] !== 1) { |
|
| 223 | - $spaces = $tokens[($asToken + 1)]['length']; |
|
| 224 | - $error = 'Expected 1 space after "as"; %s found'; |
|
| 225 | - $data = [$spaces]; |
|
| 226 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAs', $data); |
|
| 227 | - if ($fix === true) { |
|
| 228 | - $phpcsFile->fixer->replaceToken(($asToken + 1), ' '); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * How many spaces should follow the opening bracket. |
|
| 20 | + * |
|
| 21 | + * @var integer |
|
| 22 | + */ |
|
| 23 | + public $requiredSpacesAfterOpen = 0; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * How many spaces should precede the closing bracket. |
|
| 27 | + * |
|
| 28 | + * @var integer |
|
| 29 | + */ |
|
| 30 | + public $requiredSpacesBeforeClose = 0; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Returns an array of tokens this test wants to listen for. |
|
| 35 | + * |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | + public function register() |
|
| 39 | + { |
|
| 40 | + return [T_FOREACH]; |
|
| 41 | + |
|
| 42 | + }//end register() |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Processes this test, when one of its tokens is encountered. |
|
| 47 | + * |
|
| 48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | + * @param int $stackPtr The position of the current token in the |
|
| 50 | + * stack passed in $tokens. |
|
| 51 | + * |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function process(File $phpcsFile, $stackPtr) |
|
| 55 | + { |
|
| 56 | + $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; |
|
| 57 | + $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; |
|
| 58 | + $tokens = $phpcsFile->getTokens(); |
|
| 59 | + |
|
| 60 | + $openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr); |
|
| 61 | + if ($openingBracket === false) { |
|
| 62 | + $error = 'Possible parse error: FOREACH has no opening parenthesis'; |
|
| 63 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingOpenParenthesis'); |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + if (isset($tokens[$openingBracket]['parenthesis_closer']) === false) { |
|
| 68 | + $error = 'Possible parse error: FOREACH has no closing parenthesis'; |
|
| 69 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingCloseParenthesis'); |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $closingBracket = $tokens[$openingBracket]['parenthesis_closer']; |
|
| 74 | + |
|
| 75 | + if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 76 | + $error = 'Space found after opening bracket of FOREACH loop'; |
|
| 77 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen'); |
|
| 78 | + if ($fix === true) { |
|
| 79 | + $phpcsFile->fixer->replaceToken(($openingBracket + 1), ''); |
|
| 80 | + } |
|
| 81 | + } else if ($this->requiredSpacesAfterOpen > 0) { |
|
| 82 | + $spaceAfterOpen = 0; |
|
| 83 | + if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) { |
|
| 84 | + $spaceAfterOpen = $tokens[($openingBracket + 1)]['length']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { |
|
| 88 | + $error = 'Expected %s spaces after opening bracket; %s found'; |
|
| 89 | + $data = [ |
|
| 90 | + $this->requiredSpacesAfterOpen, |
|
| 91 | + $spaceAfterOpen, |
|
| 92 | + ]; |
|
| 93 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); |
|
| 94 | + if ($fix === true) { |
|
| 95 | + $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); |
|
| 96 | + if ($spaceAfterOpen === 0) { |
|
| 97 | + $phpcsFile->fixer->addContent($openingBracket, $padding); |
|
| 98 | + } else { |
|
| 99 | + $phpcsFile->fixer->replaceToken(($openingBracket + 1), $padding); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + }//end if |
|
| 104 | + |
|
| 105 | + if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 106 | + $error = 'Space found before closing bracket of FOREACH loop'; |
|
| 107 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose'); |
|
| 108 | + if ($fix === true) { |
|
| 109 | + $phpcsFile->fixer->replaceToken(($closingBracket - 1), ''); |
|
| 110 | + } |
|
| 111 | + } else if ($this->requiredSpacesBeforeClose > 0) { |
|
| 112 | + $spaceBeforeClose = 0; |
|
| 113 | + if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) { |
|
| 114 | + $spaceBeforeClose = $tokens[($closingBracket - 1)]['length']; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) { |
|
| 118 | + $error = 'Expected %s spaces before closing bracket; %s found'; |
|
| 119 | + $data = [ |
|
| 120 | + $this->requiredSpacesBeforeClose, |
|
| 121 | + $spaceBeforeClose, |
|
| 122 | + ]; |
|
| 123 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data); |
|
| 124 | + if ($fix === true) { |
|
| 125 | + $padding = str_repeat(' ', $this->requiredSpacesBeforeClose); |
|
| 126 | + if ($spaceBeforeClose === 0) { |
|
| 127 | + $phpcsFile->fixer->addContentBefore($closingBracket, $padding); |
|
| 128 | + } else { |
|
| 129 | + $phpcsFile->fixer->replaceToken(($closingBracket - 1), $padding); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + }//end if |
|
| 134 | + |
|
| 135 | + $asToken = $phpcsFile->findNext(T_AS, $openingBracket); |
|
| 136 | + if ($asToken === false) { |
|
| 137 | + $error = 'Possible parse error: FOREACH has no AS statement'; |
|
| 138 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingAs'); |
|
| 139 | + return; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $content = $tokens[$asToken]['content']; |
|
| 143 | + if ($content !== strtolower($content)) { |
|
| 144 | + $expected = strtolower($content); |
|
| 145 | + $error = 'AS keyword must be lowercase; expected "%s" but found "%s"'; |
|
| 146 | + $data = [ |
|
| 147 | + $expected, |
|
| 148 | + $content, |
|
| 149 | + ]; |
|
| 150 | + |
|
| 151 | + $fix = $phpcsFile->addFixableError($error, $asToken, 'AsNotLower', $data); |
|
| 152 | + if ($fix === true) { |
|
| 153 | + $phpcsFile->fixer->replaceToken($asToken, $expected); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $asToken, $closingBracket); |
|
| 158 | + |
|
| 159 | + if ($doubleArrow !== false) { |
|
| 160 | + if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) { |
|
| 161 | + $error = 'Expected 1 space before "=>"; 0 found'; |
|
| 162 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeArrow'); |
|
| 163 | + if ($fix === true) { |
|
| 164 | + $phpcsFile->fixer->addContentBefore($doubleArrow, ' '); |
|
| 165 | + } |
|
| 166 | + } else { |
|
| 167 | + if ($tokens[($doubleArrow - 1)]['length'] !== 1) { |
|
| 168 | + $spaces = $tokens[($doubleArrow - 1)]['length']; |
|
| 169 | + $error = 'Expected 1 space before "=>"; %s found'; |
|
| 170 | + $data = [$spaces]; |
|
| 171 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeArrow', $data); |
|
| 172 | + if ($fix === true) { |
|
| 173 | + $phpcsFile->fixer->replaceToken(($doubleArrow - 1), ' '); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) { |
|
| 179 | + $error = 'Expected 1 space after "=>"; 0 found'; |
|
| 180 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterArrow'); |
|
| 181 | + if ($fix === true) { |
|
| 182 | + $phpcsFile->fixer->addContent($doubleArrow, ' '); |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + if ($tokens[($doubleArrow + 1)]['length'] !== 1) { |
|
| 186 | + $spaces = $tokens[($doubleArrow + 1)]['length']; |
|
| 187 | + $error = 'Expected 1 space after "=>"; %s found'; |
|
| 188 | + $data = [$spaces]; |
|
| 189 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterArrow', $data); |
|
| 190 | + if ($fix === true) { |
|
| 191 | + $phpcsFile->fixer->replaceToken(($doubleArrow + 1), ' '); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + }//end if |
|
| 196 | + |
|
| 197 | + if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) { |
|
| 198 | + $error = 'Expected 1 space before "as"; 0 found'; |
|
| 199 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAs'); |
|
| 200 | + if ($fix === true) { |
|
| 201 | + $phpcsFile->fixer->addContentBefore($asToken, ' '); |
|
| 202 | + } |
|
| 203 | + } else { |
|
| 204 | + if ($tokens[($asToken - 1)]['length'] !== 1) { |
|
| 205 | + $spaces = $tokens[($asToken - 1)]['length']; |
|
| 206 | + $error = 'Expected 1 space before "as"; %s found'; |
|
| 207 | + $data = [$spaces]; |
|
| 208 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAs', $data); |
|
| 209 | + if ($fix === true) { |
|
| 210 | + $phpcsFile->fixer->replaceToken(($asToken - 1), ' '); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) { |
|
| 216 | + $error = 'Expected 1 space after "as"; 0 found'; |
|
| 217 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAs'); |
|
| 218 | + if ($fix === true) { |
|
| 219 | + $phpcsFile->fixer->addContent($asToken, ' '); |
|
| 220 | + } |
|
| 221 | + } else { |
|
| 222 | + if ($tokens[($asToken + 1)]['length'] !== 1) { |
|
| 223 | + $spaces = $tokens[($asToken + 1)]['length']; |
|
| 224 | + $error = 'Expected 1 space after "as"; %s found'; |
|
| 225 | + $data = [$spaces]; |
|
| 226 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAs', $data); |
|
| 227 | + if ($fix === true) { |
|
| 228 | + $phpcsFile->fixer->replaceToken(($asToken + 1), ' '); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + }//end process() |
|
| 234 | 234 | |
| 235 | 235 | |
| 236 | 236 | }//end class |