@@ -15,57 +15,57 @@ |
||
| 15 | 15 | class DisallowMultipleStyleDefinitionsSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['CSS']; |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['CSS']; |
|
| 24 | 24 | |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Returns the token types that this sniff is interested in. |
|
| 28 | - * |
|
| 29 | - * @return int[] |
|
| 30 | - */ |
|
| 31 | - public function register() |
|
| 32 | - { |
|
| 33 | - return [T_STYLE]; |
|
| 26 | + /** |
|
| 27 | + * Returns the token types that this sniff is interested in. |
|
| 28 | + * |
|
| 29 | + * @return int[] |
|
| 30 | + */ |
|
| 31 | + public function register() |
|
| 32 | + { |
|
| 33 | + return [T_STYLE]; |
|
| 34 | 34 | |
| 35 | - }//end register() |
|
| 35 | + }//end register() |
|
| 36 | 36 | |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Processes the tokens that this sniff is interested in. |
|
| 40 | - * |
|
| 41 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 42 | - * @param int $stackPtr The position in the stack where |
|
| 43 | - * the token was found. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function process(File $phpcsFile, $stackPtr) |
|
| 48 | - { |
|
| 49 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | - $next = $phpcsFile->findNext(T_STYLE, ($stackPtr + 1)); |
|
| 51 | - if ($next === false) { |
|
| 52 | - return; |
|
| 53 | - } |
|
| 38 | + /** |
|
| 39 | + * Processes the tokens that this sniff is interested in. |
|
| 40 | + * |
|
| 41 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 42 | + * @param int $stackPtr The position in the stack where |
|
| 43 | + * the token was found. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function process(File $phpcsFile, $stackPtr) |
|
| 48 | + { |
|
| 49 | + $tokens = $phpcsFile->getTokens(); |
|
| 50 | + $next = $phpcsFile->findNext(T_STYLE, ($stackPtr + 1)); |
|
| 51 | + if ($next === false) { |
|
| 52 | + return; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - if ($tokens[$next]['content'] === 'progid') { |
|
| 56 | - // Special case for IE filters. |
|
| 57 | - return; |
|
| 58 | - } |
|
| 55 | + if ($tokens[$next]['content'] === 'progid') { |
|
| 56 | + // Special case for IE filters. |
|
| 57 | + return; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 61 | - $error = 'Each style definition must be on a line by itself'; |
|
| 62 | - $fix = $phpcsFile->addFixableError($error, $next, 'Found'); |
|
| 63 | - if ($fix === true) { |
|
| 64 | - $phpcsFile->fixer->addNewlineBefore($next); |
|
| 65 | - } |
|
| 66 | - } |
|
| 60 | + if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) { |
|
| 61 | + $error = 'Each style definition must be on a line by itself'; |
|
| 62 | + $fix = $phpcsFile->addFixableError($error, $next, 'Found'); |
|
| 63 | + if ($fix === true) { |
|
| 64 | + $phpcsFile->fixer->addNewlineBefore($next); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - }//end process() |
|
| 68 | + }//end process() |
|
| 69 | 69 | |
| 70 | 70 | |
| 71 | 71 | }//end class |
@@ -16,40 +16,40 @@ |
||
| 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_CATCH]; |
|
| 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 All the tokens found in the document. |
|
| 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 | - $tokens = $phpcsFile->getTokens(); |
|
| 43 | - |
|
| 44 | - $scopeStart = $tokens[$stackPtr]['scope_opener']; |
|
| 45 | - $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($scopeStart + 1), $tokens[$stackPtr]['scope_closer'], true); |
|
| 46 | - |
|
| 47 | - if ($firstContent === false) { |
|
| 48 | - $error = 'Empty CATCH statement must have a comment to explain why the exception is not handled'; |
|
| 49 | - $phpcsFile->addError($error, $scopeStart, 'Missing'); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - }//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_CATCH]; |
|
| 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 All the tokens found in the document. |
|
| 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 | + $tokens = $phpcsFile->getTokens(); |
|
| 43 | + |
|
| 44 | + $scopeStart = $tokens[$stackPtr]['scope_opener']; |
|
| 45 | + $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($scopeStart + 1), $tokens[$stackPtr]['scope_closer'], true); |
|
| 46 | + |
|
| 47 | + if ($firstContent === false) { |
|
| 48 | + $error = 'Empty CATCH statement must have a comment to explain why the exception is not handled'; |
|
| 49 | + $phpcsFile->addError($error, $scopeStart, 'Missing'); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + }//end process() |
|
| 53 | 53 | |
| 54 | 54 | |
| 55 | 55 | }//end class |
@@ -114,7 +114,7 @@ |
||
| 114 | 114 | ($requiredColumn - 1), |
| 115 | 115 | ($tokens[$i]['column'] - 1), |
| 116 | 116 | ]; |
| 117 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 117 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 118 | 118 | if ($fix === true) { |
| 119 | 119 | $padding = str_repeat(' ', ($requiredColumn - 1)); |
| 120 | 120 | if ($tokens[$i]['column'] === 1) { |
@@ -16,146 +16,146 @@ |
||
| 16 | 16 | class DocCommentAlignmentSniff 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 | - /** |
|
| 31 | - * Returns an array of tokens this test wants to listen for. |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function register() |
|
| 36 | - { |
|
| 37 | - return [T_DOC_COMMENT_OPEN_TAG]; |
|
| 38 | - |
|
| 39 | - }//end register() |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Processes this test, when one of its tokens is encountered. |
|
| 44 | - * |
|
| 45 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 46 | - * @param int $stackPtr The position of the current token |
|
| 47 | - * in the stack passed in $tokens. |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function process(File $phpcsFile, $stackPtr) |
|
| 52 | - { |
|
| 53 | - $tokens = $phpcsFile->getTokens(); |
|
| 54 | - |
|
| 55 | - // We are only interested in function/class/interface doc block comments. |
|
| 56 | - $ignore = Tokens::$emptyTokens; |
|
| 57 | - if ($phpcsFile->tokenizerType === 'JS') { |
|
| 58 | - $ignore[] = T_EQUAL; |
|
| 59 | - $ignore[] = T_STRING; |
|
| 60 | - $ignore[] = T_OBJECT_OPERATOR; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $nextToken = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); |
|
| 64 | - $ignore = [ |
|
| 65 | - T_CLASS => true, |
|
| 66 | - T_INTERFACE => true, |
|
| 67 | - T_ENUM => true, |
|
| 68 | - T_ENUM_CASE => true, |
|
| 69 | - T_FUNCTION => true, |
|
| 70 | - T_PUBLIC => true, |
|
| 71 | - T_PRIVATE => true, |
|
| 72 | - T_PROTECTED => true, |
|
| 73 | - T_STATIC => true, |
|
| 74 | - T_ABSTRACT => true, |
|
| 75 | - T_PROPERTY => true, |
|
| 76 | - T_OBJECT => true, |
|
| 77 | - T_PROTOTYPE => true, |
|
| 78 | - T_VAR => true, |
|
| 79 | - T_READONLY => true, |
|
| 80 | - ]; |
|
| 81 | - |
|
| 82 | - if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) { |
|
| 83 | - // Could be a file comment. |
|
| 84 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 85 | - if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) { |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // There must be one space after each star (unless it is an empty comment line) |
|
| 91 | - // and all the stars must be aligned correctly. |
|
| 92 | - $requiredColumn = ($tokens[$stackPtr]['column'] + 1); |
|
| 93 | - $endComment = $tokens[$stackPtr]['comment_closer']; |
|
| 94 | - for ($i = ($stackPtr + 1); $i <= $endComment; $i++) { |
|
| 95 | - if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR |
|
| 96 | - && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 97 | - ) { |
|
| 98 | - continue; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) { |
|
| 102 | - if (trim($tokens[$i]['content']) === '') { |
|
| 103 | - // Don't process an unfinished docblock close tag during live coding. |
|
| 104 | - continue; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Can't process the close tag if it is not the first thing on the line. |
|
| 108 | - $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true); |
|
| 109 | - if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 110 | - continue; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($tokens[$i]['column'] !== $requiredColumn) { |
|
| 115 | - $error = 'Expected %s space(s) before asterisk; %s found'; |
|
| 116 | - $data = [ |
|
| 117 | - ($requiredColumn - 1), |
|
| 118 | - ($tokens[$i]['column'] - 1), |
|
| 119 | - ]; |
|
| 120 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 121 | - if ($fix === true) { |
|
| 122 | - $padding = str_repeat(' ', ($requiredColumn - 1)); |
|
| 123 | - if ($tokens[$i]['column'] === 1) { |
|
| 124 | - $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 125 | - } else { |
|
| 126 | - $phpcsFile->fixer->replaceToken(($i - 1), $padding); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) { |
|
| 136 | - // Line is empty. |
|
| 137 | - continue; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) { |
|
| 141 | - $error = 'Expected 1 space after asterisk; 0 found'; |
|
| 142 | - $fix = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar'); |
|
| 143 | - if ($fix === true) { |
|
| 144 | - $phpcsFile->fixer->addContent($i, ' '); |
|
| 145 | - } |
|
| 146 | - } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG |
|
| 147 | - && $tokens[($i + 1)]['content'] !== ' ' |
|
| 148 | - ) { |
|
| 149 | - $error = 'Expected 1 space after asterisk; %s found'; |
|
| 150 | - $data = [$tokens[($i + 1)]['length']]; |
|
| 151 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data); |
|
| 152 | - if ($fix === true) { |
|
| 153 | - $phpcsFile->fixer->replaceToken(($i + 1), ' '); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - }//end for |
|
| 157 | - |
|
| 158 | - }//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 | + /** |
|
| 31 | + * Returns an array of tokens this test wants to listen for. |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function register() |
|
| 36 | + { |
|
| 37 | + return [T_DOC_COMMENT_OPEN_TAG]; |
|
| 38 | + |
|
| 39 | + }//end register() |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Processes this test, when one of its tokens is encountered. |
|
| 44 | + * |
|
| 45 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 46 | + * @param int $stackPtr The position of the current token |
|
| 47 | + * in the stack passed in $tokens. |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function process(File $phpcsFile, $stackPtr) |
|
| 52 | + { |
|
| 53 | + $tokens = $phpcsFile->getTokens(); |
|
| 54 | + |
|
| 55 | + // We are only interested in function/class/interface doc block comments. |
|
| 56 | + $ignore = Tokens::$emptyTokens; |
|
| 57 | + if ($phpcsFile->tokenizerType === 'JS') { |
|
| 58 | + $ignore[] = T_EQUAL; |
|
| 59 | + $ignore[] = T_STRING; |
|
| 60 | + $ignore[] = T_OBJECT_OPERATOR; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $nextToken = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); |
|
| 64 | + $ignore = [ |
|
| 65 | + T_CLASS => true, |
|
| 66 | + T_INTERFACE => true, |
|
| 67 | + T_ENUM => true, |
|
| 68 | + T_ENUM_CASE => true, |
|
| 69 | + T_FUNCTION => true, |
|
| 70 | + T_PUBLIC => true, |
|
| 71 | + T_PRIVATE => true, |
|
| 72 | + T_PROTECTED => true, |
|
| 73 | + T_STATIC => true, |
|
| 74 | + T_ABSTRACT => true, |
|
| 75 | + T_PROPERTY => true, |
|
| 76 | + T_OBJECT => true, |
|
| 77 | + T_PROTOTYPE => true, |
|
| 78 | + T_VAR => true, |
|
| 79 | + T_READONLY => true, |
|
| 80 | + ]; |
|
| 81 | + |
|
| 82 | + if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) { |
|
| 83 | + // Could be a file comment. |
|
| 84 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 85 | + if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) { |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // There must be one space after each star (unless it is an empty comment line) |
|
| 91 | + // and all the stars must be aligned correctly. |
|
| 92 | + $requiredColumn = ($tokens[$stackPtr]['column'] + 1); |
|
| 93 | + $endComment = $tokens[$stackPtr]['comment_closer']; |
|
| 94 | + for ($i = ($stackPtr + 1); $i <= $endComment; $i++) { |
|
| 95 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR |
|
| 96 | + && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 97 | + ) { |
|
| 98 | + continue; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) { |
|
| 102 | + if (trim($tokens[$i]['content']) === '') { |
|
| 103 | + // Don't process an unfinished docblock close tag during live coding. |
|
| 104 | + continue; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Can't process the close tag if it is not the first thing on the line. |
|
| 108 | + $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true); |
|
| 109 | + if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 110 | + continue; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($tokens[$i]['column'] !== $requiredColumn) { |
|
| 115 | + $error = 'Expected %s space(s) before asterisk; %s found'; |
|
| 116 | + $data = [ |
|
| 117 | + ($requiredColumn - 1), |
|
| 118 | + ($tokens[$i]['column'] - 1), |
|
| 119 | + ]; |
|
| 120 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 121 | + if ($fix === true) { |
|
| 122 | + $padding = str_repeat(' ', ($requiredColumn - 1)); |
|
| 123 | + if ($tokens[$i]['column'] === 1) { |
|
| 124 | + $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 125 | + } else { |
|
| 126 | + $phpcsFile->fixer->replaceToken(($i - 1), $padding); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) { |
|
| 136 | + // Line is empty. |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) { |
|
| 141 | + $error = 'Expected 1 space after asterisk; 0 found'; |
|
| 142 | + $fix = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar'); |
|
| 143 | + if ($fix === true) { |
|
| 144 | + $phpcsFile->fixer->addContent($i, ' '); |
|
| 145 | + } |
|
| 146 | + } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG |
|
| 147 | + && $tokens[($i + 1)]['content'] !== ' ' |
|
| 148 | + ) { |
|
| 149 | + $error = 'Expected 1 space after asterisk; %s found'; |
|
| 150 | + $data = [$tokens[($i + 1)]['length']]; |
|
| 151 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data); |
|
| 152 | + if ($fix === true) { |
|
| 153 | + $phpcsFile->fixer->replaceToken(($i + 1), ' '); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + }//end for |
|
| 157 | + |
|
| 158 | + }//end process() |
|
| 159 | 159 | |
| 160 | 160 | |
| 161 | 161 | }//end class |
@@ -171,7 +171,7 @@ |
||
| 171 | 171 | $tokens[$stackPtr]['content'], |
| 172 | 172 | $difference, |
| 173 | 173 | ]; |
| 174 | - $fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
|
| 174 | + $fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
|
| 175 | 175 | if ($fix === true) { |
| 176 | 176 | if ($difference === 0) { |
| 177 | 177 | $first = $phpcsFile->findFirstOnLine([], $nextContent, true); |
@@ -17,190 +17,190 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Processes this test, when one of its tokens is encountered. |
|
| 22 | - * |
|
| 23 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 24 | - * @param int $stackPtr The position of the current token |
|
| 25 | - * in the stack passed in $tokens. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function process(File $phpcsFile, $stackPtr) |
|
| 30 | - { |
|
| 31 | - // We want all the errors from the PSR2 standard, plus some of our own. |
|
| 32 | - parent::process($phpcsFile, $stackPtr); |
|
| 33 | - |
|
| 34 | - // Check that this is the only class or interface in the file. |
|
| 35 | - $nextClass = $phpcsFile->findNext([T_CLASS, T_INTERFACE], ($stackPtr + 1)); |
|
| 36 | - if ($nextClass !== false) { |
|
| 37 | - // We have another, so an error is thrown. |
|
| 38 | - $error = 'Only one interface or class is allowed in a file'; |
|
| 39 | - $phpcsFile->addError($error, $nextClass, 'MultipleClasses'); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - }//end process() |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Processes the opening section of a class declaration. |
|
| 47 | - * |
|
| 48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | - * @param int $stackPtr The position of the current token |
|
| 50 | - * in the stack passed in $tokens. |
|
| 51 | - * |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function processOpen(File $phpcsFile, $stackPtr) |
|
| 55 | - { |
|
| 56 | - parent::processOpen($phpcsFile, $stackPtr); |
|
| 57 | - |
|
| 58 | - $tokens = $phpcsFile->getTokens(); |
|
| 59 | - |
|
| 60 | - if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
|
| 61 | - $prevContent = $tokens[($stackPtr - 1)]['content']; |
|
| 62 | - if ($prevContent !== $phpcsFile->eolChar) { |
|
| 63 | - $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
|
| 64 | - $spaces = strlen($blankSpace); |
|
| 65 | - |
|
| 66 | - if ($tokens[($stackPtr - 2)]['code'] !== T_ABSTRACT |
|
| 67 | - && $tokens[($stackPtr - 2)]['code'] !== T_FINAL |
|
| 68 | - ) { |
|
| 69 | - if ($spaces !== 0) { |
|
| 70 | - $type = strtolower($tokens[$stackPtr]['content']); |
|
| 71 | - $error = 'Expected 0 spaces before %s keyword; %s found'; |
|
| 72 | - $data = [ |
|
| 73 | - $type, |
|
| 74 | - $spaces, |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data); |
|
| 78 | - if ($fix === true) { |
|
| 79 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - }//end if |
|
| 84 | - }//end if |
|
| 85 | - |
|
| 86 | - }//end processOpen() |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Processes the closing section of a class declaration. |
|
| 91 | - * |
|
| 92 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 93 | - * @param int $stackPtr The position of the current token |
|
| 94 | - * in the stack passed in $tokens. |
|
| 95 | - * |
|
| 96 | - * @return void |
|
| 97 | - */ |
|
| 98 | - public function processClose(File $phpcsFile, $stackPtr) |
|
| 99 | - { |
|
| 100 | - $tokens = $phpcsFile->getTokens(); |
|
| 101 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $closeBrace = $tokens[$stackPtr]['scope_closer']; |
|
| 106 | - |
|
| 107 | - // Check that the closing brace has one blank line after it. |
|
| 108 | - for ($nextContent = ($closeBrace + 1); $nextContent < $phpcsFile->numTokens; $nextContent++) { |
|
| 109 | - // Ignore comments on the same line as the brace. |
|
| 110 | - if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line'] |
|
| 111 | - && ($tokens[$nextContent]['code'] === T_WHITESPACE |
|
| 112 | - || $tokens[$nextContent]['code'] === T_COMMENT |
|
| 113 | - || isset(Tokens::$phpcsCommentTokens[$tokens[$nextContent]['code']]) === true) |
|
| 114 | - ) { |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if ($tokens[$nextContent]['code'] !== T_WHITESPACE) { |
|
| 119 | - break; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if ($nextContent === $phpcsFile->numTokens) { |
|
| 124 | - // Ignore the line check as this is the very end of the file. |
|
| 125 | - $difference = 1; |
|
| 126 | - } else { |
|
| 127 | - $difference = ($tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), $stackPtr, true); |
|
| 131 | - |
|
| 132 | - if ($difference === -1 |
|
| 133 | - || $tokens[$lastContent]['line'] === $tokens[$closeBrace]['line'] |
|
| 134 | - ) { |
|
| 135 | - $error = 'Closing %s brace must be on a line by itself'; |
|
| 136 | - $data = [$tokens[$stackPtr]['content']]; |
|
| 137 | - $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceSameLine', $data); |
|
| 138 | - if ($fix === true) { |
|
| 139 | - if ($difference === -1) { |
|
| 140 | - $phpcsFile->fixer->addNewlineBefore($nextContent); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if ($tokens[$lastContent]['line'] === $tokens[$closeBrace]['line']) { |
|
| 144 | - $phpcsFile->fixer->addNewlineBefore($closeBrace); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - } else if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { |
|
| 148 | - $prevContent = $tokens[($closeBrace - 1)]['content']; |
|
| 149 | - if ($prevContent !== $phpcsFile->eolChar) { |
|
| 150 | - $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
|
| 151 | - $spaces = strlen($blankSpace); |
|
| 152 | - if ($spaces !== 0) { |
|
| 153 | - if ($tokens[($closeBrace - 1)]['line'] !== $tokens[$closeBrace]['line']) { |
|
| 154 | - $error = 'Expected 0 spaces before closing brace; newline found'; |
|
| 155 | - $phpcsFile->addError($error, $closeBrace, 'NewLineBeforeCloseBrace'); |
|
| 156 | - } else { |
|
| 157 | - $error = 'Expected 0 spaces before closing brace; %s found'; |
|
| 158 | - $data = [$spaces]; |
|
| 159 | - $fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data); |
|
| 160 | - if ($fix === true) { |
|
| 161 | - $phpcsFile->fixer->replaceToken(($closeBrace - 1), ''); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - }//end if |
|
| 167 | - |
|
| 168 | - if ($difference !== -1 && $difference !== 1) { |
|
| 169 | - if ($tokens[$nextContent]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 170 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$nextContent]['comment_closer'] + 1), null, true); |
|
| 171 | - if ($next !== false && $tokens[$next]['code'] === T_FUNCTION) { |
|
| 172 | - return; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $error = 'Closing brace of a %s must be followed by a single blank line; found %s'; |
|
| 177 | - $data = [ |
|
| 178 | - $tokens[$stackPtr]['content'], |
|
| 179 | - $difference, |
|
| 180 | - ]; |
|
| 181 | - $fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
|
| 182 | - if ($fix === true) { |
|
| 183 | - if ($difference === 0) { |
|
| 184 | - $first = $phpcsFile->findFirstOnLine([], $nextContent, true); |
|
| 185 | - $phpcsFile->fixer->addNewlineBefore($first); |
|
| 186 | - } else { |
|
| 187 | - $phpcsFile->fixer->beginChangeset(); |
|
| 188 | - for ($i = ($closeBrace + 1); $i < $nextContent; $i++) { |
|
| 189 | - if ($tokens[$i]['line'] <= ($tokens[$closeBrace]['line'] + 1)) { |
|
| 190 | - continue; |
|
| 191 | - } else if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) { |
|
| 192 | - break; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $phpcsFile->fixer->endChangeset(); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - }//end if |
|
| 202 | - |
|
| 203 | - }//end processClose() |
|
| 20 | + /** |
|
| 21 | + * Processes this test, when one of its tokens is encountered. |
|
| 22 | + * |
|
| 23 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 24 | + * @param int $stackPtr The position of the current token |
|
| 25 | + * in the stack passed in $tokens. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function process(File $phpcsFile, $stackPtr) |
|
| 30 | + { |
|
| 31 | + // We want all the errors from the PSR2 standard, plus some of our own. |
|
| 32 | + parent::process($phpcsFile, $stackPtr); |
|
| 33 | + |
|
| 34 | + // Check that this is the only class or interface in the file. |
|
| 35 | + $nextClass = $phpcsFile->findNext([T_CLASS, T_INTERFACE], ($stackPtr + 1)); |
|
| 36 | + if ($nextClass !== false) { |
|
| 37 | + // We have another, so an error is thrown. |
|
| 38 | + $error = 'Only one interface or class is allowed in a file'; |
|
| 39 | + $phpcsFile->addError($error, $nextClass, 'MultipleClasses'); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + }//end process() |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Processes the opening section of a class declaration. |
|
| 47 | + * |
|
| 48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 49 | + * @param int $stackPtr The position of the current token |
|
| 50 | + * in the stack passed in $tokens. |
|
| 51 | + * |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function processOpen(File $phpcsFile, $stackPtr) |
|
| 55 | + { |
|
| 56 | + parent::processOpen($phpcsFile, $stackPtr); |
|
| 57 | + |
|
| 58 | + $tokens = $phpcsFile->getTokens(); |
|
| 59 | + |
|
| 60 | + if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
|
| 61 | + $prevContent = $tokens[($stackPtr - 1)]['content']; |
|
| 62 | + if ($prevContent !== $phpcsFile->eolChar) { |
|
| 63 | + $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
|
| 64 | + $spaces = strlen($blankSpace); |
|
| 65 | + |
|
| 66 | + if ($tokens[($stackPtr - 2)]['code'] !== T_ABSTRACT |
|
| 67 | + && $tokens[($stackPtr - 2)]['code'] !== T_FINAL |
|
| 68 | + ) { |
|
| 69 | + if ($spaces !== 0) { |
|
| 70 | + $type = strtolower($tokens[$stackPtr]['content']); |
|
| 71 | + $error = 'Expected 0 spaces before %s keyword; %s found'; |
|
| 72 | + $data = [ |
|
| 73 | + $type, |
|
| 74 | + $spaces, |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data); |
|
| 78 | + if ($fix === true) { |
|
| 79 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + }//end if |
|
| 84 | + }//end if |
|
| 85 | + |
|
| 86 | + }//end processOpen() |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Processes the closing section of a class declaration. |
|
| 91 | + * |
|
| 92 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 93 | + * @param int $stackPtr The position of the current token |
|
| 94 | + * in the stack passed in $tokens. |
|
| 95 | + * |
|
| 96 | + * @return void |
|
| 97 | + */ |
|
| 98 | + public function processClose(File $phpcsFile, $stackPtr) |
|
| 99 | + { |
|
| 100 | + $tokens = $phpcsFile->getTokens(); |
|
| 101 | + if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $closeBrace = $tokens[$stackPtr]['scope_closer']; |
|
| 106 | + |
|
| 107 | + // Check that the closing brace has one blank line after it. |
|
| 108 | + for ($nextContent = ($closeBrace + 1); $nextContent < $phpcsFile->numTokens; $nextContent++) { |
|
| 109 | + // Ignore comments on the same line as the brace. |
|
| 110 | + if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line'] |
|
| 111 | + && ($tokens[$nextContent]['code'] === T_WHITESPACE |
|
| 112 | + || $tokens[$nextContent]['code'] === T_COMMENT |
|
| 113 | + || isset(Tokens::$phpcsCommentTokens[$tokens[$nextContent]['code']]) === true) |
|
| 114 | + ) { |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if ($tokens[$nextContent]['code'] !== T_WHITESPACE) { |
|
| 119 | + break; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if ($nextContent === $phpcsFile->numTokens) { |
|
| 124 | + // Ignore the line check as this is the very end of the file. |
|
| 125 | + $difference = 1; |
|
| 126 | + } else { |
|
| 127 | + $difference = ($tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), $stackPtr, true); |
|
| 131 | + |
|
| 132 | + if ($difference === -1 |
|
| 133 | + || $tokens[$lastContent]['line'] === $tokens[$closeBrace]['line'] |
|
| 134 | + ) { |
|
| 135 | + $error = 'Closing %s brace must be on a line by itself'; |
|
| 136 | + $data = [$tokens[$stackPtr]['content']]; |
|
| 137 | + $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceSameLine', $data); |
|
| 138 | + if ($fix === true) { |
|
| 139 | + if ($difference === -1) { |
|
| 140 | + $phpcsFile->fixer->addNewlineBefore($nextContent); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if ($tokens[$lastContent]['line'] === $tokens[$closeBrace]['line']) { |
|
| 144 | + $phpcsFile->fixer->addNewlineBefore($closeBrace); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + } else if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { |
|
| 148 | + $prevContent = $tokens[($closeBrace - 1)]['content']; |
|
| 149 | + if ($prevContent !== $phpcsFile->eolChar) { |
|
| 150 | + $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
|
| 151 | + $spaces = strlen($blankSpace); |
|
| 152 | + if ($spaces !== 0) { |
|
| 153 | + if ($tokens[($closeBrace - 1)]['line'] !== $tokens[$closeBrace]['line']) { |
|
| 154 | + $error = 'Expected 0 spaces before closing brace; newline found'; |
|
| 155 | + $phpcsFile->addError($error, $closeBrace, 'NewLineBeforeCloseBrace'); |
|
| 156 | + } else { |
|
| 157 | + $error = 'Expected 0 spaces before closing brace; %s found'; |
|
| 158 | + $data = [$spaces]; |
|
| 159 | + $fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data); |
|
| 160 | + if ($fix === true) { |
|
| 161 | + $phpcsFile->fixer->replaceToken(($closeBrace - 1), ''); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + }//end if |
|
| 167 | + |
|
| 168 | + if ($difference !== -1 && $difference !== 1) { |
|
| 169 | + if ($tokens[$nextContent]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
| 170 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$nextContent]['comment_closer'] + 1), null, true); |
|
| 171 | + if ($next !== false && $tokens[$next]['code'] === T_FUNCTION) { |
|
| 172 | + return; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $error = 'Closing brace of a %s must be followed by a single blank line; found %s'; |
|
| 177 | + $data = [ |
|
| 178 | + $tokens[$stackPtr]['content'], |
|
| 179 | + $difference, |
|
| 180 | + ]; |
|
| 181 | + $fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
|
| 182 | + if ($fix === true) { |
|
| 183 | + if ($difference === 0) { |
|
| 184 | + $first = $phpcsFile->findFirstOnLine([], $nextContent, true); |
|
| 185 | + $phpcsFile->fixer->addNewlineBefore($first); |
|
| 186 | + } else { |
|
| 187 | + $phpcsFile->fixer->beginChangeset(); |
|
| 188 | + for ($i = ($closeBrace + 1); $i < $nextContent; $i++) { |
|
| 189 | + if ($tokens[$i]['line'] <= ($tokens[$closeBrace]['line'] + 1)) { |
|
| 190 | + continue; |
|
| 191 | + } else if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) { |
|
| 192 | + break; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $phpcsFile->fixer->endChangeset(); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + }//end if |
|
| 202 | + |
|
| 203 | + }//end processClose() |
|
| 204 | 204 | |
| 205 | 205 | |
| 206 | 206 | }//end class |
@@ -15,68 +15,68 @@ |
||
| 15 | 15 | class DuplicatePropertySniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['JS']; |
|
| 24 | - |
|
| 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 [T_OBJECT]; |
|
| 34 | - |
|
| 35 | - }//end register() |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Processes this test, when one of its tokens is encountered. |
|
| 40 | - * |
|
| 41 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed. |
|
| 42 | - * @param int $stackPtr The position of the current token in the |
|
| 43 | - * stack passed in $tokens. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function process(File $phpcsFile, $stackPtr) |
|
| 48 | - { |
|
| 49 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | - |
|
| 51 | - $properties = []; |
|
| 52 | - $wantedTokens = [ |
|
| 53 | - T_PROPERTY, |
|
| 54 | - T_OBJECT, |
|
| 55 | - ]; |
|
| 56 | - |
|
| 57 | - $next = $phpcsFile->findNext($wantedTokens, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']); |
|
| 58 | - while ($next !== false && $next < $tokens[$stackPtr]['bracket_closer']) { |
|
| 59 | - if ($tokens[$next]['code'] === T_OBJECT) { |
|
| 60 | - // Skip nested objects. |
|
| 61 | - $next = $tokens[$next]['bracket_closer']; |
|
| 62 | - } else { |
|
| 63 | - $propName = $tokens[$next]['content']; |
|
| 64 | - if (isset($properties[$propName]) === true) { |
|
| 65 | - $error = 'Duplicate property definition found for "%s"; previously defined on line %s'; |
|
| 66 | - $data = [ |
|
| 67 | - $propName, |
|
| 68 | - $tokens[$properties[$propName]]['line'], |
|
| 69 | - ]; |
|
| 70 | - $phpcsFile->addError($error, $next, 'Found', $data); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $properties[$propName] = $next; |
|
| 74 | - }//end if |
|
| 75 | - |
|
| 76 | - $next = $phpcsFile->findNext($wantedTokens, ($next + 1), $tokens[$stackPtr]['bracket_closer']); |
|
| 77 | - }//end while |
|
| 78 | - |
|
| 79 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['JS']; |
|
| 24 | + |
|
| 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 [T_OBJECT]; |
|
| 34 | + |
|
| 35 | + }//end register() |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Processes this test, when one of its tokens is encountered. |
|
| 40 | + * |
|
| 41 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed. |
|
| 42 | + * @param int $stackPtr The position of the current token in the |
|
| 43 | + * stack passed in $tokens. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function process(File $phpcsFile, $stackPtr) |
|
| 48 | + { |
|
| 49 | + $tokens = $phpcsFile->getTokens(); |
|
| 50 | + |
|
| 51 | + $properties = []; |
|
| 52 | + $wantedTokens = [ |
|
| 53 | + T_PROPERTY, |
|
| 54 | + T_OBJECT, |
|
| 55 | + ]; |
|
| 56 | + |
|
| 57 | + $next = $phpcsFile->findNext($wantedTokens, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']); |
|
| 58 | + while ($next !== false && $next < $tokens[$stackPtr]['bracket_closer']) { |
|
| 59 | + if ($tokens[$next]['code'] === T_OBJECT) { |
|
| 60 | + // Skip nested objects. |
|
| 61 | + $next = $tokens[$next]['bracket_closer']; |
|
| 62 | + } else { |
|
| 63 | + $propName = $tokens[$next]['content']; |
|
| 64 | + if (isset($properties[$propName]) === true) { |
|
| 65 | + $error = 'Duplicate property definition found for "%s"; previously defined on line %s'; |
|
| 66 | + $data = [ |
|
| 67 | + $propName, |
|
| 68 | + $tokens[$properties[$propName]]['line'], |
|
| 69 | + ]; |
|
| 70 | + $phpcsFile->addError($error, $next, 'Found', $data); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $properties[$propName] = $next; |
|
| 74 | + }//end if |
|
| 75 | + |
|
| 76 | + $next = $phpcsFile->findNext($wantedTokens, ($next + 1), $tokens[$stackPtr]['bracket_closer']); |
|
| 77 | + }//end while |
|
| 78 | + |
|
| 79 | + }//end process() |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | 82 | }//end class |
@@ -16,52 +16,52 @@ |
||
| 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_LOGICAL_AND, |
|
| 28 | - T_LOGICAL_OR, |
|
| 29 | - ]; |
|
| 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_LOGICAL_AND, |
|
| 28 | + T_LOGICAL_OR, |
|
| 29 | + ]; |
|
| 30 | 30 | |
| 31 | - }//end register() |
|
| 31 | + }//end register() |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Processes this test, when one of its tokens is encountered. |
|
| 36 | - * |
|
| 37 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned. |
|
| 38 | - * @param int $stackPtr The position of the current token in the |
|
| 39 | - * stack passed in $tokens. |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function process(File $phpcsFile, $stackPtr) |
|
| 44 | - { |
|
| 45 | - $tokens = $phpcsFile->getTokens(); |
|
| 34 | + /** |
|
| 35 | + * Processes this test, when one of its tokens is encountered. |
|
| 36 | + * |
|
| 37 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned. |
|
| 38 | + * @param int $stackPtr The position of the current token in the |
|
| 39 | + * stack passed in $tokens. |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function process(File $phpcsFile, $stackPtr) |
|
| 44 | + { |
|
| 45 | + $tokens = $phpcsFile->getTokens(); |
|
| 46 | 46 | |
| 47 | - $replacements = [ |
|
| 48 | - 'and' => '&&', |
|
| 49 | - 'or' => '||', |
|
| 50 | - ]; |
|
| 47 | + $replacements = [ |
|
| 48 | + 'and' => '&&', |
|
| 49 | + 'or' => '||', |
|
| 50 | + ]; |
|
| 51 | 51 | |
| 52 | - $operator = strtolower($tokens[$stackPtr]['content']); |
|
| 53 | - if (isset($replacements[$operator]) === false) { |
|
| 54 | - return; |
|
| 55 | - } |
|
| 52 | + $operator = strtolower($tokens[$stackPtr]['content']); |
|
| 53 | + if (isset($replacements[$operator]) === false) { |
|
| 54 | + return; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - $error = 'Logical operator "%s" is prohibited; use "%s" instead'; |
|
| 58 | - $data = [ |
|
| 59 | - $operator, |
|
| 60 | - $replacements[$operator], |
|
| 61 | - ]; |
|
| 62 | - $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data); |
|
| 57 | + $error = 'Logical operator "%s" is prohibited; use "%s" instead'; |
|
| 58 | + $data = [ |
|
| 59 | + $operator, |
|
| 60 | + $replacements[$operator], |
|
| 61 | + ]; |
|
| 62 | + $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data); |
|
| 63 | 63 | |
| 64 | - }//end process() |
|
| 64 | + }//end process() |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | 67 | }//end class |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | function myFunction($variable) { |
| 3 | - // $variable is unused. |
|
| 4 | - echo 'hi'; |
|
| 3 | + // $variable is unused. |
|
| 4 | + echo 'hi'; |
|
| 5 | 5 | } |
| 6 | 6 | ?> |
@@ -4,5 +4,5 @@ |
||
| 4 | 4 | //some code |
| 5 | 5 | function foo() |
| 6 | 6 | { |
| 7 | - return 'bar'; |
|
| 7 | + return 'bar'; |
|
| 8 | 8 | } |
@@ -2,5 +2,5 @@ |
||
| 2 | 2 | define("MAXSIZE", 100); |
| 3 | 3 | $defined = true; |
| 4 | 4 | if (defined('MINSIZE') === false) { |
| 5 | - $defined = false; |
|
| 5 | + $defined = false; |
|
| 6 | 6 | } |