1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
<?php
/**
* Validates variable names.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://opensource.org/licenses/BSD-3-Clause
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
}
/**
* Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
*
* Checks the naming of variables and member variables.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class OpenCart_Sniffs_Variables_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff {
/**
* Tokens to ignore so that we can find a DOUBLE_COLON.
*
* @var array
*/
private $_ignore = array(
T_WHITESPACE,
T_COMMENT,
);
/**
* Regex to match valid underscore names for variables
*
* @var string
*/
private static $underscore_var = '/^(_|[a-z](?:_?[a-z0-9]+)*)$/';
/**
* Complementary regex to just exclude camel casing
* @var string
*/
private static $camelcase = '/[a-z][A-Z]/';
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
$phpReservedVars = array(
'_SERVER',
'_GET',
'_POST',
'_REQUEST',
'_SESSION',
'_ENV',
'_COOKIE',
'_FILES',
'GLOBALS',
);
// If it's a php reserved var, then its ok.
if (in_array($varName, $phpReservedVars) === true) {
return;
}
if ($tokens[$stackPtr - 1]['code'] === T_PAAMAYIM_NEKUDOTAYIM) {
// static vars just ensure no camelcase (caps allowed)
if (preg_match(self::$camelcase, $varName)) {
$error = 'Variable "%s" is not in valid underscore format';
$phpcsFile->addError($error, $stackPtr, 'NotUnderscore', array($varName));
}
return;
}
$objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
// Check to see if we are using a variable from an object.
$var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
if ($tokens[$var]['code'] === T_STRING) {
$bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
$objVarName = $tokens[$var]['content'];
if (preg_match(self::$underscore_var, $objVarName) === 0) {
$phpcsFile->addError(
'Variable "%s" is not in valid underscore format',
$var,
'NotUnderscore',
array($objVarName)
);
}
}//end if
}//end if
}//end if
if (preg_match(self::$underscore_var, $varName) === 0) {
$error = 'Variable "%s" is not in valid underscore format';
$phpcsFile->addError($error, $stackPtr, 'NotUnderscore', array($varName));
}
}//end processVariable()
/**
* Processes class member variables.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
if (empty($memberProps) === true) {
// Couldn't get any info about this variable, which
// generally means it is invalid or possibly has a parse
// error. Any errors will be reported by the core, so
// we can ignore it.
return;
}
$errorData = array($varName);
if (
($memberProps['is_static'] && preg_match(self::$camelcase, $varName))
|| (!$memberProps['is_static'] && preg_match(self::$underscore_var, $varName) === 0)
) {
$error = 'Variable "%s" is not in valid underscore format';
$phpcsFile->addError($error, $stackPtr, 'MemberNotUnderscore', $errorData);
}
}//end processMemberVar()
/**
* Processes the variable found within a double quoted string.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the double quoted
* string.
*
* @return void
*/
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$phpReservedVars = array(
'_SERVER',
'_GET',
'_POST',
'_REQUEST',
'_SESSION',
'_ENV',
'_COOKIE',
'_FILES',
'GLOBALS',
);
if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
foreach ($matches[1] as $varName) {
// If it's a php reserved var, then its ok.
if (in_array($varName, $phpReservedVars) === true) {
continue;
}
// There is no way for us to know if the var is public or private,
// so we have to ignore a leading underscore if there is one and just
// check the main part of the variable name.
$originalVarName = $varName;
if (substr($varName, 0, 1) === '_') {
if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
$varName = substr($varName, 1);
}
}
if (preg_match(self::$underscore_var, $varName) === 0) {
$varName = $matches[0];
$error = 'Variable "%s" is not in valid underscore format';
$data = array($originalVarName);
$phpcsFile->addError($error, $stackPtr, 'StringNotUnderscore', $data);
}
}
}//end if
}//end processVariableInString()
}//end class
|