Skip to content

Commit 67ae154

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/UpperCaseConstantName: handle unconventional spacing and comments before define
This commit fixes false positives in the sniff when handling unconventional spacing and comments before `define`. When checking to see if the `define` found is not a method call, the sniff would incorrectly exclude only whitespaces. But comments can also be placed between `define` and one of the tokens the sniff looks for (T_OBJECT_OPERATOR, T_DOUBLE_COLON or T_NULLSAFE_OBJECT_OPERATOR). Looking for the first non-empty token before `define` fixes this problem.
1 parent a60ed63 commit 67ae154

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr)
8888
}
8989

9090
// Make sure this is not a method call.
91-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
91+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
9292
if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR
9393
|| $tokens[$prev]['code'] === T_DOUBLE_COLON
9494
|| $tokens[$prev]['code'] === T_NULLSAFE_OBJECT_OPERATOR

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ define
5151
'CommentsInUnconventionalPlaces',
5252
'value'
5353
);
54+
55+
$foo-> /* comment */ define('bar');
56+
$foo?->
57+
// phpcs:ignore Stnd.Cat.SniffName -- for reasons.
58+
define('bar');

0 commit comments

Comments
 (0)