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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license http://matrix.squiz.net/developer/tools/php_cs/licence 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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
30
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
31
* @version Release: 1.3.4
32
* @link http://pear.php.net/package/PHP_CodeSniffer
34
class Zend_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
// Either a var name or a function call, so check for bracket.
85
$bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
87
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
88
$objVarName = $tokens[$var]['content'];
90
// There is no way for us to know if the var is public or private,
91
// so we have to ignore a leading underscore if there is one and just
92
// check the main part of the variable name.
93
$originalVarName = $objVarName;
94
if (substr($objVarName, 0, 1) === '_') {
95
$objVarName = substr($objVarName, 1);
98
if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
99
$error = 'Variable "%s" is not in valid camel caps format';
100
$data = array($originalVarName);
101
$phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
102
} else if (preg_match('|\d|', $objVarName)) {
103
$warning = 'Variable "%s" contains numbers but this is discouraged';
104
$data = array($originalVarName);
105
$phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
111
// There is no way for us to know if the var is public or private,
112
// so we have to ignore a leading underscore if there is one and just
113
// check the main part of the variable name.
114
$originalVarName = $varName;
115
if (substr($varName, 0, 1) === '_') {
116
$objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
117
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
118
// The variable lives within a class, and is referenced like
119
// this: MyClass::$_variable, so we don't know its scope.
122
$inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
125
if ($inClass === true) {
126
$varName = substr($varName, 1);
130
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
131
$error = 'Variable "%s" is not in valid camel caps format';
132
$data = array($originalVarName);
133
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
134
} else if (preg_match('|\d|', $varName)) {
135
$warning = 'Variable "%s" contains numbers but this is discouraged';
136
$data = array($originalVarName);
137
$phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
140
}//end processVariable()
144
* Processes class member variables.
146
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
147
* @param int $stackPtr The position of the current token in the
148
* stack passed in $tokens.
152
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
154
$tokens = $phpcsFile->getTokens();
155
$varName = ltrim($tokens[$stackPtr]['content'], '$');
156
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
157
$public = ($memberProps['scope'] === 'public');
159
if ($public === true) {
160
if (substr($varName, 0, 1) === '_') {
161
$error = 'Public member variable "%s" must not contain a leading underscore';
162
$data = array($varName);
163
$phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
167
if (substr($varName, 0, 1) !== '_') {
168
$scope = ucfirst($memberProps['scope']);
169
$error = '%s member variable "%s" must contain a leading underscore';
174
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
179
if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
180
$error = 'Variable "%s" is not in valid camel caps format';
181
$data = array($varName);
182
$phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
183
} else if (preg_match('|\d|', $varName)) {
184
$warning = 'Variable "%s" contains numbers but this is discouraged';
185
$data = array($varName);
186
$phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
189
}//end processMemberVar()
193
* Processes the variable found within a double quoted string.
195
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
196
* @param int $stackPtr The position of the double quoted
201
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
203
$tokens = $phpcsFile->getTokens();
205
$phpReservedVars = array(
217
if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
218
foreach ($matches[1] as $varName) {
219
// If it's a php reserved var, then its ok.
220
if (in_array($varName, $phpReservedVars) === true) {
224
// There is no way for us to know if the var is public or private,
225
// so we have to ignore a leading underscore if there is one and just
226
// check the main part of the variable name.
227
$originalVarName = $varName;
228
if (substr($varName, 0, 1) === '_') {
229
if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
230
$varName = substr($varName, 1);
234
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
235
$varName = $matches[0];
236
$error = 'Variable "%s" is not in valid camel caps format';
237
$data = array($originalVarName);
238
$phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
239
} else if (preg_match('|\d|', $varName)) {
240
$warning = 'Variable "%s" contains numbers but this is discouraged';
241
$data = array($originalVarName);
242
$phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
247
}//end processVariableInString()