summaryrefslogtreecommitdiff
path: root/tools/PHP_CodeSniffer/CodeSniffer/Standards/RoundCube/Sniffs/ControlStructures/DisallowPEARIfElseElseifSniff.php
blob: 377b21d46aef08ac7170243e82e4e3df4c5bcc1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
 * PHP_CodeSniffer tokenises PHP code and detects violations of a
 * defined set of coding standards.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Till Klampaeckel <till@php.net>
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
 * @version   CVS: $Id: $
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */

require_once 'PHP/CodeSniffer/Sniff.php';

/**
 * This sniff prohibits PEAR-style, if/else/elseif
 *
 * An example of the PEAR-style is:
 *
 * <code>
 *  if (...) {
 *  ...
 *  } elseif (...) {
 *  ...
 *  } else {
 *  ...
 *  }
 * </code>
 * 
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Till Klampaeckel <till@php.net>
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @version   Release: @package_version@
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
class RoundCube_Sniffs_ControlStructures_DisallowPEARIfElseElseifSniff implements PHP_CodeSniffer_Sniff
{


    /**
     * Returns the token types that this sniff is interested in.
     *
     * @return array()
     */
    public function register()
    {
        return array(T_ELSE,T_ELSEIF);

    }//end register()


    /**
     * Processes the tokens that this sniff is interested in.
     *
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
     * @param int                  $stackPtr  The position in the stack where
     *                                        the token was found.
     *
     * @return void
     */
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $count  = 0;
        while(true) {
            $count++;
            if ($tokens[$stackPtr - 1] === "\n") {
                break;
            }
            if ($tokens[$stackPtr - 1] === "\r") {
                break;
            }
            if ($tokens[$stackPtr - 1] === "\r\n") {
                break;
            }
            if ($count > 3) {
                $phpcsFile->addError('} else {/} elseif (...) { not allowed.', $stackPtr);
                return;
            }
        }
        return;
    }//end process()

}//end class