@@ -16,92 +16,92 @@ |
||
| 16 | 16 | class ColonSpacingSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = ['CSS']; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Returns the token types that this sniff is interested in. |
|
| 29 | - * |
|
| 30 | - * @return int[] |
|
| 31 | - */ |
|
| 32 | - public function register() |
|
| 33 | - { |
|
| 34 | - return [T_COLON]; |
|
| 35 | - |
|
| 36 | - }//end register() |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Processes the tokens that this sniff is interested in. |
|
| 41 | - * |
|
| 42 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | - * @param int $stackPtr The position in the stack where |
|
| 44 | - * the token was found. |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function process(File $phpcsFile, $stackPtr) |
|
| 49 | - { |
|
| 50 | - $tokens = $phpcsFile->getTokens(); |
|
| 51 | - |
|
| 52 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 53 | - if ($tokens[$prev]['code'] !== T_STYLE) { |
|
| 54 | - // The colon is not part of a style definition. |
|
| 55 | - return; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if ($tokens[$prev]['content'] === 'progid') { |
|
| 59 | - // Special case for IE filters. |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
|
| 64 | - $error = 'There must be no space before a colon in a style definition'; |
|
| 65 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); |
|
| 66 | - if ($fix === true) { |
|
| 67 | - $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 72 | - if ($tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['code'] === T_STYLE) { |
|
| 73 | - // Empty style definition, ignore it. |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
| 78 | - $error = 'Expected 1 space after colon in style definition; 0 found'; |
|
| 79 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoneAfter'); |
|
| 80 | - if ($fix === true) { |
|
| 81 | - $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 82 | - } |
|
| 83 | - } else { |
|
| 84 | - $content = $tokens[($stackPtr + 1)]['content']; |
|
| 85 | - if (strpos($content, $phpcsFile->eolChar) === false) { |
|
| 86 | - $length = strlen($content); |
|
| 87 | - if ($length !== 1) { |
|
| 88 | - $error = 'Expected 1 space after colon in style definition; %s found'; |
|
| 89 | - $data = [$length]; |
|
| 90 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'After', $data); |
|
| 91 | - if ($fix === true) { |
|
| 92 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } else { |
|
| 96 | - $error = 'Expected 1 space after colon in style definition; newline found'; |
|
| 97 | - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'AfterNewline'); |
|
| 98 | - if ($fix === true) { |
|
| 99 | - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - }//end if |
|
| 103 | - |
|
| 104 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = ['CSS']; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Returns the token types that this sniff is interested in. |
|
| 29 | + * |
|
| 30 | + * @return int[] |
|
| 31 | + */ |
|
| 32 | + public function register() |
|
| 33 | + { |
|
| 34 | + return [T_COLON]; |
|
| 35 | + |
|
| 36 | + }//end register() |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Processes the tokens that this sniff is interested in. |
|
| 41 | + * |
|
| 42 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | + * @param int $stackPtr The position in the stack where |
|
| 44 | + * the token was found. |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function process(File $phpcsFile, $stackPtr) |
|
| 49 | + { |
|
| 50 | + $tokens = $phpcsFile->getTokens(); |
|
| 51 | + |
|
| 52 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 53 | + if ($tokens[$prev]['code'] !== T_STYLE) { |
|
| 54 | + // The colon is not part of a style definition. |
|
| 55 | + return; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if ($tokens[$prev]['content'] === 'progid') { |
|
| 59 | + // Special case for IE filters. |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
|
| 64 | + $error = 'There must be no space before a colon in a style definition'; |
|
| 65 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); |
|
| 66 | + if ($fix === true) { |
|
| 67 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
| 72 | + if ($tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['code'] === T_STYLE) { |
|
| 73 | + // Empty style definition, ignore it. |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
|
| 78 | + $error = 'Expected 1 space after colon in style definition; 0 found'; |
|
| 79 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoneAfter'); |
|
| 80 | + if ($fix === true) { |
|
| 81 | + $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
| 82 | + } |
|
| 83 | + } else { |
|
| 84 | + $content = $tokens[($stackPtr + 1)]['content']; |
|
| 85 | + if (strpos($content, $phpcsFile->eolChar) === false) { |
|
| 86 | + $length = strlen($content); |
|
| 87 | + if ($length !== 1) { |
|
| 88 | + $error = 'Expected 1 space after colon in style definition; %s found'; |
|
| 89 | + $data = [$length]; |
|
| 90 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'After', $data); |
|
| 91 | + if ($fix === true) { |
|
| 92 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } else { |
|
| 96 | + $error = 'Expected 1 space after colon in style definition; newline found'; |
|
| 97 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'AfterNewline'); |
|
| 98 | + if ($fix === true) { |
|
| 99 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + }//end if |
|
| 103 | + |
|
| 104 | + }//end process() |
|
| 105 | 105 | |
| 106 | 106 | |
| 107 | 107 | }//end class |
@@ -16,49 +16,49 @@ |
||
| 16 | 16 | class EmptyStyleDefinitionSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = ['CSS']; |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = ['CSS']; |
|
| 25 | 25 | |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Returns the token types that this sniff is interested in. |
|
| 29 | - * |
|
| 30 | - * @return int[] |
|
| 31 | - */ |
|
| 32 | - public function register() |
|
| 33 | - { |
|
| 34 | - return [T_STYLE]; |
|
| 27 | + /** |
|
| 28 | + * Returns the token types that this sniff is interested in. |
|
| 29 | + * |
|
| 30 | + * @return int[] |
|
| 31 | + */ |
|
| 32 | + public function register() |
|
| 33 | + { |
|
| 34 | + return [T_STYLE]; |
|
| 35 | 35 | |
| 36 | - }//end register() |
|
| 36 | + }//end register() |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Processes the tokens that this sniff is interested in. |
|
| 41 | - * |
|
| 42 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | - * @param int $stackPtr The position in the stack where |
|
| 44 | - * the token was found. |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function process(File $phpcsFile, $stackPtr) |
|
| 49 | - { |
|
| 50 | - $tokens = $phpcsFile->getTokens(); |
|
| 39 | + /** |
|
| 40 | + * Processes the tokens that this sniff is interested in. |
|
| 41 | + * |
|
| 42 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | + * @param int $stackPtr The position in the stack where |
|
| 44 | + * the token was found. |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function process(File $phpcsFile, $stackPtr) |
|
| 49 | + { |
|
| 50 | + $tokens = $phpcsFile->getTokens(); |
|
| 51 | 51 | |
| 52 | - $ignore = Tokens::$emptyTokens; |
|
| 53 | - $ignore[] = T_COLON; |
|
| 52 | + $ignore = Tokens::$emptyTokens; |
|
| 53 | + $ignore[] = T_COLON; |
|
| 54 | 54 | |
| 55 | - $next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); |
|
| 56 | - if ($next === false || $tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 57 | - $error = 'Style definition is empty'; |
|
| 58 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 59 | - } |
|
| 55 | + $next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); |
|
| 56 | + if ($next === false || $tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
| 57 | + $error = 'Style definition is empty'; |
|
| 58 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - }//end process() |
|
| 61 | + }//end process() |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | }//end class |
@@ -15,83 +15,83 @@ |
||
| 15 | 15 | class LowercaseStyleDefinitionSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['CSS']; |
|
| 24 | - |
|
| 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_OPEN_CURLY_BRACKET]; |
|
| 34 | - |
|
| 35 | - }//end register() |
|
| 36 | - |
|
| 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 | - $start = ($stackPtr + 1); |
|
| 51 | - $end = ($tokens[$stackPtr]['bracket_closer'] - 1); |
|
| 52 | - $inStyle = null; |
|
| 53 | - |
|
| 54 | - for ($i = $start; $i <= $end; $i++) { |
|
| 55 | - // Skip nested definitions as they are checked individually. |
|
| 56 | - if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) { |
|
| 57 | - $i = $tokens[$i]['bracket_closer']; |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - if ($tokens[$i]['code'] === T_STYLE) { |
|
| 62 | - $inStyle = $tokens[$i]['content']; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if ($tokens[$i]['code'] === T_SEMICOLON) { |
|
| 66 | - $inStyle = null; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ($inStyle === 'progid') { |
|
| 70 | - // Special case for IE filters. |
|
| 71 | - continue; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ($tokens[$i]['code'] === T_STYLE |
|
| 75 | - || ($inStyle !== null |
|
| 76 | - && $tokens[$i]['code'] === T_STRING) |
|
| 77 | - ) { |
|
| 78 | - $expected = strtolower($tokens[$i]['content']); |
|
| 79 | - if ($expected !== $tokens[$i]['content']) { |
|
| 80 | - $error = 'Style definitions must be lowercase; expected %s but found %s'; |
|
| 81 | - $data = [ |
|
| 82 | - $expected, |
|
| 83 | - $tokens[$i]['content'], |
|
| 84 | - ]; |
|
| 85 | - |
|
| 86 | - $fix = $phpcsFile->addFixableError($error, $i, 'FoundUpper', $data); |
|
| 87 | - if ($fix === true) { |
|
| 88 | - $phpcsFile->fixer->replaceToken($i, $expected); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - }//end for |
|
| 93 | - |
|
| 94 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['CSS']; |
|
| 24 | + |
|
| 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_OPEN_CURLY_BRACKET]; |
|
| 34 | + |
|
| 35 | + }//end register() |
|
| 36 | + |
|
| 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 | + $start = ($stackPtr + 1); |
|
| 51 | + $end = ($tokens[$stackPtr]['bracket_closer'] - 1); |
|
| 52 | + $inStyle = null; |
|
| 53 | + |
|
| 54 | + for ($i = $start; $i <= $end; $i++) { |
|
| 55 | + // Skip nested definitions as they are checked individually. |
|
| 56 | + if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) { |
|
| 57 | + $i = $tokens[$i]['bracket_closer']; |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + if ($tokens[$i]['code'] === T_STYLE) { |
|
| 62 | + $inStyle = $tokens[$i]['content']; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if ($tokens[$i]['code'] === T_SEMICOLON) { |
|
| 66 | + $inStyle = null; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ($inStyle === 'progid') { |
|
| 70 | + // Special case for IE filters. |
|
| 71 | + continue; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ($tokens[$i]['code'] === T_STYLE |
|
| 75 | + || ($inStyle !== null |
|
| 76 | + && $tokens[$i]['code'] === T_STRING) |
|
| 77 | + ) { |
|
| 78 | + $expected = strtolower($tokens[$i]['content']); |
|
| 79 | + if ($expected !== $tokens[$i]['content']) { |
|
| 80 | + $error = 'Style definitions must be lowercase; expected %s but found %s'; |
|
| 81 | + $data = [ |
|
| 82 | + $expected, |
|
| 83 | + $tokens[$i]['content'], |
|
| 84 | + ]; |
|
| 85 | + |
|
| 86 | + $fix = $phpcsFile->addFixableError($error, $i, 'FoundUpper', $data); |
|
| 87 | + if ($fix === true) { |
|
| 88 | + $phpcsFile->fixer->replaceToken($i, $expected); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + }//end for |
|
| 93 | + |
|
| 94 | + }//end process() |
|
| 95 | 95 | |
| 96 | 96 | |
| 97 | 97 | }//end class |
@@ -75,7 +75,7 @@ |
||
| 75 | 75 | } else { |
| 76 | 76 | $styleNames[$name] = $next; |
| 77 | 77 | } |
| 78 | - } while ($next !== false); |
|
| 78 | + }while ($next !== false); |
|
| 79 | 79 | |
| 80 | 80 | }//end process() |
| 81 | 81 | |
@@ -15,74 +15,74 @@ |
||
| 15 | 15 | class DuplicateStyleDefinitionSniff implements Sniff |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A list of tokenizers this sniff supports. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - public $supportedTokenizers = ['CSS']; |
|
| 24 | - |
|
| 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_OPEN_CURLY_BRACKET]; |
|
| 34 | - |
|
| 35 | - }//end register() |
|
| 36 | - |
|
| 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 | - |
|
| 51 | - if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
|
| 52 | - // Syntax error or live coding, bow out. |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // Find the content of each style definition name. |
|
| 57 | - $styleNames = []; |
|
| 58 | - |
|
| 59 | - $next = $stackPtr; |
|
| 60 | - $end = $tokens[$stackPtr]['bracket_closer']; |
|
| 61 | - |
|
| 62 | - do { |
|
| 63 | - $next = $phpcsFile->findNext([T_STYLE, T_OPEN_CURLY_BRACKET], ($next + 1), $end); |
|
| 64 | - if ($next === false) { |
|
| 65 | - // Class definition is empty. |
|
| 66 | - break; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) { |
|
| 70 | - $next = $tokens[$next]['bracket_closer']; |
|
| 71 | - continue; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $name = $tokens[$next]['content']; |
|
| 75 | - if (isset($styleNames[$name]) === true) { |
|
| 76 | - $first = $styleNames[$name]; |
|
| 77 | - $error = 'Duplicate style definition found; first defined on line %s'; |
|
| 78 | - $data = [$tokens[$first]['line']]; |
|
| 79 | - $phpcsFile->addError($error, $next, 'Found', $data); |
|
| 80 | - } else { |
|
| 81 | - $styleNames[$name] = $next; |
|
| 82 | - } |
|
| 83 | - } while ($next !== false); |
|
| 84 | - |
|
| 85 | - }//end process() |
|
| 18 | + /** |
|
| 19 | + * A list of tokenizers this sniff supports. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + public $supportedTokenizers = ['CSS']; |
|
| 24 | + |
|
| 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_OPEN_CURLY_BRACKET]; |
|
| 34 | + |
|
| 35 | + }//end register() |
|
| 36 | + |
|
| 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 | + |
|
| 51 | + if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
|
| 52 | + // Syntax error or live coding, bow out. |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // Find the content of each style definition name. |
|
| 57 | + $styleNames = []; |
|
| 58 | + |
|
| 59 | + $next = $stackPtr; |
|
| 60 | + $end = $tokens[$stackPtr]['bracket_closer']; |
|
| 61 | + |
|
| 62 | + do { |
|
| 63 | + $next = $phpcsFile->findNext([T_STYLE, T_OPEN_CURLY_BRACKET], ($next + 1), $end); |
|
| 64 | + if ($next === false) { |
|
| 65 | + // Class definition is empty. |
|
| 66 | + break; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) { |
|
| 70 | + $next = $tokens[$next]['bracket_closer']; |
|
| 71 | + continue; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $name = $tokens[$next]['content']; |
|
| 75 | + if (isset($styleNames[$name]) === true) { |
|
| 76 | + $first = $styleNames[$name]; |
|
| 77 | + $error = 'Duplicate style definition found; first defined on line %s'; |
|
| 78 | + $data = [$tokens[$first]['line']]; |
|
| 79 | + $phpcsFile->addError($error, $next, 'Found', $data); |
|
| 80 | + } else { |
|
| 81 | + $styleNames[$name] = $next; |
|
| 82 | + } |
|
| 83 | + } while ($next !== false); |
|
| 84 | + |
|
| 85 | + }//end process() |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | }//end class |
@@ -16,46 +16,46 @@ |
||
| 16 | 16 | class EmptyClassDefinitionSniff implements Sniff |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of tokenizers this sniff supports. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - public $supportedTokenizers = ['CSS']; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Returns the token types that this sniff is interested in. |
|
| 29 | - * |
|
| 30 | - * @return int[] |
|
| 31 | - */ |
|
| 32 | - public function register() |
|
| 33 | - { |
|
| 34 | - return [T_OPEN_CURLY_BRACKET]; |
|
| 35 | - |
|
| 36 | - }//end register() |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Processes the tokens that this sniff is interested in. |
|
| 41 | - * |
|
| 42 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | - * @param int $stackPtr The position in the stack where |
|
| 44 | - * the token was found. |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function process(File $phpcsFile, $stackPtr) |
|
| 49 | - { |
|
| 50 | - $tokens = $phpcsFile->getTokens(); |
|
| 51 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 52 | - |
|
| 53 | - if ($next === false || $tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 54 | - $error = 'Class definition is empty'; |
|
| 55 | - $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - }//end process() |
|
| 19 | + /** |
|
| 20 | + * A list of tokenizers this sniff supports. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + public $supportedTokenizers = ['CSS']; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Returns the token types that this sniff is interested in. |
|
| 29 | + * |
|
| 30 | + * @return int[] |
|
| 31 | + */ |
|
| 32 | + public function register() |
|
| 33 | + { |
|
| 34 | + return [T_OPEN_CURLY_BRACKET]; |
|
| 35 | + |
|
| 36 | + }//end register() |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Processes the tokens that this sniff is interested in. |
|
| 41 | + * |
|
| 42 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. |
|
| 43 | + * @param int $stackPtr The position in the stack where |
|
| 44 | + * the token was found. |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function process(File $phpcsFile, $stackPtr) |
|
| 49 | + { |
|
| 50 | + $tokens = $phpcsFile->getTokens(); |
|
| 51 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
| 52 | + |
|
| 53 | + if ($next === false || $tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 54 | + $error = 'Class definition is empty'; |
|
| 55 | + $phpcsFile->addError($error, $stackPtr, 'Found'); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + }//end process() |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | 61 | }//end class |
@@ -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,114 +16,114 @@ |
||
| 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_FUNCTION, |
|
| 28 | - T_CLASS, |
|
| 29 | - T_INTERFACE, |
|
| 30 | - ]; |
|
| 31 | - |
|
| 32 | - }//end register() |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Processes this test, when one of its tokens is encountered. |
|
| 37 | - * |
|
| 38 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 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 | - |
|
| 48 | - if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 49 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
| 50 | - |
|
| 51 | - // Abstract methods do not require a closing comment. |
|
| 52 | - if ($methodProps['is_abstract'] === true) { |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // If this function is in an interface then we don't require |
|
| 57 | - // a closing comment. |
|
| 58 | - if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 63 | - $error = 'Possible parse error: non-abstract method defined as abstract'; |
|
| 64 | - $phpcsFile->addWarning($error, $stackPtr, 'Abstract'); |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $decName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 69 | - $comment = '//end '.$decName.'()'; |
|
| 70 | - } else if ($tokens[$stackPtr]['code'] === T_CLASS) { |
|
| 71 | - $comment = '//end class'; |
|
| 72 | - } else { |
|
| 73 | - $comment = '//end interface'; |
|
| 74 | - }//end if |
|
| 75 | - |
|
| 76 | - if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 77 | - $error = 'Possible parse error: %s missing opening or closing brace'; |
|
| 78 | - $data = [$tokens[$stackPtr]['content']]; |
|
| 79 | - $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
| 80 | - return; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - $closingBracket = $tokens[$stackPtr]['scope_closer']; |
|
| 84 | - |
|
| 85 | - if ($closingBracket === null) { |
|
| 86 | - // Possible inline structure. Other tests will handle it. |
|
| 87 | - return; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $data = [$comment]; |
|
| 91 | - if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { |
|
| 92 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); |
|
| 93 | - if (rtrim($tokens[$next]['content']) === $comment) { |
|
| 94 | - // The comment isn't really missing; it is just in the wrong place. |
|
| 95 | - $fix = $phpcsFile->addFixableError('Expected %s directly after closing brace', $closingBracket, 'Misplaced', $data); |
|
| 96 | - if ($fix === true) { |
|
| 97 | - $phpcsFile->fixer->beginChangeset(); |
|
| 98 | - for ($i = ($closingBracket + 1); $i < $next; $i++) { |
|
| 99 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Just in case, because indentation fixes can add indents onto |
|
| 103 | - // these comments and cause us to be unable to fix them. |
|
| 104 | - $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar); |
|
| 105 | - $phpcsFile->fixer->endChangeset(); |
|
| 106 | - } |
|
| 107 | - } else { |
|
| 108 | - $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Missing', $data); |
|
| 109 | - if ($fix === true) { |
|
| 110 | - $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return; |
|
| 115 | - }//end if |
|
| 116 | - |
|
| 117 | - if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) { |
|
| 118 | - $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Incorrect', $data); |
|
| 119 | - if ($fix === true) { |
|
| 120 | - $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - }//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_FUNCTION, |
|
| 28 | + T_CLASS, |
|
| 29 | + T_INTERFACE, |
|
| 30 | + ]; |
|
| 31 | + |
|
| 32 | + }//end register() |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Processes this test, when one of its tokens is encountered. |
|
| 37 | + * |
|
| 38 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
| 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 | + |
|
| 48 | + if ($tokens[$stackPtr]['code'] === T_FUNCTION) { |
|
| 49 | + $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
| 50 | + |
|
| 51 | + // Abstract methods do not require a closing comment. |
|
| 52 | + if ($methodProps['is_abstract'] === true) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // If this function is in an interface then we don't require |
|
| 57 | + // a closing comment. |
|
| 58 | + if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 63 | + $error = 'Possible parse error: non-abstract method defined as abstract'; |
|
| 64 | + $phpcsFile->addWarning($error, $stackPtr, 'Abstract'); |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $decName = $phpcsFile->getDeclarationName($stackPtr); |
|
| 69 | + $comment = '//end '.$decName.'()'; |
|
| 70 | + } else if ($tokens[$stackPtr]['code'] === T_CLASS) { |
|
| 71 | + $comment = '//end class'; |
|
| 72 | + } else { |
|
| 73 | + $comment = '//end interface'; |
|
| 74 | + }//end if |
|
| 75 | + |
|
| 76 | + if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
| 77 | + $error = 'Possible parse error: %s missing opening or closing brace'; |
|
| 78 | + $data = [$tokens[$stackPtr]['content']]; |
|
| 79 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data); |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + $closingBracket = $tokens[$stackPtr]['scope_closer']; |
|
| 84 | + |
|
| 85 | + if ($closingBracket === null) { |
|
| 86 | + // Possible inline structure. Other tests will handle it. |
|
| 87 | + return; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $data = [$comment]; |
|
| 91 | + if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) { |
|
| 92 | + $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true); |
|
| 93 | + if (rtrim($tokens[$next]['content']) === $comment) { |
|
| 94 | + // The comment isn't really missing; it is just in the wrong place. |
|
| 95 | + $fix = $phpcsFile->addFixableError('Expected %s directly after closing brace', $closingBracket, 'Misplaced', $data); |
|
| 96 | + if ($fix === true) { |
|
| 97 | + $phpcsFile->fixer->beginChangeset(); |
|
| 98 | + for ($i = ($closingBracket + 1); $i < $next; $i++) { |
|
| 99 | + $phpcsFile->fixer->replaceToken($i, ''); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Just in case, because indentation fixes can add indents onto |
|
| 103 | + // these comments and cause us to be unable to fix them. |
|
| 104 | + $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar); |
|
| 105 | + $phpcsFile->fixer->endChangeset(); |
|
| 106 | + } |
|
| 107 | + } else { |
|
| 108 | + $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Missing', $data); |
|
| 109 | + if ($fix === true) { |
|
| 110 | + $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return; |
|
| 115 | + }//end if |
|
| 116 | + |
|
| 117 | + if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) { |
|
| 118 | + $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Incorrect', $data); |
|
| 119 | + if ($fix === true) { |
|
| 120 | + $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + }//end process() |
|
| 127 | 127 | |
| 128 | 128 | |
| 129 | 129 | }//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,143 +16,143 @@ |
||
| 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_FUNCTION => true, |
|
| 68 | - T_PUBLIC => true, |
|
| 69 | - T_PRIVATE => true, |
|
| 70 | - T_PROTECTED => true, |
|
| 71 | - T_STATIC => true, |
|
| 72 | - T_ABSTRACT => true, |
|
| 73 | - T_PROPERTY => true, |
|
| 74 | - T_OBJECT => true, |
|
| 75 | - T_PROTOTYPE => true, |
|
| 76 | - T_VAR => true, |
|
| 77 | - ]; |
|
| 78 | - |
|
| 79 | - if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) { |
|
| 80 | - // Could be a file comment. |
|
| 81 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 82 | - if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) { |
|
| 83 | - return; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // There must be one space after each star (unless it is an empty comment line) |
|
| 88 | - // and all the stars must be aligned correctly. |
|
| 89 | - $requiredColumn = ($tokens[$stackPtr]['column'] + 1); |
|
| 90 | - $endComment = $tokens[$stackPtr]['comment_closer']; |
|
| 91 | - for ($i = ($stackPtr + 1); $i <= $endComment; $i++) { |
|
| 92 | - if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR |
|
| 93 | - && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 94 | - ) { |
|
| 95 | - continue; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) { |
|
| 99 | - if (trim($tokens[$i]['content']) === '') { |
|
| 100 | - // Don't process an unfinished docblock close tag during live coding. |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // Can't process the close tag if it is not the first thing on the line. |
|
| 105 | - $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true); |
|
| 106 | - if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if ($tokens[$i]['column'] !== $requiredColumn) { |
|
| 112 | - $error = 'Expected %s space(s) before asterisk; %s found'; |
|
| 113 | - $data = [ |
|
| 114 | - ($requiredColumn - 1), |
|
| 115 | - ($tokens[$i]['column'] - 1), |
|
| 116 | - ]; |
|
| 117 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 118 | - if ($fix === true) { |
|
| 119 | - $padding = str_repeat(' ', ($requiredColumn - 1)); |
|
| 120 | - if ($tokens[$i]['column'] === 1) { |
|
| 121 | - $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 122 | - } else { |
|
| 123 | - $phpcsFile->fixer->replaceToken(($i - 1), $padding); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { |
|
| 129 | - continue; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) { |
|
| 133 | - // Line is empty. |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) { |
|
| 138 | - $error = 'Expected 1 space after asterisk; 0 found'; |
|
| 139 | - $fix = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar'); |
|
| 140 | - if ($fix === true) { |
|
| 141 | - $phpcsFile->fixer->addContent($i, ' '); |
|
| 142 | - } |
|
| 143 | - } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG |
|
| 144 | - && $tokens[($i + 1)]['content'] !== ' ' |
|
| 145 | - ) { |
|
| 146 | - $error = 'Expected 1 space after asterisk; %s found'; |
|
| 147 | - $data = [$tokens[($i + 1)]['length']]; |
|
| 148 | - $fix = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data); |
|
| 149 | - if ($fix === true) { |
|
| 150 | - $phpcsFile->fixer->replaceToken(($i + 1), ' '); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - }//end for |
|
| 154 | - |
|
| 155 | - }//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_FUNCTION => true, |
|
| 68 | + T_PUBLIC => true, |
|
| 69 | + T_PRIVATE => true, |
|
| 70 | + T_PROTECTED => true, |
|
| 71 | + T_STATIC => true, |
|
| 72 | + T_ABSTRACT => true, |
|
| 73 | + T_PROPERTY => true, |
|
| 74 | + T_OBJECT => true, |
|
| 75 | + T_PROTOTYPE => true, |
|
| 76 | + T_VAR => true, |
|
| 77 | + ]; |
|
| 78 | + |
|
| 79 | + if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) { |
|
| 80 | + // Could be a file comment. |
|
| 81 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
| 82 | + if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) { |
|
| 83 | + return; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // There must be one space after each star (unless it is an empty comment line) |
|
| 88 | + // and all the stars must be aligned correctly. |
|
| 89 | + $requiredColumn = ($tokens[$stackPtr]['column'] + 1); |
|
| 90 | + $endComment = $tokens[$stackPtr]['comment_closer']; |
|
| 91 | + for ($i = ($stackPtr + 1); $i <= $endComment; $i++) { |
|
| 92 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR |
|
| 93 | + && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
|
| 94 | + ) { |
|
| 95 | + continue; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) { |
|
| 99 | + if (trim($tokens[$i]['content']) === '') { |
|
| 100 | + // Don't process an unfinished docblock close tag during live coding. |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // Can't process the close tag if it is not the first thing on the line. |
|
| 105 | + $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true); |
|
| 106 | + if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if ($tokens[$i]['column'] !== $requiredColumn) { |
|
| 112 | + $error = 'Expected %s space(s) before asterisk; %s found'; |
|
| 113 | + $data = [ |
|
| 114 | + ($requiredColumn - 1), |
|
| 115 | + ($tokens[$i]['column'] - 1), |
|
| 116 | + ]; |
|
| 117 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
|
| 118 | + if ($fix === true) { |
|
| 119 | + $padding = str_repeat(' ', ($requiredColumn - 1)); |
|
| 120 | + if ($tokens[$i]['column'] === 1) { |
|
| 121 | + $phpcsFile->fixer->addContentBefore($i, $padding); |
|
| 122 | + } else { |
|
| 123 | + $phpcsFile->fixer->replaceToken(($i - 1), $padding); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { |
|
| 129 | + continue; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) { |
|
| 133 | + // Line is empty. |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) { |
|
| 138 | + $error = 'Expected 1 space after asterisk; 0 found'; |
|
| 139 | + $fix = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar'); |
|
| 140 | + if ($fix === true) { |
|
| 141 | + $phpcsFile->fixer->addContent($i, ' '); |
|
| 142 | + } |
|
| 143 | + } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG |
|
| 144 | + && $tokens[($i + 1)]['content'] !== ' ' |
|
| 145 | + ) { |
|
| 146 | + $error = 'Expected 1 space after asterisk; %s found'; |
|
| 147 | + $data = [$tokens[($i + 1)]['length']]; |
|
| 148 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data); |
|
| 149 | + if ($fix === true) { |
|
| 150 | + $phpcsFile->fixer->replaceToken(($i + 1), ' '); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + }//end for |
|
| 154 | + |
|
| 155 | + }//end process() |
|
| 156 | 156 | |
| 157 | 157 | |
| 158 | 158 | }//end class |