Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * A class to find T_VARIABLE tokens.
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
13
 * @version   CVS: $Id: AbstractVariableSniff.php,v 1.15 2008/12/01 05:41:28 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
18
    $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
19
    throw new PHP_CodeSniffer_Exception($error);
20
}
21
 
22
/**
23
 * A class to find T_VARIABLE tokens.
24
 *
25
 * This class can distingush between normal T_VARIABLE tokens, and those tokens
26
 * that represent class members. If a class member is encountered, then then
27
 * processMemberVar method is called so the extending class can process it. If
28
 * the token is found to be a normal T_VARIABLE token, then processVariable is
29
 * called.
30
 *
31
 * @category  PHP
32
 * @package   PHP_CodeSniffer
33
 * @author    Greg Sherwood <gsherwood@squiz.net>
34
 * @author    Marc McIntyre <mmcintyre@squiz.net>
35
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
36
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
37
 * @version   Release: 1.2.0RC1
38
 * @link      http://pear.php.net/package/PHP_CodeSniffer
39
 */
40
abstract class PHP_CodeSniffer_Standards_AbstractVariableSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
41
{
42
 
43
    /**
44
     * The end token of the current function that we are in.
45
     *
46
     * @var int
47
     */
48
    private $_endFunction = -1;
49
 
50
    /**
51
     * true if a function is currently open.
52
     *
53
     * @var boolean
54
     */
55
    private $_functionOpen = false;
56
 
57
    /**
58
     * The current PHP_CodeSniffer file that we are processing.
59
     *
60
     * @var PHP_CodeSniffer_File
61
     */
62
    protected $currentFile = null;
63
 
64
 
65
    /**
66
     * Constructs an AbstractVariableTest.
67
     */
68
    public function __construct()
69
    {
70
        $listen = array(
71
                   T_CLASS,
72
                   T_INTERFACE,
73
                  );
74
 
75
        $scopes = array(
76
                   T_FUNCTION,
77
                   T_VARIABLE,
78
                   T_DOUBLE_QUOTED_STRING,
79
                  );
80
 
81
        parent::__construct($listen, $scopes, true);
82
 
83
    }//end __construct()
84
 
85
 
86
    /**
87
     * Processes the token in the specified PHP_CodeSniffer_File.
88
     *
89
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
90
     *                                        token was found.
91
     * @param int                  $stackPtr  The position where the token was found.
92
     * @param array                $currScope The current scope opener token.
93
     *
94
     * @return void
95
     */
96
    protected final function processTokenWithinScope(
97
        PHP_CodeSniffer_File $phpcsFile,
98
        $stackPtr,
99
        $currScope
100
    ) {
101
        if ($this->currentFile !== $phpcsFile) {
102
            $this->currentFile   = $phpcsFile;
103
            $this->_functionOpen = false;
104
            $this->_endFunction  = -1;
105
        }
106
 
107
        $tokens = $phpcsFile->getTokens();
108
 
109
        if ($stackPtr > $this->_endFunction) {
110
            $this->_functionOpen = false;
111
        }
112
 
113
        if ($tokens[$stackPtr]['code'] === T_FUNCTION
114
            && $this->_functionOpen === false
115
        ) {
116
            $this->_functionOpen = true;
117
 
118
            $methodProps = $phpcsFile->getMethodProperties($stackPtr);
119
 
120
            // If the function is abstract, or is in an interface,
121
            // then set the end of the function to it's closing semicolon.
122
            if ($methodProps['is_abstract'] === true
123
                || $tokens[$currScope]['code'] === T_INTERFACE
124
            ) {
125
                $this->_endFunction
126
                    = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr);
127
            } else {
128
                if (isset($tokens[$stackPtr]['scope_closer']) === false) {
129
                    $error = 'Possible parse error: non-abstract method defined as abstract';
130
                    $phpcsFile->addWarning($error, $stackPtr);
131
                    return;
132
                }
133
 
134
                $this->_endFunction = $tokens[$stackPtr]['scope_closer'];
135
            }
136
 
137
        }
138
 
139
        if ($this->_functionOpen === true) {
140
            if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
141
                $this->processVariable($phpcsFile, $stackPtr);
142
            } else if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
143
                // Check to see if this string has a variable in it.
144
                $pattern = '|[^\\\]\$[a-zA-Z0-9_]+|';
145
                if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
146
                    $this->processVariableInString($phpcsFile, $stackPtr);
147
                }
148
            }
149
 
150
            return;
151
        } else {
152
            // What if we assign a member variable to another?
153
            // ie. private $_count = $this->_otherCount + 1;.
154
            $this->processMemberVar($phpcsFile, $stackPtr);
155
        }
156
 
157
    }//end processTokenWithinScope()
158
 
159
 
160
    /**
161
     * Processes the token outside the scope in the file.
162
     *
163
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
164
     *                                        token was found.
165
     * @param int                  $stackPtr  The position where the token was found.
166
     *
167
     * @return void
168
     */
169
    protected final function processTokenOutsideScope(
170
        PHP_CodeSniffer_File $phpcsFile,
171
        $stackPtr
172
    ) {
173
        $tokens = $phpcsFile->getTokens();
174
        // These variables are not member vars.
175
        if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
176
            $this->processVariable($phpcsFile, $stackPtr);
177
        } else {
178
            $this->processVariableInString($phpcsFile, $stackPtr);
179
        }
180
 
181
    }//end processTokenOutsideScope()
182
 
183
 
184
    /**
185
     * Called to process class member vars.
186
     *
187
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
188
     *                                        token was found.
189
     * @param int                  $stackPtr  The position where the token was found.
190
     *
191
     * @return void
192
     */
193
    abstract protected function processMemberVar(
194
        PHP_CodeSniffer_File $phpcsFile,
195
        $stackPtr
196
    );
197
 
198
 
199
    /**
200
     * Called to process normal member vars.
201
     *
202
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
203
     *                                        token was found.
204
     * @param int                  $stackPtr  The position where the token was found.
205
     *
206
     * @return void
207
     */
208
    abstract protected function processVariable(
209
        PHP_CodeSniffer_File $phpcsFile,
210
        $stackPtr
211
    );
212
 
213
 
214
    /**
215
     * Called to process variables found in duoble quoted strings.
216
     *
217
     * Note that there may be more than one variable in the string, which will
218
     * result only in one call for the string.
219
     *
220
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
221
     *                                        token was found.
222
     * @param int                  $stackPtr  The position where the double quoted
223
     *                                        string was found.
224
     *
225
     * @return void
226
     */
227
    abstract protected function processVariableInString(
228
        PHP_CodeSniffer_File
229
        $phpcsFile,
230
        $stackPtr
231
    );
232
 
233
 
234
}//end class
235
 
236
?>