Source for file UpperCaseConstantNameSniff.php
Documentation is available at UpperCaseConstantNameSniff.php
* Ensures that constant names are all uppercase.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class UpperCaseConstantNameSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
public function process (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 ) {
// Special case for PHP 5.5 class name resolution.
&& $tokens[($stackPtr - 1 )]['code'] === T_DOUBLE_COLON
// Special case for PHPUnit.
if ($constName === 'PHPUnit_MAIN_METHOD') {
// 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 ) {
if ($openBracket === $phpcsFile->numTokens ) {
$functionKeyword = $phpcsFile->findPrevious (
if ($tokens[$functionKeyword]['code'] !== T_CONST ) {
// This is a class constant.
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'lower');
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'mixed');
$error = 'Class constants must be uppercase; expected %s but found %s';
$phpcsFile->addError ($error, $stackPtr, 'ClassConstantNotUpperCase', $data);
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'upper');
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
// 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 ) {
$constName = $tokens[$constPtr]['content'];
// Check for constants like self::CONSTANT.
$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 ));
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'lower');
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'mixed');
$error = 'Constants must be uppercase; expected %s but found %s';
$phpcsFile->addError ($error, $stackPtr, 'ConstantNotUpperCase', $data);
$phpcsFile->recordMetric ($stackPtr, 'Constant name case', 'upper');
Documentation generated on Mon, 11 Mar 2019 15:27:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|