| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
|
|
|
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: ValidVariableNameSniff.php 253706 2008-02-25 00:25:02Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
|
|
|
18 |
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
|
|
|
23 |
*
|
|
|
24 |
* Checks the naming of variables and member variables.
|
|
|
25 |
*
|
|
|
26 |
* @category PHP
|
|
|
27 |
* @package PHP_CodeSniffer
|
|
|
28 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
29 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
30 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
31 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
32 |
* @version Release: 1.2.1
|
|
|
33 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
34 |
*/
|
|
|
35 |
class Squiz_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
|
|
|
36 |
{
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Tokens to ignore so that we can find a DOUBLE_COLON.
|
|
|
40 |
*
|
|
|
41 |
* @var array
|
|
|
42 |
*/
|
|
|
43 |
private $_ignore = array(
|
|
|
44 |
T_WHITESPACE,
|
|
|
45 |
T_COMMENT,
|
|
|
46 |
);
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Processes this test, when one of its tokens is encountered.
|
|
|
51 |
*
|
|
|
52 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
53 |
* @param int $stackPtr The position of the current token in the
|
|
|
54 |
* stack passed in $tokens.
|
|
|
55 |
*
|
|
|
56 |
* @return void
|
|
|
57 |
*/
|
|
|
58 |
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
59 |
{
|
|
|
60 |
$tokens = $phpcsFile->getTokens();
|
|
|
61 |
$varName = ltrim($tokens[$stackPtr]['content'], '$');
|
|
|
62 |
|
|
|
63 |
$phpReservedVars = array(
|
|
|
64 |
'_SERVER',
|
|
|
65 |
'_GET',
|
|
|
66 |
'_POST',
|
|
|
67 |
'_REQUEST',
|
|
|
68 |
'_SESSION',
|
|
|
69 |
'_ENV',
|
|
|
70 |
'_COOKIE',
|
|
|
71 |
'_FILES',
|
|
|
72 |
'GLOBALS',
|
|
|
73 |
);
|
|
|
74 |
|
|
|
75 |
// If it's a php reserved var, then its ok.
|
|
|
76 |
if (in_array($varName, $phpReservedVars) === true) {
|
|
|
77 |
return;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
|
|
|
81 |
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
|
|
|
82 |
// Check to see if we are using a variable from an object.
|
|
|
83 |
$var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
|
|
|
84 |
if ($tokens[$var]['code'] === T_STRING) {
|
|
|
85 |
$bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
|
|
|
86 |
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
|
|
|
87 |
$objVarName = $tokens[$var]['content'];
|
|
|
88 |
|
|
|
89 |
// There is no way for us to know if the var is public or private,
|
|
|
90 |
// so we have to ignore a leading underscore if there is one and just
|
|
|
91 |
// check the main part of the variable name.
|
|
|
92 |
$originalVarName = $objVarName;
|
|
|
93 |
if (substr($objVarName, 0, 1) === '_') {
|
|
|
94 |
$objVarName = substr($objVarName, 1);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
|
|
|
98 |
$error = "Variable \"$originalVarName\" is not in valid camel caps format";
|
|
|
99 |
$phpcsFile->addError($error, $var);
|
|
|
100 |
}
|
|
|
101 |
}//end if
|
|
|
102 |
}//end if
|
|
|
103 |
}//end if
|
|
|
104 |
|
|
|
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.
|
|
|
114 |
$inClass = true;
|
|
|
115 |
} else {
|
|
|
116 |
$inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if ($inClass === true) {
|
|
|
120 |
$varName = substr($varName, 1);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
|
|
|
125 |
$error = "Variable \"$originalVarName\" is not in valid camel caps format";
|
|
|
126 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
}//end processVariable()
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Processes class member variables.
|
|
|
134 |
*
|
|
|
135 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
136 |
* @param int $stackPtr The position of the current token in the
|
|
|
137 |
* stack passed in $tokens.
|
|
|
138 |
*
|
|
|
139 |
* @return void
|
|
|
140 |
*/
|
|
|
141 |
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
142 |
{
|
|
|
143 |
$tokens = $phpcsFile->getTokens();
|
|
|
144 |
|
|
|
145 |
$varName = ltrim($tokens[$stackPtr]['content'], '$');
|
|
|
146 |
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
|
|
|
147 |
$public = ($memberProps['scope'] !== 'private');
|
|
|
148 |
|
|
|
149 |
if ($public === true) {
|
|
|
150 |
if (substr($varName, 0, 1) === '_') {
|
|
|
151 |
$scope = ucfirst($memberProps['scope']);
|
|
|
152 |
$error = "$scope member variable \"$varName\" must not contain a leading underscore";
|
|
|
153 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
154 |
return;
|
|
|
155 |
}
|
|
|
156 |
} else {
|
|
|
157 |
if (substr($varName, 0, 1) !== '_') {
|
|
|
158 |
$error = "Private member variable \"$varName\" must contain a leading underscore";
|
|
|
159 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
160 |
return;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
|
|
|
165 |
$error = "Variable \"$varName\" is not in valid camel caps format";
|
|
|
166 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
}//end processMemberVar()
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Processes the variable found within a double quoted string.
|
|
|
174 |
*
|
|
|
175 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
176 |
* @param int $stackPtr The position of the double quoted
|
|
|
177 |
* string.
|
|
|
178 |
*
|
|
|
179 |
* @return void
|
|
|
180 |
*/
|
|
|
181 |
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
182 |
{
|
|
|
183 |
$tokens = $phpcsFile->getTokens();
|
|
|
184 |
|
|
|
185 |
$phpReservedVars = array(
|
|
|
186 |
'_SERVER',
|
|
|
187 |
'_GET',
|
|
|
188 |
'_POST',
|
|
|
189 |
'_REQUEST',
|
|
|
190 |
'_SESSION',
|
|
|
191 |
'_ENV',
|
|
|
192 |
'_COOKIE',
|
|
|
193 |
'_FILES',
|
|
|
194 |
'GLOBALS',
|
|
|
195 |
);
|
|
|
196 |
|
|
|
197 |
if (preg_match_all('|[^\\\]\$([a-zA-Z0-9_]+)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
|
|
|
198 |
foreach ($matches[1] as $varName) {
|
|
|
199 |
// If it's a php reserved var, then its ok.
|
|
|
200 |
if (in_array($varName, $phpReservedVars) === true) {
|
|
|
201 |
continue;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
// There is no way for us to know if the var is public or private,
|
|
|
205 |
// so we have to ignore a leading underscore if there is one and just
|
|
|
206 |
// check the main part of the variable name.
|
|
|
207 |
$originalVarName = $varName;
|
|
|
208 |
if (substr($varName, 0, 1) === '_') {
|
|
|
209 |
if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
|
|
|
210 |
$varName = substr($varName, 1);
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
|
|
|
215 |
$varName = $matches[0];
|
|
|
216 |
$error = "Variable \"$originalVarName\" is not in valid camel caps format";
|
|
|
217 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
}//end if
|
|
|
221 |
|
|
|
222 |
}//end processVariableInString()
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
}//end class
|
|
|
226 |
|
|
|
227 |
?>
|