Skip to content

Commit a60ed63

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/UpperCaseConstantName: handle unconventional spacing and comments after define(
This commit fixes false negatives in the sniff when handling unconventional spacing and comments after `define(`. When checking for the constant name after the opening parenthesis, the sniff would incorrectly exclude only whitespaces while comments can also be placed between the opening parenthesis and the constant name. Looking for the constant name by getting the next non-empty token after the opening parenthesis fixes this problem. The added tests also include comments between `define` and the opening parenthesis but that was already handled correctly by the sniff.
1 parent ffd4f09 commit a60ed63

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public function process(File $phpcsFile, $stackPtr)
103103
return;
104104
}
105105

106-
// The next non-whitespace token must be the constant name.
107-
$constPtr = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
106+
// The next non-empty token must be the constant name.
107+
$constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
108108
if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
109109
return;
110110
}

src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ class TypedConstants {
4141
const FALSE false = false; // Yes, false can be used as a constant name, don't ask.
4242
const array ARRAY = array(); // Same goes for array.
4343
}
44+
45+
define /* comment */ ( /* comment */ 'CommentsInUnconventionalPlaces', 'value' );
46+
47+
define
48+
// comment
49+
(
50+
// phpcs:ignore Stnd.Cat.SniffName -- for reasons.
51+
'CommentsInUnconventionalPlaces',
52+
'value'
53+
);

src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function getErrorList()
4040
30 => 1,
4141
40 => 1,
4242
41 => 1,
43+
45 => 1,
44+
47 => 1,
4345
];
4446

4547
}//end getErrorList()

0 commit comments

Comments
 (0)