diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 1373d3b0e3..f62cfe94bd 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -46,8 +46,16 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['code'] === T_CONST) { - // This is a class constant. - $constant = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + // This is a constant declared with the "const" keyword. + // This may be an OO constant, in which case it could be typed, so we need to + // jump over a potential type to get to the name. + $assignmentOperator = $phpcsFile->findNext([T_EQUAL, T_SEMICOLON], ($stackPtr + 1)); + if ($assignmentOperator === false || $tokens[$assignmentOperator]['code'] !== T_EQUAL) { + // Parse error/live coding. Nothing to do. Rest of loop is moot. + return; + } + + $constant = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($assignmentOperator - 1), ($stackPtr + 1), true); if ($constant === false) { return; } diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc index 6bf96f251c..20bbf90e26 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc @@ -31,3 +31,13 @@ class ClassConstBowOutTest { } $foo->getBar()?->define('foo'); + +// PHP 8.3 introduces typed constants. +class TypedConstants { + const MISSING_VALUE; // Parse error. + const MyClass MYCONST = new MyClass; + const int VALID_NAME = 0; + const INT invalid_name = 0; + const FALSE false = false; // Yes, false can be used as a constant name, don't ask. + const array ARRAY = array(); // Same goes for array. +} diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php index 907d8bb5be..afe3625589 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php @@ -38,6 +38,8 @@ public function getErrorList() 19 => 1, 28 => 1, 30 => 1, + 40 => 1, + 41 => 1, ]; }//end getErrorList()