3
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
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: UpperCaseConstantNameSniff.php,v 1.10 2008/05/07 23:13:49 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
20
* Ensures that constant names are all uppercase.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
* @version Release: 1.1.0
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff implements PHP_CodeSniffer_Sniff
36
* Returns an array of tokens this test wants to listen for.
40
public function register()
42
return array(T_STRING);
48
* Processes this test, when one of its tokens is encountered.
50
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
* @param int $stackPtr The position of the current token in the
52
* stack passed in $tokens.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
$tokens = $phpcsFile->getTokens();
59
$constName = $tokens[$stackPtr]['content'];
61
// If this token is in a heredoc, ignore it.
62
if ($phpcsFile->hasCondition($stackPtr, T_START_HEREDOC) === true) {
66
// If the next non-whitespace token after this token
67
// is not an opening parenthesis then it is not a function call.
68
$openBracket = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
69
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
70
$functionKeyword = $phpcsFile->findPrevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING), ($stackPtr - 1), null, true);
72
$declarations = array(
81
if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) {
82
// This is just a declaration; no constants here.
86
if ($tokens[$functionKeyword]['code'] === T_CONST) {
87
// This is a class constant.
88
if (strtoupper($constName) !== $constName) {
89
$error = 'Class constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
90
$phpcsFile->addError($error, $stackPtr);
96
// Is this a class name?
97
$nextPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
98
if ($tokens[$nextPtr]['code'] === T_DOUBLE_COLON) {
102
// Is this a type hint?
103
if ($tokens[$nextPtr]['code'] === T_VARIABLE) {
105
} else if ($phpcsFile->isReference($nextPtr) === true) {
109
// Is this a member var name?
110
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
111
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
115
// Is this an instance of declare()
116
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackPtr - 1), null, true);
117
if ($tokens[$prevPtr]['code'] === T_DECLARE) {
121
// This is a real constant.
122
if (strtoupper($constName) !== $constName) {
123
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
124
$phpcsFile->addError($error, $stackPtr);
127
} else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') {
130
This may be a "define" or "constant" function call.
133
// The next non-whitespace token must be the constant name.
134
$constPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($openBracket + 1), null, true);
135
if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
139
$constName = $tokens[$constPtr]['content'];
140
if (strtoupper($constName) !== $constName) {
141
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
142
$phpcsFile->addError($error, $stackPtr);