@@ -33,48 +33,48 @@ |
||
| 33 | 33 | class ElseOnNewLineSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns an array of tokens this test wants to listen for. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public function register() |
|
| 42 | - { |
|
| 43 | - return array( |
|
| 44 | - T_ELSE, |
|
| 45 | - T_ELSEIF, |
|
| 46 | - ); |
|
| 47 | - }//end register() |
|
| 36 | + /** |
|
| 37 | + * Returns an array of tokens this test wants to listen for. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public function register() |
|
| 42 | + { |
|
| 43 | + return array( |
|
| 44 | + T_ELSE, |
|
| 45 | + T_ELSEIF, |
|
| 46 | + ); |
|
| 47 | + }//end register() |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param File $phpcsFile The current file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param File $phpcsFile The current file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | 62 | |
| 63 | - $else_token = $tokens[$stackPtr]; |
|
| 64 | - $previous_non_blank_token_ptr = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true); |
|
| 63 | + $else_token = $tokens[$stackPtr]; |
|
| 64 | + $previous_non_blank_token_ptr = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true); |
|
| 65 | 65 | |
| 66 | - if (false === $previous_non_blank_token_ptr) { |
|
| 67 | - // else is no preceded with any symbol, but it is not the responsibility of this sniff. |
|
| 68 | - return; |
|
| 69 | - } |
|
| 66 | + if (false === $previous_non_blank_token_ptr) { |
|
| 67 | + // else is no preceded with any symbol, but it is not the responsibility of this sniff. |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $previous_non_blank_token = $tokens[$previous_non_blank_token_ptr]; |
|
| 72 | - if ($previous_non_blank_token['line'] === $else_token['line']) { |
|
| 73 | - $error = '"' . $else_token['content'] . '" should be on a new line.'; |
|
| 74 | - $phpcsFile->addError($error, $stackPtr); |
|
| 75 | - } |
|
| 71 | + $previous_non_blank_token = $tokens[$previous_non_blank_token_ptr]; |
|
| 72 | + if ($previous_non_blank_token['line'] === $else_token['line']) { |
|
| 73 | + $error = '"' . $else_token['content'] . '" should be on a new line.'; |
|
| 74 | + $phpcsFile->addError($error, $stackPtr); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - }//end process() |
|
| 77 | + }//end process() |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | 80 | }//end class |
@@ -34,60 +34,60 @@ |
||
| 34 | 34 | class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Returns an array of tokens this test wants to listen for. |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public function register() |
|
| 43 | - { |
|
| 44 | - return array( |
|
| 45 | - T_OPEN_TAG, |
|
| 46 | - T_CLOSE_TAG |
|
| 47 | - ); |
|
| 37 | + /** |
|
| 38 | + * Returns an array of tokens this test wants to listen for. |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public function register() |
|
| 43 | + { |
|
| 44 | + return array( |
|
| 45 | + T_OPEN_TAG, |
|
| 46 | + T_CLOSE_TAG |
|
| 47 | + ); |
|
| 48 | 48 | |
| 49 | - }//end register() |
|
| 49 | + }//end register() |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Processes this test, when one of its tokens is encountered. |
|
| 54 | - * |
|
| 55 | - * @param File $phpcsFile The current file being scanned. |
|
| 56 | - * @param int $stackPtr The position of the current token |
|
| 57 | - * in the stack passed in $tokens. |
|
| 58 | - * |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public function process(File $phpcsFile, $stackPtr) |
|
| 62 | - { |
|
| 63 | - $tokens = $phpcsFile->getTokens(); |
|
| 52 | + /** |
|
| 53 | + * Processes this test, when one of its tokens is encountered. |
|
| 54 | + * |
|
| 55 | + * @param File $phpcsFile The current file being scanned. |
|
| 56 | + * @param int $stackPtr The position of the current token |
|
| 57 | + * in the stack passed in $tokens. |
|
| 58 | + * |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public function process(File $phpcsFile, $stackPtr) |
|
| 62 | + { |
|
| 63 | + $tokens = $phpcsFile->getTokens(); |
|
| 64 | 64 | |
| 65 | - $php_tag_token = $tokens[$stackPtr]; |
|
| 66 | - $php_tag_code = $php_tag_token['code']; |
|
| 65 | + $php_tag_token = $tokens[$stackPtr]; |
|
| 66 | + $php_tag_code = $php_tag_token['code']; |
|
| 67 | 67 | |
| 68 | - if (T_OPEN_TAG === $php_tag_code) { |
|
| 69 | - // opening php tag should be the first token. |
|
| 70 | - // any whitespace beofre an opening php tag is tokenized |
|
| 71 | - // as T_INLINE_HTML, so no need to check the content of the token. |
|
| 72 | - $isFirst = 0 === $stackPtr; |
|
| 73 | - if ( ! $isFirst) { |
|
| 74 | - $error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.'; |
|
| 75 | - $phpcsFile->addError($error, $stackPtr); |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - // if (T_CLOSE_TAG === $php_tag_code) |
|
| 79 | - // closing php tag should be the last token |
|
| 80 | - // and it must not contain any whitespace. |
|
| 81 | - $php_tag_string = $php_tag_token['content']; |
|
| 82 | - $isLast = count($tokens) - 1 === $stackPtr; |
|
| 83 | - // both of the two closing php tags contains 2 chars exactly. |
|
| 84 | - $containsEndTagOnly = strlen($php_tag_string) > 2; |
|
| 85 | - if ( ! $isLast || ! $containsEndTagOnly ) { |
|
| 86 | - $error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.'; |
|
| 87 | - $phpcsFile->addError($error, $stackPtr); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - }//end process() |
|
| 68 | + if (T_OPEN_TAG === $php_tag_code) { |
|
| 69 | + // opening php tag should be the first token. |
|
| 70 | + // any whitespace beofre an opening php tag is tokenized |
|
| 71 | + // as T_INLINE_HTML, so no need to check the content of the token. |
|
| 72 | + $isFirst = 0 === $stackPtr; |
|
| 73 | + if ( ! $isFirst) { |
|
| 74 | + $error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.'; |
|
| 75 | + $phpcsFile->addError($error, $stackPtr); |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + // if (T_CLOSE_TAG === $php_tag_code) |
|
| 79 | + // closing php tag should be the last token |
|
| 80 | + // and it must not contain any whitespace. |
|
| 81 | + $php_tag_string = $php_tag_token['content']; |
|
| 82 | + $isLast = count($tokens) - 1 === $stackPtr; |
|
| 83 | + // both of the two closing php tags contains 2 chars exactly. |
|
| 84 | + $containsEndTagOnly = strlen($php_tag_string) > 2; |
|
| 85 | + if ( ! $isLast || ! $containsEndTagOnly ) { |
|
| 86 | + $error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.'; |
|
| 87 | + $phpcsFile->addError($error, $stackPtr); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + }//end process() |
|
| 91 | 91 | |
| 92 | 92 | |
| 93 | 93 | }//end class |
@@ -33,41 +33,41 @@ |
||
| 33 | 33 | class LogicalNotSpacingSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Returns an array of tokens this test wants to listen for. |
|
| 38 | - * |
|
| 39 | - * @return array |
|
| 40 | - */ |
|
| 41 | - public function register() |
|
| 42 | - { |
|
| 43 | - return array( |
|
| 44 | - T_BOOLEAN_NOT, |
|
| 45 | - ); |
|
| 46 | - }//end register() |
|
| 36 | + /** |
|
| 37 | + * Returns an array of tokens this test wants to listen for. |
|
| 38 | + * |
|
| 39 | + * @return array |
|
| 40 | + */ |
|
| 41 | + public function register() |
|
| 42 | + { |
|
| 43 | + return array( |
|
| 44 | + T_BOOLEAN_NOT, |
|
| 45 | + ); |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param File $phpcsFile The current file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token |
|
| 54 | - * in the stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param File $phpcsFile The current file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token |
|
| 54 | + * in the stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | 61 | |
| 62 | - $operator_token = $tokens[$stackPtr]; |
|
| 62 | + $operator_token = $tokens[$stackPtr]; |
|
| 63 | 63 | |
| 64 | - $previous_token = $tokens[$stackPtr - 1]; |
|
| 65 | - $next_token = $tokens[$stackPtr + 1]; |
|
| 66 | - if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { |
|
| 67 | - $error = 'Logical operator ! should always be preceded and followed with a whitespace.'; |
|
| 68 | - $phpcsFile->addError($error, $stackPtr); |
|
| 69 | - } |
|
| 70 | - }//end process() |
|
| 64 | + $previous_token = $tokens[$stackPtr - 1]; |
|
| 65 | + $next_token = $tokens[$stackPtr + 1]; |
|
| 66 | + if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { |
|
| 67 | + $error = 'Logical operator ! should always be preceded and followed with a whitespace.'; |
|
| 68 | + $phpcsFile->addError($error, $stackPtr); |
|
| 69 | + } |
|
| 70 | + }//end process() |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | 73 | }//end class |
@@ -33,53 +33,53 @@ |
||
| 33 | 33 | class DisallowSpaceIndentSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * A list of tokenizers this sniff supports. |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - public $supportedTokenizers = array( |
|
| 42 | - 'PHP', |
|
| 43 | - 'JS', |
|
| 44 | - 'CSS', |
|
| 45 | - ); |
|
| 36 | + /** |
|
| 37 | + * A list of tokenizers this sniff supports. |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + public $supportedTokenizers = array( |
|
| 42 | + 'PHP', |
|
| 43 | + 'JS', |
|
| 44 | + 'CSS', |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Returns an array of tokens this test wants to listen for. |
|
| 50 | - * |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public function register() |
|
| 54 | - { |
|
| 55 | - return array(T_WHITESPACE); |
|
| 56 | - }//end register() |
|
| 48 | + /** |
|
| 49 | + * Returns an array of tokens this test wants to listen for. |
|
| 50 | + * |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public function register() |
|
| 54 | + { |
|
| 55 | + return array(T_WHITESPACE); |
|
| 56 | + }//end register() |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Processes this test, when one of its tokens is encountered. |
|
| 61 | - * |
|
| 62 | - * @param File $phpcsFile All the tokens found in the document. |
|
| 63 | - * @param int $stackPtr The position of the current token |
|
| 64 | - * in the stack passed in $tokens. |
|
| 65 | - * |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - public function process(File $phpcsFile, $stackPtr) |
|
| 69 | - { |
|
| 70 | - $tokens = $phpcsFile->getTokens(); |
|
| 59 | + /** |
|
| 60 | + * Processes this test, when one of its tokens is encountered. |
|
| 61 | + * |
|
| 62 | + * @param File $phpcsFile All the tokens found in the document. |
|
| 63 | + * @param int $stackPtr The position of the current token |
|
| 64 | + * in the stack passed in $tokens. |
|
| 65 | + * |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + public function process(File $phpcsFile, $stackPtr) |
|
| 69 | + { |
|
| 70 | + $tokens = $phpcsFile->getTokens(); |
|
| 71 | 71 | |
| 72 | - // Make sure this is whitespace used for indentation. |
|
| 73 | - $line = $tokens[$stackPtr]['line']; |
|
| 74 | - if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) { |
|
| 75 | - return; |
|
| 76 | - } |
|
| 72 | + // Make sure this is whitespace used for indentation. |
|
| 73 | + $line = $tokens[$stackPtr]['line']; |
|
| 74 | + if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - if (strpos($tokens[$stackPtr]['content'], " ") !== false) { |
|
| 79 | - $error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation'; |
|
| 80 | - $phpcsFile->addError($error, $stackPtr, 'SpacesUsedForIndentation'); |
|
| 81 | - } |
|
| 82 | - }//end process() |
|
| 78 | + if (strpos($tokens[$stackPtr]['content'], " ") !== false) { |
|
| 79 | + $error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation'; |
|
| 80 | + $phpcsFile->addError($error, $stackPtr, 'SpacesUsedForIndentation'); |
|
| 81 | + } |
|
| 82 | + }//end process() |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | 85 | }//end class |
@@ -327,137 +327,137 @@ |
||
| 327 | 327 | */ |
| 328 | 328 | class DoubleQuoteUsageSniff extends VariableUsageSniff |
| 329 | 329 | { |
| 330 | - /** |
|
| 331 | - * Returns an array of tokens this test wants to listen for. |
|
| 332 | - * |
|
| 333 | - * @return array |
|
| 334 | - */ |
|
| 335 | - public function register() |
|
| 336 | - { |
|
| 337 | - return array( |
|
| 338 | - T_DOUBLE_QUOTED_STRING, |
|
| 339 | - T_CONSTANT_ENCAPSED_STRING, |
|
| 340 | - ); |
|
| 341 | - }//end register() |
|
| 330 | + /** |
|
| 331 | + * Returns an array of tokens this test wants to listen for. |
|
| 332 | + * |
|
| 333 | + * @return array |
|
| 334 | + */ |
|
| 335 | + public function register() |
|
| 336 | + { |
|
| 337 | + return array( |
|
| 338 | + T_DOUBLE_QUOTED_STRING, |
|
| 339 | + T_CONSTANT_ENCAPSED_STRING, |
|
| 340 | + ); |
|
| 341 | + }//end register() |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * Processes this test, when one of its tokens is encountered. |
|
| 345 | - * |
|
| 346 | - * @param File $phpcsFile The current file being scanned. |
|
| 347 | - * @param int $stackPtr The position of the current token |
|
| 348 | - * in the stack passed in $tokens. |
|
| 349 | - * |
|
| 350 | - * @return void |
|
| 351 | - */ |
|
| 352 | - public function process(File $phpcsFile, $stackPtr) |
|
| 353 | - { |
|
| 354 | - // no variable are in the string from here |
|
| 355 | - $tokens = $phpcsFile->getTokens(); |
|
| 356 | - $qtString = $tokens[$stackPtr]['content']; |
|
| 357 | - // makes sure that it is about a double quote string, |
|
| 358 | - // since variables are not parsed out of double quoted string |
|
| 359 | - $open_qt_str = substr($qtString, 0, 1); |
|
| 343 | + /** |
|
| 344 | + * Processes this test, when one of its tokens is encountered. |
|
| 345 | + * |
|
| 346 | + * @param File $phpcsFile The current file being scanned. |
|
| 347 | + * @param int $stackPtr The position of the current token |
|
| 348 | + * in the stack passed in $tokens. |
|
| 349 | + * |
|
| 350 | + * @return void |
|
| 351 | + */ |
|
| 352 | + public function process(File $phpcsFile, $stackPtr) |
|
| 353 | + { |
|
| 354 | + // no variable are in the string from here |
|
| 355 | + $tokens = $phpcsFile->getTokens(); |
|
| 356 | + $qtString = $tokens[$stackPtr]['content']; |
|
| 357 | + // makes sure that it is about a double quote string, |
|
| 358 | + // since variables are not parsed out of double quoted string |
|
| 359 | + $open_qt_str = substr($qtString, 0, 1); |
|
| 360 | 360 | |
| 361 | - // clean the enclosing quotes |
|
| 362 | - $qtString = substr($qtString, 1, strlen($qtString) - 1 - 1); |
|
| 361 | + // clean the enclosing quotes |
|
| 362 | + $qtString = substr($qtString, 1, strlen($qtString) - 1 - 1); |
|
| 363 | 363 | |
| 364 | - if (0 === strcmp($open_qt_str, '"')) { |
|
| 365 | - $this->processDoubleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 366 | - } else if (0 === strcmp($open_qt_str, "'")) { |
|
| 367 | - $this->processSingleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 368 | - } |
|
| 369 | - }//end process() |
|
| 364 | + if (0 === strcmp($open_qt_str, '"')) { |
|
| 365 | + $this->processDoubleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 366 | + } else if (0 === strcmp($open_qt_str, "'")) { |
|
| 367 | + $this->processSingleQuotedString($phpcsFile, $stackPtr, $qtString); |
|
| 368 | + } |
|
| 369 | + }//end process() |
|
| 370 | 370 | |
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * Processes this test, when the token encountered is a double-quoted string. |
|
| 374 | - * |
|
| 375 | - * @param File $phpcsFile The current file being scanned. |
|
| 376 | - * @param int $stackPtr The position of the current token |
|
| 377 | - * in the stack passed in $tokens. |
|
| 378 | - * @param string $qtString The double-quoted string content, |
|
| 379 | - * i.e. without quotes. |
|
| 380 | - * |
|
| 381 | - * @return void |
|
| 382 | - */ |
|
| 383 | - protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 384 | - { |
|
| 385 | - // so there should be at least a single quote or a special char |
|
| 386 | - // if there are the 2 kinds of quote and no special char, then add a warning |
|
| 387 | - $has_variable = parent::processDoubleQuotedString($phpcsFile, $stackPtr, '"'.$qtString.'"'); |
|
| 388 | - $has_specific_sequence = $this->_hasSpecificSequence($qtString); |
|
| 389 | - $dbl_qt_at = strpos($qtString, '"'); |
|
| 390 | - $smpl_qt_at = strpos($qtString, "'"); |
|
| 391 | - if (false === $has_variable && false === $has_specific_sequence |
|
| 392 | - && false === $smpl_qt_at |
|
| 393 | - ) { |
|
| 394 | - $error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.'; |
|
| 395 | - $phpcsFile->addError($error, $stackPtr); |
|
| 396 | - } else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
| 397 | - && false === $has_variable && false === $has_specific_sequence |
|
| 398 | - ) { |
|
| 399 | - $warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.'; |
|
| 400 | - $phpcsFile->addWarning($warning, $stackPtr); |
|
| 401 | - } |
|
| 402 | - }//end processDoubleQuotedString() |
|
| 372 | + /** |
|
| 373 | + * Processes this test, when the token encountered is a double-quoted string. |
|
| 374 | + * |
|
| 375 | + * @param File $phpcsFile The current file being scanned. |
|
| 376 | + * @param int $stackPtr The position of the current token |
|
| 377 | + * in the stack passed in $tokens. |
|
| 378 | + * @param string $qtString The double-quoted string content, |
|
| 379 | + * i.e. without quotes. |
|
| 380 | + * |
|
| 381 | + * @return void |
|
| 382 | + */ |
|
| 383 | + protected function processDoubleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 384 | + { |
|
| 385 | + // so there should be at least a single quote or a special char |
|
| 386 | + // if there are the 2 kinds of quote and no special char, then add a warning |
|
| 387 | + $has_variable = parent::processDoubleQuotedString($phpcsFile, $stackPtr, '"'.$qtString.'"'); |
|
| 388 | + $has_specific_sequence = $this->_hasSpecificSequence($qtString); |
|
| 389 | + $dbl_qt_at = strpos($qtString, '"'); |
|
| 390 | + $smpl_qt_at = strpos($qtString, "'"); |
|
| 391 | + if (false === $has_variable && false === $has_specific_sequence |
|
| 392 | + && false === $smpl_qt_at |
|
| 393 | + ) { |
|
| 394 | + $error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.'; |
|
| 395 | + $phpcsFile->addError($error, $stackPtr); |
|
| 396 | + } else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
| 397 | + && false === $has_variable && false === $has_specific_sequence |
|
| 398 | + ) { |
|
| 399 | + $warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.'; |
|
| 400 | + $phpcsFile->addWarning($warning, $stackPtr); |
|
| 401 | + } |
|
| 402 | + }//end processDoubleQuotedString() |
|
| 403 | 403 | |
| 404 | 404 | |
| 405 | - /** |
|
| 406 | - * Processes this test, when the token encountered is a single-quoted string. |
|
| 407 | - * |
|
| 408 | - * @param File $phpcsFile The current file being scanned. |
|
| 409 | - * @param int $stackPtr The position of the current token |
|
| 410 | - * in the stack passed in $tokens. |
|
| 411 | - * @param string $qtString The single-quoted string content, |
|
| 412 | - * i.e. without quotes. |
|
| 413 | - * |
|
| 414 | - * @return void |
|
| 415 | - */ |
|
| 416 | - protected function processSingleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 417 | - { |
|
| 418 | - // if there is single quotes without additional double quotes, |
|
| 419 | - // then user is allowed to use double quote to avoid having to |
|
| 420 | - // escape single quotes. Don't add the warning, if an error was |
|
| 421 | - // already added, because a variable was found in a single-quoted |
|
| 422 | - // string. |
|
| 423 | - $has_variable = parent::processSingleQuotedString($phpcsFile, $stackPtr, "'".$qtString."'"); |
|
| 424 | - $dbl_qt_at = strpos($qtString, '"'); |
|
| 425 | - $smpl_qt_at = strpos($qtString, "'"); |
|
| 426 | - if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) { |
|
| 427 | - $warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.'; |
|
| 428 | - $phpcsFile->addWarning($warning, $stackPtr); |
|
| 429 | - } |
|
| 430 | - }//end processSingleQuotedString() |
|
| 405 | + /** |
|
| 406 | + * Processes this test, when the token encountered is a single-quoted string. |
|
| 407 | + * |
|
| 408 | + * @param File $phpcsFile The current file being scanned. |
|
| 409 | + * @param int $stackPtr The position of the current token |
|
| 410 | + * in the stack passed in $tokens. |
|
| 411 | + * @param string $qtString The single-quoted string content, |
|
| 412 | + * i.e. without quotes. |
|
| 413 | + * |
|
| 414 | + * @return void |
|
| 415 | + */ |
|
| 416 | + protected function processSingleQuotedString (File $phpcsFile, $stackPtr, $qtString) |
|
| 417 | + { |
|
| 418 | + // if there is single quotes without additional double quotes, |
|
| 419 | + // then user is allowed to use double quote to avoid having to |
|
| 420 | + // escape single quotes. Don't add the warning, if an error was |
|
| 421 | + // already added, because a variable was found in a single-quoted |
|
| 422 | + // string. |
|
| 423 | + $has_variable = parent::processSingleQuotedString($phpcsFile, $stackPtr, "'".$qtString."'"); |
|
| 424 | + $dbl_qt_at = strpos($qtString, '"'); |
|
| 425 | + $smpl_qt_at = strpos($qtString, "'"); |
|
| 426 | + if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) { |
|
| 427 | + $warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.'; |
|
| 428 | + $phpcsFile->addWarning($warning, $stackPtr); |
|
| 429 | + } |
|
| 430 | + }//end processSingleQuotedString() |
|
| 431 | 431 | |
| 432 | - /** |
|
| 433 | - * Return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 434 | - * in double-quoted strings is found, FALSE otherwise. |
|
| 435 | - * |
|
| 436 | - * @param string $string String in which sequence of special chars will |
|
| 437 | - * be researched. |
|
| 438 | - * |
|
| 439 | - * @return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 440 | - * in double-quoted strings is found, FALSE otherwise. |
|
| 441 | - * |
|
| 442 | - * @link http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double |
|
| 443 | - */ |
|
| 444 | - private function _hasSpecificSequence($string) |
|
| 445 | - { |
|
| 446 | - $hasSpecificSequence = FALSE; |
|
| 447 | - $specialMeaningStrs = array('\n', '\r', '\t', '\v', '\f'); |
|
| 448 | - foreach ($specialMeaningStrs as $splStr) { |
|
| 449 | - if (FALSE !== strpos($string, $splStr)) { |
|
| 450 | - $hasSpecificSequence = TRUE; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - $specialMeaningPtrns = array('\[0-7]{1,3}', '\x[0-9A-Fa-f]{1,2}'); |
|
| 454 | - foreach ($specialMeaningPtrns as $splPtrn) { |
|
| 455 | - if (1 === preg_match("/{$splPtrn}/", $string)) { |
|
| 456 | - $hasSpecificSequence = TRUE; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - return $hasSpecificSequence; |
|
| 460 | - }//end _hasSpecificSequence() |
|
| 432 | + /** |
|
| 433 | + * Return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 434 | + * in double-quoted strings is found, FALSE otherwise. |
|
| 435 | + * |
|
| 436 | + * @param string $string String in which sequence of special chars will |
|
| 437 | + * be researched. |
|
| 438 | + * |
|
| 439 | + * @return TRUE, if a sequence of chars that is parsed in a specific way |
|
| 440 | + * in double-quoted strings is found, FALSE otherwise. |
|
| 441 | + * |
|
| 442 | + * @link http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double |
|
| 443 | + */ |
|
| 444 | + private function _hasSpecificSequence($string) |
|
| 445 | + { |
|
| 446 | + $hasSpecificSequence = FALSE; |
|
| 447 | + $specialMeaningStrs = array('\n', '\r', '\t', '\v', '\f'); |
|
| 448 | + foreach ($specialMeaningStrs as $splStr) { |
|
| 449 | + if (FALSE !== strpos($string, $splStr)) { |
|
| 450 | + $hasSpecificSequence = TRUE; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + $specialMeaningPtrns = array('\[0-7]{1,3}', '\x[0-9A-Fa-f]{1,2}'); |
|
| 454 | + foreach ($specialMeaningPtrns as $splPtrn) { |
|
| 455 | + if (1 === preg_match("/{$splPtrn}/", $string)) { |
|
| 456 | + $hasSpecificSequence = TRUE; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + return $hasSpecificSequence; |
|
| 460 | + }//end _hasSpecificSequence() |
|
| 461 | 461 | |
| 462 | 462 | }//end class |
| 463 | 463 | |
@@ -32,155 +32,155 @@ |
||
| 32 | 32 | |
| 33 | 33 | class InlineCommentSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * @var int Limit defining long comments. |
|
| 37 | - * Long comments count $longCommentLimit or more lines. |
|
| 38 | - */ |
|
| 39 | - public $longCommentLimit = 5; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Returns an array of tokens this test wants to listen for. |
|
| 43 | - * |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function register() |
|
| 47 | - { |
|
| 48 | - return array( |
|
| 49 | - T_COMMENT |
|
| 50 | - ); |
|
| 51 | - }//end register() |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Processes this test, when one of its tokens is encountered. |
|
| 55 | - * |
|
| 56 | - * @param File $phpcsFile The current file being scanned. |
|
| 57 | - * @param int $stackPtr The position of the current token |
|
| 58 | - * in the stack passed in $tokens. |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function process(File $phpcsFile, $stackPtr) |
|
| 63 | - { |
|
| 64 | - $tokens = $phpcsFile->getTokens(); |
|
| 65 | - |
|
| 66 | - // keep testing only if it's about the first comment of the block |
|
| 67 | - $previousCommentPtr = $phpcsFile->findPrevious($tokens[$stackPtr]['code'], $stackPtr - 1); |
|
| 68 | - if ($tokens[$previousCommentPtr]['line'] !== $tokens[$stackPtr]['line'] - 1) { |
|
| 69 | - if (TRUE !== $this->_checkCommentStyle($phpcsFile, $stackPtr)) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $commentLines = $this->_getCommentBlock($phpcsFile, $stackPtr); |
|
| 74 | - |
|
| 75 | - if (count($commentLines) >= $this->longCommentLimit) { |
|
| 76 | - $this->_checkBlankLinesAroundLongComment($phpcsFile, $commentLines); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - }//end process() |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start |
|
| 84 | - * with '//'. |
|
| 85 | - * |
|
| 86 | - * @param File $phpcsFile The current file being scanned. |
|
| 87 | - * @param int $stackPtr The position of the current token |
|
| 88 | - * that has to be a comment. |
|
| 89 | - * |
|
| 90 | - * @return bool TRUE if the content of the token pointed by $stackPtr starts |
|
| 91 | - * with //, FALSE if an error was added to $phpcsFile. |
|
| 92 | - */ |
|
| 93 | - private function _checkCommentStyle(File $phpcsFile, $stackPtr) |
|
| 94 | - { |
|
| 95 | - $tokens = $phpcsFile->getTokens(); |
|
| 96 | - if ($tokens[$stackPtr]['content']{0} === '#') { |
|
| 97 | - $error = 'Perl-style comments are not allowed; use "// Comment" or DocBlock comments instead'; |
|
| 98 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 99 | - return FALSE; |
|
| 100 | - } else if (substr($tokens[$stackPtr]['content'], 0, 2) === '/*' |
|
| 101 | - || $tokens[$stackPtr]['content']{0} === '*' |
|
| 102 | - ) { |
|
| 103 | - $error = 'Multi lines comments are not allowed; use "// Comment" DocBlock comments instead'; |
|
| 104 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 105 | - return FALSE; |
|
| 106 | - } else if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') { |
|
| 107 | - $error = 'Use single line or DocBlock comments within code'; |
|
| 108 | - $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 109 | - return FALSE; |
|
| 110 | - } |
|
| 111 | - return TRUE; |
|
| 112 | - }//_checkCommentStyle() |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Gather into an array all comment lines to which $stackPtr belongs. |
|
| 117 | - * |
|
| 118 | - * @param File $phpcsFile The current file being scanned. |
|
| 119 | - * @param int $stackPtr Pointer to the first comment line. |
|
| 120 | - * |
|
| 121 | - * @return type array Pointers to tokens making up the comment block. |
|
| 122 | - */ |
|
| 123 | - private function _getCommentBlock(File $phpcsFile, $stackPtr) |
|
| 124 | - { |
|
| 125 | - $tokens = $phpcsFile->getTokens(); |
|
| 126 | - $commentLines = array($stackPtr); |
|
| 127 | - $nextComment = $stackPtr; |
|
| 128 | - $lastLine = $tokens[$stackPtr]['line']; |
|
| 129 | - |
|
| 130 | - while (($nextComment = $phpcsFile->findNext($tokens[$stackPtr]['code'], ($nextComment + 1), null, false)) !== false) { |
|
| 131 | - if (($tokens[$nextComment]['line'] - 1) !== $lastLine) { |
|
| 132 | - // Not part of the block. |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $lastLine = $tokens[$nextComment]['line']; |
|
| 137 | - $commentLines[] = $nextComment; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $commentLines; |
|
| 141 | - }//_getCommentBlock() |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
|
| 146 | - * |
|
| 147 | - * @param File $phpcsFile The current file being scanned. |
|
| 148 | - * @param array $commentLines Lines of the comment block being checked. |
|
| 149 | - * |
|
| 150 | - * @return bool TRUE if $commentLines is enclosed with at least a blank line |
|
| 151 | - * before and after, FALSE otherwise. |
|
| 152 | - */ |
|
| 153 | - private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines) |
|
| 154 | - { |
|
| 155 | - $hasBlankLinesAround = TRUE; |
|
| 156 | - $tokens = $phpcsFile->getTokens(); |
|
| 157 | - |
|
| 158 | - // check blank line before the long comment |
|
| 159 | - $firstCommentPtr = reset($commentLines); |
|
| 160 | - $firstPreviousSpacePtr = $firstCommentPtr - 1; |
|
| 161 | - while (T_WHITESPACE === $tokens[$firstPreviousSpacePtr]['code'] && $firstPreviousSpacePtr > 0) { |
|
| 162 | - $firstPreviousSpacePtr--; |
|
| 163 | - } |
|
| 164 | - if ($tokens[$firstPreviousSpacePtr]['line'] >= $tokens[$firstCommentPtr]['line'] - 1) { |
|
| 165 | - $error = "Please add a blank line before comments counting more than {$this->longCommentLimit} lines."; |
|
| 166 | - $phpcsFile->addError($error, $firstCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 167 | - $hasBlankLinesAround = FALSE; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // check blank line after the long comment |
|
| 171 | - $lastCommentPtr = end($commentLines); |
|
| 172 | - $lastNextSpacePtr = $lastCommentPtr + 1; |
|
| 173 | - while (T_WHITESPACE === $tokens[$lastNextSpacePtr]['code'] && $lastNextSpacePtr < count($tokens)) { |
|
| 174 | - $lastNextSpacePtr++; |
|
| 175 | - } |
|
| 176 | - if ($tokens[$lastNextSpacePtr]['line'] <= $tokens[$lastCommentPtr]['line'] + 1) { |
|
| 177 | - $error = "Please add a blank line after comments counting more than {$this->longCommentLimit} lines."; |
|
| 178 | - $phpcsFile->addError($error, $lastCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 179 | - $hasBlankLinesAround = FALSE; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $hasBlankLinesAround; |
|
| 183 | - }//end _checkBlanksAroundLongComment() |
|
| 35 | + /** |
|
| 36 | + * @var int Limit defining long comments. |
|
| 37 | + * Long comments count $longCommentLimit or more lines. |
|
| 38 | + */ |
|
| 39 | + public $longCommentLimit = 5; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Returns an array of tokens this test wants to listen for. |
|
| 43 | + * |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function register() |
|
| 47 | + { |
|
| 48 | + return array( |
|
| 49 | + T_COMMENT |
|
| 50 | + ); |
|
| 51 | + }//end register() |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Processes this test, when one of its tokens is encountered. |
|
| 55 | + * |
|
| 56 | + * @param File $phpcsFile The current file being scanned. |
|
| 57 | + * @param int $stackPtr The position of the current token |
|
| 58 | + * in the stack passed in $tokens. |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function process(File $phpcsFile, $stackPtr) |
|
| 63 | + { |
|
| 64 | + $tokens = $phpcsFile->getTokens(); |
|
| 65 | + |
|
| 66 | + // keep testing only if it's about the first comment of the block |
|
| 67 | + $previousCommentPtr = $phpcsFile->findPrevious($tokens[$stackPtr]['code'], $stackPtr - 1); |
|
| 68 | + if ($tokens[$previousCommentPtr]['line'] !== $tokens[$stackPtr]['line'] - 1) { |
|
| 69 | + if (TRUE !== $this->_checkCommentStyle($phpcsFile, $stackPtr)) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $commentLines = $this->_getCommentBlock($phpcsFile, $stackPtr); |
|
| 74 | + |
|
| 75 | + if (count($commentLines) >= $this->longCommentLimit) { |
|
| 76 | + $this->_checkBlankLinesAroundLongComment($phpcsFile, $commentLines); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + }//end process() |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Add error to $phpcsFile, if comment pointed by $stackPtr doesn't start |
|
| 84 | + * with '//'. |
|
| 85 | + * |
|
| 86 | + * @param File $phpcsFile The current file being scanned. |
|
| 87 | + * @param int $stackPtr The position of the current token |
|
| 88 | + * that has to be a comment. |
|
| 89 | + * |
|
| 90 | + * @return bool TRUE if the content of the token pointed by $stackPtr starts |
|
| 91 | + * with //, FALSE if an error was added to $phpcsFile. |
|
| 92 | + */ |
|
| 93 | + private function _checkCommentStyle(File $phpcsFile, $stackPtr) |
|
| 94 | + { |
|
| 95 | + $tokens = $phpcsFile->getTokens(); |
|
| 96 | + if ($tokens[$stackPtr]['content']{0} === '#') { |
|
| 97 | + $error = 'Perl-style comments are not allowed; use "// Comment" or DocBlock comments instead'; |
|
| 98 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 99 | + return FALSE; |
|
| 100 | + } else if (substr($tokens[$stackPtr]['content'], 0, 2) === '/*' |
|
| 101 | + || $tokens[$stackPtr]['content']{0} === '*' |
|
| 102 | + ) { |
|
| 103 | + $error = 'Multi lines comments are not allowed; use "// Comment" DocBlock comments instead'; |
|
| 104 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 105 | + return FALSE; |
|
| 106 | + } else if (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') { |
|
| 107 | + $error = 'Use single line or DocBlock comments within code'; |
|
| 108 | + $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); |
|
| 109 | + return FALSE; |
|
| 110 | + } |
|
| 111 | + return TRUE; |
|
| 112 | + }//_checkCommentStyle() |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Gather into an array all comment lines to which $stackPtr belongs. |
|
| 117 | + * |
|
| 118 | + * @param File $phpcsFile The current file being scanned. |
|
| 119 | + * @param int $stackPtr Pointer to the first comment line. |
|
| 120 | + * |
|
| 121 | + * @return type array Pointers to tokens making up the comment block. |
|
| 122 | + */ |
|
| 123 | + private function _getCommentBlock(File $phpcsFile, $stackPtr) |
|
| 124 | + { |
|
| 125 | + $tokens = $phpcsFile->getTokens(); |
|
| 126 | + $commentLines = array($stackPtr); |
|
| 127 | + $nextComment = $stackPtr; |
|
| 128 | + $lastLine = $tokens[$stackPtr]['line']; |
|
| 129 | + |
|
| 130 | + while (($nextComment = $phpcsFile->findNext($tokens[$stackPtr]['code'], ($nextComment + 1), null, false)) !== false) { |
|
| 131 | + if (($tokens[$nextComment]['line'] - 1) !== $lastLine) { |
|
| 132 | + // Not part of the block. |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $lastLine = $tokens[$nextComment]['line']; |
|
| 137 | + $commentLines[] = $nextComment; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $commentLines; |
|
| 141 | + }//_getCommentBlock() |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Add errors to $phpcsFile, if $commentLines isn't enclosed with blank lines. |
|
| 146 | + * |
|
| 147 | + * @param File $phpcsFile The current file being scanned. |
|
| 148 | + * @param array $commentLines Lines of the comment block being checked. |
|
| 149 | + * |
|
| 150 | + * @return bool TRUE if $commentLines is enclosed with at least a blank line |
|
| 151 | + * before and after, FALSE otherwise. |
|
| 152 | + */ |
|
| 153 | + private function _checkBlankLinesAroundLongComment(File $phpcsFile, array $commentLines) |
|
| 154 | + { |
|
| 155 | + $hasBlankLinesAround = TRUE; |
|
| 156 | + $tokens = $phpcsFile->getTokens(); |
|
| 157 | + |
|
| 158 | + // check blank line before the long comment |
|
| 159 | + $firstCommentPtr = reset($commentLines); |
|
| 160 | + $firstPreviousSpacePtr = $firstCommentPtr - 1; |
|
| 161 | + while (T_WHITESPACE === $tokens[$firstPreviousSpacePtr]['code'] && $firstPreviousSpacePtr > 0) { |
|
| 162 | + $firstPreviousSpacePtr--; |
|
| 163 | + } |
|
| 164 | + if ($tokens[$firstPreviousSpacePtr]['line'] >= $tokens[$firstCommentPtr]['line'] - 1) { |
|
| 165 | + $error = "Please add a blank line before comments counting more than {$this->longCommentLimit} lines."; |
|
| 166 | + $phpcsFile->addError($error, $firstCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 167 | + $hasBlankLinesAround = FALSE; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // check blank line after the long comment |
|
| 171 | + $lastCommentPtr = end($commentLines); |
|
| 172 | + $lastNextSpacePtr = $lastCommentPtr + 1; |
|
| 173 | + while (T_WHITESPACE === $tokens[$lastNextSpacePtr]['code'] && $lastNextSpacePtr < count($tokens)) { |
|
| 174 | + $lastNextSpacePtr++; |
|
| 175 | + } |
|
| 176 | + if ($tokens[$lastNextSpacePtr]['line'] <= $tokens[$lastCommentPtr]['line'] + 1) { |
|
| 177 | + $error = "Please add a blank line after comments counting more than {$this->longCommentLimit} lines."; |
|
| 178 | + $phpcsFile->addError($error, $lastCommentPtr, 'LongCommentWithoutSpacing'); |
|
| 179 | + $hasBlankLinesAround = FALSE; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $hasBlankLinesAround; |
|
| 183 | + }//end _checkBlanksAroundLongComment() |
|
| 184 | 184 | |
| 185 | 185 | }//end class |
| 186 | 186 | |
@@ -33,47 +33,47 @@ |
||
| 33 | 33 | class LogicalOperatorAndSniff implements Sniff |
| 34 | 34 | { |
| 35 | 35 | /** |
| 36 | - * Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_LOGICAL_AND, |
|
| 44 | - ); |
|
| 36 | + * Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_LOGICAL_AND, |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - }//end register() |
|
| 46 | + }//end register() |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Processes this test, when one of its tokens is encountered. |
|
| 51 | - * |
|
| 52 | - * @param File $phpcsFile The current file being scanned. |
|
| 53 | - * @param int $stackPtr The position of the current token |
|
| 54 | - * in the stack passed in $tokens. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - public function process(File $phpcsFile, $stackPtr) |
|
| 59 | - { |
|
| 60 | - $tokens = $phpcsFile->getTokens(); |
|
| 49 | + /** |
|
| 50 | + * Processes this test, when one of its tokens is encountered. |
|
| 51 | + * |
|
| 52 | + * @param File $phpcsFile The current file being scanned. |
|
| 53 | + * @param int $stackPtr The position of the current token |
|
| 54 | + * in the stack passed in $tokens. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + public function process(File $phpcsFile, $stackPtr) |
|
| 59 | + { |
|
| 60 | + $tokens = $phpcsFile->getTokens(); |
|
| 61 | 61 | |
| 62 | - $operator_token = $tokens[$stackPtr]; |
|
| 63 | - $operator_string = $operator_token['content']; |
|
| 64 | - $operator_code = $operator_token['code']; |
|
| 62 | + $operator_token = $tokens[$stackPtr]; |
|
| 63 | + $operator_string = $operator_token['content']; |
|
| 64 | + $operator_code = $operator_token['code']; |
|
| 65 | 65 | |
| 66 | - if ($operator_string !== strtoupper($operator_string)) { |
|
| 67 | - $error_message = 'Logical operator should be in upper case;' |
|
| 68 | - . ' use "' . strtoupper($operator_string) |
|
| 69 | - . '" instead of "' . $operator_string . '"'; |
|
| 70 | - $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator'); |
|
| 71 | - } |
|
| 66 | + if ($operator_string !== strtoupper($operator_string)) { |
|
| 67 | + $error_message = 'Logical operator should be in upper case;' |
|
| 68 | + . ' use "' . strtoupper($operator_string) |
|
| 69 | + . '" instead of "' . $operator_string . '"'; |
|
| 70 | + $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator'); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; |
|
| 74 | - $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator'); |
|
| 73 | + $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; |
|
| 74 | + $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator'); |
|
| 75 | 75 | |
| 76 | - }//end process() |
|
| 76 | + }//end process() |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | 79 | }//end class |
@@ -33,47 +33,47 @@ |
||
| 33 | 33 | |
| 34 | 34 | class StrictComparisonOperatorSniff implements Sniff |
| 35 | 35 | { |
| 36 | - private static $_replacements = array( |
|
| 37 | - T_IS_EQUAL => '===', |
|
| 38 | - T_IS_NOT_EQUAL => '!==', |
|
| 39 | - ); |
|
| 36 | + private static $_replacements = array( |
|
| 37 | + T_IS_EQUAL => '===', |
|
| 38 | + T_IS_NOT_EQUAL => '!==', |
|
| 39 | + ); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Returns an array of tokens this test wants to listen for. |
|
| 43 | - * |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function register() |
|
| 47 | - { |
|
| 48 | - return array( |
|
| 49 | - T_IS_EQUAL, |
|
| 50 | - T_IS_NOT_EQUAL, |
|
| 51 | - ); |
|
| 52 | - }//end register() |
|
| 41 | + /** |
|
| 42 | + * Returns an array of tokens this test wants to listen for. |
|
| 43 | + * |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function register() |
|
| 47 | + { |
|
| 48 | + return array( |
|
| 49 | + T_IS_EQUAL, |
|
| 50 | + T_IS_NOT_EQUAL, |
|
| 51 | + ); |
|
| 52 | + }//end register() |
|
| 53 | 53 | |
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Processes this test, when one of its tokens is encountered. |
|
| 57 | - * |
|
| 58 | - * @param File $phpcsFile The current file being scanned. |
|
| 59 | - * @param int $stackPtr The position of the current token |
|
| 60 | - * in the stack passed in $tokens. |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function process(File $phpcsFile, $stackPtr) |
|
| 65 | - { |
|
| 66 | - $tokens = $phpcsFile->getTokens(); |
|
| 55 | + /** |
|
| 56 | + * Processes this test, when one of its tokens is encountered. |
|
| 57 | + * |
|
| 58 | + * @param File $phpcsFile The current file being scanned. |
|
| 59 | + * @param int $stackPtr The position of the current token |
|
| 60 | + * in the stack passed in $tokens. |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function process(File $phpcsFile, $stackPtr) |
|
| 65 | + { |
|
| 66 | + $tokens = $phpcsFile->getTokens(); |
|
| 67 | 67 | |
| 68 | - $operator_token = $tokens[$stackPtr]; |
|
| 69 | - $operator_string = $operator_token['content']; |
|
| 70 | - $operator_code = $operator_token['code']; |
|
| 68 | + $operator_token = $tokens[$stackPtr]; |
|
| 69 | + $operator_string = $operator_token['content']; |
|
| 70 | + $operator_code = $operator_token['code']; |
|
| 71 | 71 | |
| 72 | - $error_message = '"==" and "!=" are prohibited; use "' |
|
| 73 | - . self::$_replacements[$operator_code] . '" instead of "' |
|
| 74 | - . $operator_string . '".'; |
|
| 75 | - $phpcsFile->addError($error_message, $stackPtr, 'NonStrictComparisonUsed'); |
|
| 76 | - }//end process() |
|
| 72 | + $error_message = '"==" and "!=" are prohibited; use "' |
|
| 73 | + . self::$_replacements[$operator_code] . '" instead of "' |
|
| 74 | + . $operator_string . '".'; |
|
| 75 | + $phpcsFile->addError($error_message, $stackPtr, 'NonStrictComparisonUsed'); |
|
| 76 | + }//end process() |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | 79 | }//end class |
@@ -32,51 +32,51 @@ |
||
| 32 | 32 | |
| 33 | 33 | class UppercaseLogicalOperatorOrSniff implements Sniff |
| 34 | 34 | { |
| 35 | - /** |
|
| 36 | - * Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
|
| 37 | - * |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function register() |
|
| 41 | - { |
|
| 42 | - return array( |
|
| 43 | - T_BOOLEAN_OR, |
|
| 44 | - T_LOGICAL_OR, |
|
| 45 | - ); |
|
| 35 | + /** |
|
| 36 | + * Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
|
| 37 | + * |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function register() |
|
| 41 | + { |
|
| 42 | + return array( |
|
| 43 | + T_BOOLEAN_OR, |
|
| 44 | + T_LOGICAL_OR, |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | - }//end register() |
|
| 47 | + }//end register() |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Processes this test, when one of its tokens is encountered. |
|
| 52 | - * |
|
| 53 | - * @param File $phpcsFile The current file being scanned. |
|
| 54 | - * @param int $stackPtr The position of the current token |
|
| 55 | - * in the stack passed in $tokens. |
|
| 56 | - * |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function process(File $phpcsFile, $stackPtr) |
|
| 60 | - { |
|
| 61 | - $tokens = $phpcsFile->getTokens(); |
|
| 50 | + /** |
|
| 51 | + * Processes this test, when one of its tokens is encountered. |
|
| 52 | + * |
|
| 53 | + * @param File $phpcsFile The current file being scanned. |
|
| 54 | + * @param int $stackPtr The position of the current token |
|
| 55 | + * in the stack passed in $tokens. |
|
| 56 | + * |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function process(File $phpcsFile, $stackPtr) |
|
| 60 | + { |
|
| 61 | + $tokens = $phpcsFile->getTokens(); |
|
| 62 | 62 | |
| 63 | - $operator_token = $tokens[$stackPtr]; |
|
| 64 | - $operator_string = $operator_token['content']; |
|
| 65 | - $operator_code = $operator_token['code']; |
|
| 63 | + $operator_token = $tokens[$stackPtr]; |
|
| 64 | + $operator_string = $operator_token['content']; |
|
| 65 | + $operator_code = $operator_token['code']; |
|
| 66 | 66 | |
| 67 | - if ($operator_code == T_BOOLEAN_OR) { |
|
| 68 | - $error_message = 'Logical operator "' . $operator_string |
|
| 69 | - . '" is prohibited; use "OR" instead'; |
|
| 70 | - $phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR'); |
|
| 71 | - } |
|
| 72 | - // it is literal, if it is not symbolic |
|
| 73 | - else if ($operator_string !== strtoupper($operator_string)) { |
|
| 74 | - $error_message = 'Logical operator should be in upper case;' |
|
| 75 | - . ' use "' . strtoupper($operator_string) |
|
| 76 | - . '" instead of "' . $operator_string . '"'; |
|
| 77 | - $phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr'); |
|
| 78 | - } |
|
| 79 | - }//end process() |
|
| 67 | + if ($operator_code == T_BOOLEAN_OR) { |
|
| 68 | + $error_message = 'Logical operator "' . $operator_string |
|
| 69 | + . '" is prohibited; use "OR" instead'; |
|
| 70 | + $phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR'); |
|
| 71 | + } |
|
| 72 | + // it is literal, if it is not symbolic |
|
| 73 | + else if ($operator_string !== strtoupper($operator_string)) { |
|
| 74 | + $error_message = 'Logical operator should be in upper case;' |
|
| 75 | + . ' use "' . strtoupper($operator_string) |
|
| 76 | + . '" instead of "' . $operator_string . '"'; |
|
| 77 | + $phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr'); |
|
| 78 | + } |
|
| 79 | + }//end process() |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | 82 | }//end class |