Skip to content

Commit 5529b4c

Browse files
authored
Merge pull request #332 from PHPCSStandards/php-8.3/generic-uppercaseconstantname-support-typed-constants
PHP 8.3 | Generic/UpperCaseConstantName: add support for typed constants
2 parents 5e9339a + 45bf3ad commit 5529b4c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ public function process(File $phpcsFile, $stackPtr)
4646
$tokens = $phpcsFile->getTokens();
4747

4848
if ($tokens[$stackPtr]['code'] === T_CONST) {
49-
// This is a class constant.
50-
$constant = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
49+
// This is a constant declared with the "const" keyword.
50+
// This may be an OO constant, in which case it could be typed, so we need to
51+
// jump over a potential type to get to the name.
52+
$assignmentOperator = $phpcsFile->findNext([T_EQUAL, T_SEMICOLON], ($stackPtr + 1));
53+
if ($assignmentOperator === false || $tokens[$assignmentOperator]['code'] !== T_EQUAL) {
54+
// Parse error/live coding. Nothing to do. Rest of loop is moot.
55+
return;
56+
}
57+
58+
$constant = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($assignmentOperator - 1), ($stackPtr + 1), true);
5159
if ($constant === false) {
5260
return;
5361
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ class ClassConstBowOutTest {
3131
}
3232

3333
$foo->getBar()?->define('foo');
34+
35+
// PHP 8.3 introduces typed constants.
36+
class TypedConstants {
37+
const MISSING_VALUE; // Parse error.
38+
const MyClass MYCONST = new MyClass;
39+
const int VALID_NAME = 0;
40+
const INT invalid_name = 0;
41+
const FALSE false = false; // Yes, false can be used as a constant name, don't ask.
42+
const array ARRAY = array(); // Same goes for array.
43+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function getErrorList()
3838
19 => 1,
3939
28 => 1,
4040
30 => 1,
41+
40 => 1,
42+
41 => 1,
4143
];
4244

4345
}//end getErrorList()

0 commit comments

Comments
 (0)