3
* A class to find T_VARIABLE tokens.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
16
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
17
$error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
18
throw new PHP_CodeSniffer_Exception($error);
22
* A class to find T_VARIABLE tokens.
24
* This class can distinguish between normal T_VARIABLE tokens, and those tokens
25
* that represent class members. If a class member is encountered, then then
26
* processMemberVar method is called so the extending class can process it. If
27
* the token is found to be a normal T_VARIABLE token, then processVariable is
31
* @package PHP_CodeSniffer
32
* @author Greg Sherwood <gsherwood@squiz.net>
33
* @author Marc McIntyre <mmcintyre@squiz.net>
34
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
35
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
36
* @version Release: 1.5.4
37
* @link http://pear.php.net/package/PHP_CodeSniffer
39
abstract class PHP_CodeSniffer_Standards_AbstractVariableSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
43
* The end token of the current function that we are in.
47
private $_endFunction = -1;
50
* true if a function is currently open.
54
private $_functionOpen = false;
57
* The current PHP_CodeSniffer file that we are processing.
59
* @var PHP_CodeSniffer_File
61
protected $currentFile = null;
65
* Constructs an AbstractVariableTest.
67
public function __construct()
78
T_DOUBLE_QUOTED_STRING,
82
parent::__construct($scopes, $listen, true);
88
* Processes the token in the specified PHP_CodeSniffer_File.
90
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
92
* @param int $stackPtr The position where the token was found.
93
* @param array $currScope The current scope opener token.
97
protected final function processTokenWithinScope(
98
PHP_CodeSniffer_File $phpcsFile,
102
if ($this->currentFile !== $phpcsFile) {
103
$this->currentFile = $phpcsFile;
104
$this->_functionOpen = false;
105
$this->_endFunction = -1;
108
$tokens = $phpcsFile->getTokens();
110
if ($stackPtr > $this->_endFunction) {
111
$this->_functionOpen = false;
114
if ($tokens[$stackPtr]['code'] === T_FUNCTION
115
&& $this->_functionOpen === false
117
$this->_functionOpen = true;
119
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
121
// If the function is abstract, or is in an interface,
122
// then set the end of the function to it's closing semicolon.
123
if ($methodProps['is_abstract'] === true
124
|| $tokens[$currScope]['code'] === T_INTERFACE
127
= $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr);
129
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
130
$error = 'Possible parse error: non-abstract method defined as abstract';
131
$phpcsFile->addWarning($error, $stackPtr);
135
$this->_endFunction = $tokens[$stackPtr]['scope_closer'];
139
if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
140
|| $tokens[$stackPtr]['code'] === T_HEREDOC
142
// Check to see if this string has a variable in it.
143
$pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
144
if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
145
$this->processVariableInString($phpcsFile, $stackPtr);
151
if ($this->_functionOpen === true) {
152
if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
153
$this->processVariable($phpcsFile, $stackPtr);
156
// What if we assign a member variable to another?
157
// ie. private $_count = $this->_otherCount + 1;.
158
$this->processMemberVar($phpcsFile, $stackPtr);
161
}//end processTokenWithinScope()
165
* Processes the token outside the scope in the file.
167
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
169
* @param int $stackPtr The position where the token was found.
173
protected final function processTokenOutsideScope(
174
PHP_CodeSniffer_File $phpcsFile,
177
$tokens = $phpcsFile->getTokens();
178
// These variables are not member vars.
179
if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
180
$this->processVariable($phpcsFile, $stackPtr);
181
} else if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
182
|| $tokens[$stackPtr]['code'] === T_HEREDOC
184
// Check to see if this string has a variable in it.
185
$pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
186
if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
187
$this->processVariableInString($phpcsFile, $stackPtr);
191
}//end processTokenOutsideScope()
195
* Called to process class member vars.
197
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
199
* @param int $stackPtr The position where the token was found.
203
abstract protected function processMemberVar(
204
PHP_CodeSniffer_File $phpcsFile,
210
* Called to process normal member vars.
212
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
214
* @param int $stackPtr The position where the token was found.
218
abstract protected function processVariable(
219
PHP_CodeSniffer_File $phpcsFile,
225
* Called to process variables found in double quoted strings or heredocs.
227
* Note that there may be more than one variable in the string, which will
228
* result only in one call for the string or one call per line for heredocs.
230
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
232
* @param int $stackPtr The position where the double quoted
237
abstract protected function processVariableInString(