3
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
15
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
16
$error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
17
throw new PHP_CodeSniffer_Exception($error);
21
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
23
* Checks the naming of member variables.
26
* @package PHP_CodeSniffer
27
* @author Greg Sherwood <gsherwood@squiz.net>
28
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
29
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
30
* @version Release: 1.3.4
31
* @link http://pear.php.net/package/PHP_CodeSniffer
33
class PEAR_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
38
* Processes class member variables.
40
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
* @param int $stackPtr The position of the current token
42
* in the stack passed in $tokens.
46
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
48
$tokens = $phpcsFile->getTokens();
50
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
51
if (empty($memberProps) === true) {
55
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
56
$isPublic = ($memberProps['scope'] === 'private') ? false : true;
57
$scope = $memberProps['scope'];
58
$scopeSpecified = $memberProps['scope_specified'];
60
// If it's a private member, it must have an underscore on the front.
61
if ($isPublic === false && $memberName{0} !== '_') {
62
$error = 'Private member variable "%s" must be prefixed with an underscore';
63
$data = array($memberName);
64
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
68
// If it's not a private member, it must not have an underscore on the front.
69
if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
70
$error = '%s member variable "%s" must not be prefixed with an underscore';
75
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
79
}//end processMemberVar()
83
* Processes normal variables.
85
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
86
* @param int $stackPtr The position where the token was found.
90
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
92
// We don't care about normal variables.
95
}//end processVariable()
99
* Processes variables in double quoted strings.
101
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
102
* @param int $stackPtr The position where the token was found.
106
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
108
// We don't care about normal variables.
111
}//end processVariableInString()