Skip to content

Commit

Permalink
Constant naming false positive on goto labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktomk committed May 12, 2012
1 parent c717874 commit f15acb2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
T_NAMESPACE,
T_USE,
T_AS,
T_GOTO,
);

if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) {
Expand Down Expand Up @@ -136,11 +137,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

// Is this an instance of declare()
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackPtr - 1), null, true);
if ($tokens[$prevPtr]['code'] === T_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.
if (strtoupper($constName) !== $constName) {
$error = 'Constants must be uppercase; expected %s but found %s';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,21 @@ class A {
}

A::constant('anything-not-in-uppercase');

// goto
goto gotolabel;

gototarget1:

function ConstTest() {
gototarget2:
}

switch($foo)
{
case $bar:
gototarget3:
}


?>
4 changes: 4 additions & 0 deletions CodeSniffer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
define('T_NS_SEPARATOR', 1051);
}

if (defined('T_GOTO') === false) {
define('T_GOTO', 1053);
}

// Some PHP 5.4 tokens, replicated for lower versions.
if (defined('T_TRAIT') === false) {
define('T_TRAIT', 1052);
Expand Down

0 comments on commit f15acb2

Please sign in to comment.