| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * PHP_CodeSniffer tokenises PHP code and detects violations of a |
|---|
| 4 | * defined set of coding standards. |
|---|
| 5 | * |
|---|
| 6 | * PHP version 5 |
|---|
| 7 | * |
|---|
| 8 | * @category PHP |
|---|
| 9 | * @package PHP_CodeSniffer |
|---|
| 10 | * @author Till Klampaeckel <till@php.net> |
|---|
| 11 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|---|
| 12 | * @version CVS: $Id: $ |
|---|
| 13 | * @link http://pear.php.net/package/PHP_CodeSniffer |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | require_once 'PHP/CodeSniffer/Sniff.php'; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * This sniff prohibits PEAR-style, if/else/elseif |
|---|
| 20 | * |
|---|
| 21 | * An example of the PEAR-style is: |
|---|
| 22 | * |
|---|
| 23 | * <code> |
|---|
| 24 | * if (...) { |
|---|
| 25 | * ... |
|---|
| 26 | * } elseif (...) { |
|---|
| 27 | * ... |
|---|
| 28 | * } else { |
|---|
| 29 | * ... |
|---|
| 30 | * } |
|---|
| 31 | * </code> |
|---|
| 32 | * |
|---|
| 33 | * @category PHP |
|---|
| 34 | * @package PHP_CodeSniffer |
|---|
| 35 | * @author Till Klampaeckel <till@php.net> |
|---|
| 36 | * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
|---|
| 37 | * @version Release: @package_version@ |
|---|
| 38 | * @link http://pear.php.net/package/PHP_CodeSniffer |
|---|
| 39 | */ |
|---|
| 40 | class RoundCube_Sniffs_ControlStructures_DisallowPEARIfElseElseifSniff implements PHP_CodeSniffer_Sniff |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Returns the token types that this sniff is interested in. |
|---|
| 46 | * |
|---|
| 47 | * @return array() |
|---|
| 48 | */ |
|---|
| 49 | public function register() |
|---|
| 50 | { |
|---|
| 51 | return array(T_ELSE,T_ELSEIF); |
|---|
| 52 | |
|---|
| 53 | }//end register() |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Processes the tokens that this sniff is interested in. |
|---|
| 58 | * |
|---|
| 59 | * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. |
|---|
| 60 | * @param int $stackPtr The position in the stack where |
|---|
| 61 | * the token was found. |
|---|
| 62 | * |
|---|
| 63 | * @return void |
|---|
| 64 | */ |
|---|
| 65 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|---|
| 66 | { |
|---|
| 67 | static $_eol = array("\n", "\r", "\n\r"); |
|---|
| 68 | |
|---|
| 69 | $tokens = $phpcsFile->getTokens(); |
|---|
| 70 | $count = 0; |
|---|
| 71 | $error = '} else {/} elseif (...) { not allowed.'; |
|---|
| 72 | |
|---|
| 73 | if ($tokens[$stackPtr - 1]['type'] !== 'T_WHITESPACE') { |
|---|
| 74 | $phpcsFile->addError($error, $stackPtr); |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | if (!in_array($tokens[$stackPtr - 1]['content'], $_eol)) { |
|---|
| 78 | $phpcsFile->addError($error . " - " . var_export($tokens[$stackPtr - 1]['content'], true), $stackPtr); |
|---|
| 79 | return; |
|---|
| 80 | } |
|---|
| 81 | return; |
|---|
| 82 | }//end process() |
|---|
| 83 | |
|---|
| 84 | }//end class |
|---|