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
|
<?php
/**
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
*
* Ensures that constant names are all uppercase.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: 2.7.1
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_STRING);
}//end register()
/**
* 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
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$constName = $tokens[$stackPtr]['content'];
// If this token is in a heredoc, ignore it.
if ($phpcsFile->hasCondition($stackPtr, T_START_HEREDOC) === true) {
return;
}
// Special case for PHP 5.5 class name resolution.
if (strtolower($constName) === 'class'
&& $tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON
) {
return;
}
// Special case for PHPUnit.
if ($constName === 'PHPUnit_MAIN_METHOD') {
return;
}
// If the next non-whitespace token after this token
// is not an opening parenthesis then it is not a function call.
for ($openBracket = ($stackPtr + 1); $openBracket < $phpcsFile->numTokens; $openBracket++) {
if ($tokens[$openBracket]['code'] !== T_WHITESPACE) {
break;
}
}
if ($openBracket === $phpcsFile->numTokens) {
return;
}
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
$functionKeyword = $phpcsFile->findPrevious(
array(
T_WHITESPACE,
T_COMMA,
T_COMMENT,
T_STRING,
T_NS_SEPARATOR,
),
($stackPtr - 1),
null,
true
);
if ($tokens[$functionKeyword]['code'] !== T_CONST) {
return;
}
// This is a class constant.
if (strtoupper($constName) !== $constName) {
if (strtolower($constName) === $constName) {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'lower');
} else {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'mixed');
}
$error = 'Class constants must be uppercase; expected %s but found %s';
$data = array(
strtoupper($constName),
$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ClassConstantNotUpperCase', $data);
} else {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'upper');
}
return;
}//end if
if (strtolower($constName) !== 'define') {
return;
}
/*
This may be a "define" function call.
*/
// Make sure this is not a method call.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR
|| $tokens[$prev]['code'] === T_DOUBLE_COLON
) {
return;
}
// The next non-whitespace token must be the constant name.
$constPtr = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
$constName = $tokens[$constPtr]['content'];
// Check for constants like self::CONSTANT.
$prefix = '';
$splitPos = strpos($constName, '::');
if ($splitPos !== false) {
$prefix = substr($constName, 0, ($splitPos + 2));
$constName = substr($constName, ($splitPos + 2));
}
// Strip namesspace from constant like /foo/bar/CONSTANT.
$splitPos = strrpos($constName, '\\');
if ($splitPos !== false) {
$prefix = substr($constName, 0, ($splitPos + 1));
$constName = substr($constName, ($splitPos + 1));
}
if (strtoupper($constName) !== $constName) {
if (strtolower($constName) === $constName) {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'lower');
} else {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'mixed');
}
$error = 'Constants must be uppercase; expected %s but found %s';
$data = array(
$prefix.strtoupper($constName),
$prefix.$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
} else {
$phpcsFile->recordMetric($stackPtr, 'Constant name case', 'upper');
}
}//end process()
}//end class
|