3
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2012 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_AbstractVariableSniff', true) === false) {
17
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
21
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
23
* Checks the naming of variables and member variables.
26
* @package PHP_CodeSniffer
27
* @author Greg Sherwood <gsherwood@squiz.net>
28
* @author Marc McIntyre <mmcintyre@squiz.net>
29
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
30
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31
* @version Release: 1.5.0RC2
32
* @link http://pear.php.net/package/PHP_CodeSniffer
34
class Squiz_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
38
* Tokens to ignore so that we can find a DOUBLE_COLON.
42
private $_ignore = array(
49
* Processes this test, when one of its tokens is encountered.
51
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
* @param int $stackPtr The position of the current token in the
53
* stack passed in $tokens.
57
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59
$tokens = $phpcsFile->getTokens();
60
$varName = ltrim($tokens[$stackPtr]['content'], '$');
62
$phpReservedVars = array(
74
// If it's a php reserved var, then its ok.
75
if (in_array($varName, $phpReservedVars) === true) {
79
$objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
80
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
81
// Check to see if we are using a variable from an object.
82
$var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
83
if ($tokens[$var]['code'] === T_STRING) {
84
$bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
85
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
86
$objVarName = $tokens[$var]['content'];
88
// There is no way for us to know if the var is public or private,
89
// so we have to ignore a leading underscore if there is one and just
90
// check the main part of the variable name.
91
$originalVarName = $objVarName;
92
if (substr($objVarName, 0, 1) === '_') {
93
$objVarName = substr($objVarName, 1);
96
if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
97
$error = 'Variable "%s" is not in valid camel caps format';
98
$data = array($originalVarName);
99
$phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
105
// There is no way for us to know if the var is public or private,
106
// so we have to ignore a leading underscore if there is one and just
107
// check the main part of the variable name.
108
$originalVarName = $varName;
109
if (substr($varName, 0, 1) === '_') {
110
$objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
111
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
112
// The variable lives within a class, and is referenced like
113
// this: MyClass::$_variable, so we don't know its scope.
116
$inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
119
if ($inClass === true) {
120
$varName = substr($varName, 1);
124
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
125
$error = 'Variable "%s" is not in valid camel caps format';
126
$data = array($originalVarName);
127
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
130
}//end processVariable()
134
* Processes class member variables.
136
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
137
* @param int $stackPtr The position of the current token in the
138
* stack passed in $tokens.
142
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
144
$tokens = $phpcsFile->getTokens();
146
$varName = ltrim($tokens[$stackPtr]['content'], '$');
147
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
148
if (empty($memberProps) === true) {
149
// Couldn't get any info about this variable, which
150
// generally means it is invalid or possibly has a parse
151
// error. Any errors will be reported by the core, so
156
$public = ($memberProps['scope'] !== 'private');
157
$errorData = array($varName);
159
if ($public === true) {
160
if (substr($varName, 0, 1) === '_') {
161
$error = '%s member variable "%s" must not contain a leading underscore';
163
ucfirst($memberProps['scope']),
166
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
170
if (substr($varName, 0, 1) !== '_') {
171
$error = 'Private member variable "%s" must contain a leading underscore';
172
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
177
if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
178
$error = 'Variable "%s" is not in valid camel caps format';
179
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData);
182
}//end processMemberVar()
186
* Processes the variable found within a double quoted string.
188
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
189
* @param int $stackPtr The position of the double quoted
194
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
196
$tokens = $phpcsFile->getTokens();
198
$phpReservedVars = array(
209
if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
210
foreach ($matches[1] as $varName) {
211
// If it's a php reserved var, then its ok.
212
if (in_array($varName, $phpReservedVars) === true) {
216
// There is no way for us to know if the var is public or private,
217
// so we have to ignore a leading underscore if there is one and just
218
// check the main part of the variable name.
219
$originalVarName = $varName;
220
if (substr($varName, 0, 1) === '_') {
221
if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
222
$varName = substr($varName, 1);
226
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
227
$varName = $matches[0];
228
$error = 'Variable "%s" is not in valid camel caps format';
229
$data = array($originalVarName);
230
$phpcsFile->addError($error, $stackPtr, 'StringNotCamelCaps', $data);
236
}//end processVariableInString()