Skip to content

Commit

Permalink
Generic UpperCaseConstantNameSniff no longer reports errors where con…
Browse files Browse the repository at this point in the history
…stants are used (request #20090)
  • Loading branch information
gsherwood committed Oct 31, 2013
1 parent 2c884a0 commit 4af1311
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 152 deletions.
Expand Up @@ -91,153 +91,63 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
true
);

$declarations = array(
T_FUNCTION,
T_CLASS,
T_INTERFACE,
T_TRAIT,
T_IMPLEMENTS,
T_EXTENDS,
T_INSTANCEOF,
T_NEW,
T_NAMESPACE,
T_USE,
T_AS,
T_GOTO,
T_INSTEADOF,
T_PUBLIC,
T_PRIVATE,
T_PROTECTED,
);

if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) {
// This is just a declaration; no constants here.
if ($tokens[$functionKeyword]['code'] !== T_CONST) {
return;
}

if ($tokens[$functionKeyword]['code'] === T_CONST) {
// This is a class constant.
if (strtoupper($constName) !== $constName) {
$error = 'Class constants must be uppercase; expected %s but found %s';
$data = array(
strtoupper($constName),
$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ClassConstantNotUpperCase', $data);
}

return;
}

// Is this a class name?
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$nextPtr]['code'] === T_DOUBLE_COLON) {
return;
}

// Is this a namespace name?
if ($tokens[$nextPtr]['code'] === T_NS_SEPARATOR) {
return;
}

// Is this an insteadof name?
if ($tokens[$nextPtr]['code'] === T_INSTEADOF) {
return;
}

// Is this an as name?
if ($tokens[$nextPtr]['code'] === T_AS) {
return;
}

// Is this a type hint?
if ($tokens[$nextPtr]['code'] === T_VARIABLE
|| $phpcsFile->isReference($nextPtr) === true
) {
return;
}

// Is this a member var name?
$prevPtr = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
return;
}

// Is this a variable name, in the form ${varname} ?
if ($tokens[$prevPtr]['code'] === T_OPEN_CURLY_BRACKET) {
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$nextPtr]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
}

// Is this a namespace name?
if ($tokens[$prevPtr]['code'] === T_NS_SEPARATOR) {
return;
}

// Is this an instance of declare()
$prevPtrDeclare = $phpcsFile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackPtr - 1), null, true);
if ($tokens[$prevPtrDeclare]['code'] === T_DECLARE) {
return;
}

// Is this a goto label target?
if ($tokens[$nextPtr]['code'] === T_COLON) {
if (in_array($tokens[$prevPtr]['code'], array(T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_COLON), true)) {
return;
}
}

// This is a real constant.
// This is a class constant.
if (strtoupper($constName) !== $constName) {
$error = 'Constants must be uppercase; expected %s but found %s';
$error = 'Class constants must be uppercase; expected %s but found %s';
$data = array(
strtoupper($constName),
$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
$phpcsFile->addError($error, $stackPtr, 'ClassConstantNotUpperCase', $data);
}

} else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') {
return;
}

/*
This may be a "define" or "constant" function call.
*/
if (strtolower($constName) !== 'define') {
return;
}

// 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;
}
/*
This may be a "define" function call.
*/

// 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;
}
// 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;
}

$constName = $tokens[$constPtr]['content'];
// 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;
}

// Check for constants like self::CONSTANT.
$prefix = '';
$splitPos = strpos($constName, '::');
if ($splitPos !== false) {
$prefix = substr($constName, 0, ($splitPos + 2));
$constName = substr($constName, ($splitPos + 2));
}
$constName = $tokens[$constPtr]['content'];

if (strtoupper($constName) !== $constName) {
$error = 'Constants must be uppercase; expected %s but found %s';
$data = array(
$prefix.strtoupper($constName),
$prefix.$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
}
}//end if
// Check for constants like self::CONSTANT.
$prefix = '';
$splitPos = strpos($constName, '::');
if ($splitPos !== false) {
$prefix = substr($constName, 0, ($splitPos + 2));
$constName = substr($constName, ($splitPos + 2));
}

if (strtoupper($constName) !== $constName) {
$error = 'Constants must be uppercase; expected %s but found %s';
$data = array(
$prefix.strtoupper($constName),
$prefix.$constName,
);
$phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
}

}//end process()

Expand Down
Expand Up @@ -42,27 +42,11 @@ class Generic_Tests_NamingConventions_UpperCaseConstantNameUnitTest extends Abst
*/
public function getErrorList()
{
$errors = array(
8 => 1,
10 => 1,
15 => 1,
25 => 1,
26 => 1,
27 => 1,
28 => 1,
29 => 1,
32 => 1,
35 => 1,
100 => 1,
);

// The trait insteadof test will only work in PHP version where traits exist
// and will throw errors in earlier versions.
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
$errors[131] = 3;
}

return $errors;
return array(
8 => 1,
10 => 1,
15 => 1,
);

}//end getErrorList()

Expand Down
2 changes: 2 additions & 0 deletions package.xml
Expand Up @@ -30,6 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- E.g., to ignore comments, override a property using:
-- name="ignoreIndentationTokens" type="array" value="T_COMMENT,T_DOC_COMMENT"
- PSR2 standard now ignores comments when checking indentation rules
- Generic UpperCaseConstantNameSniff no longer reports errors where constants are used (request #20090)
-- It still reports errors where constants are defined
- Fixed bug #20093 : Bug with ternary operator token
- Fixed bug #20097 : CLI.php throws error in php 5.2
- Fixed bug #20100 : incorrect Function mysql() has been deprecated report
Expand Down

0 comments on commit 4af1311

Please sign in to comment.