Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * Generic_Sniffs_ControlStructures_InlineControlStructureSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
34 aurelien 13
 * @version   CVS: $Id: InlineControlStructureSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Generic_Sniffs_ControlStructures_InlineControlStructureSniff.
19
 *
20
 * Verifies that inline control statements are not present.
21
 *
22
 * @category  PHP
23
 * @package   PHP_CodeSniffer
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 * @author    Marc McIntyre <mmcintyre@squiz.net>
26
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
 * @version   Release: 1.2.0RC1
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 */
31
class Generic_Sniffs_ControlStructures_InlineControlStructureSniff implements PHP_CodeSniffer_Sniff
32
{
33
 
34
    /**
35
     * A list of tokenizers this sniff supports.
36
     *
37
     * @var array
38
     */
39
    public $supportedTokenizers = array(
40
                                   'PHP',
41
                                   'JS',
42
                                  );
43
 
44
    /**
45
     * If true, an error will be thrown; otherwise a warning.
46
     *
47
     * @var bool
48
     */
49
    protected $error = true;
50
 
51
    /**
52
     * Returns an array of tokens this test wants to listen for.
53
     *
54
     * @return array
55
     */
56
    public function register()
57
    {
58
        return array(
59
                T_IF,
60
                T_ELSE,
61
                T_FOREACH,
62
                T_WHILE,
63
                T_DO,
64
                T_SWITCH,
65
                T_FOR,
66
               );
67
 
68
    }//end register()
69
 
70
 
71
    /**
72
     * Processes this test, when one of its tokens is encountered.
73
     *
74
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
75
     * @param int                  $stackPtr  The position of the current token in the
76
     *                                        stack passed in $tokens.
77
     *
78
     * @return void
79
     */
80
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
81
    {
82
        $tokens = $phpcsFile->getTokens();
83
 
84
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
85
            // Ignore the ELSE in ELSE IF. We'll process the IF part later.
86
            if (($tokens[$stackPtr]['code'] === T_ELSE) && ($tokens[($stackPtr + 2)]['code'] === T_IF)) {
87
                return;
88
            }
89
 
90
            if ($tokens[$stackPtr]['code'] === T_WHILE) {
91
                // This could be from a DO WHILE, which doesn't have an opening brace.
92
                $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
93
                if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
94
                    $brace = $tokens[$lastContent];
95
                    if (isset($brace['scope_condition']) === true) {
96
                        $condition = $tokens[$brace['scope_condition']];
97
                        if ($condition['code'] === T_DO) {
98
                            return;
99
                        }
100
                    }
101
                }
102
            }
103
 
104
            // This is a control structure without an opening brace,
105
            // so it is an inline statement.
106
            if ($this->error === true) {
107
                $phpcsFile->addError('Inline control structures are not allowed', $stackPtr);
108
            } else {
109
                $phpcsFile->addWarning('Inline control structures are discouraged', $stackPtr);
110
            }
111
 
112
            return;
113
        }//end if
114
 
115
    }//end process()
116
 
117
 
118
}//end class
119
 
120
?>