diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php index cae35eeb41..4447f46304 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php @@ -81,6 +81,9 @@ public function register() $targets[] = T_CLOSURE; $targets[] = T_FN; + // Register constant keyword to filter out type declarations. + $targets[] = T_CONST; + return $targets; }//end register() @@ -101,6 +104,19 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + // Skip over potential type declarations for constants. + if ($tokens[$stackPtr]['code'] === T_CONST) { + // Constant must always have a value assigned to it, so we can just look for the assignment + // operator. Anything between the const keyword and the assignment can be safely ignored. + $skipTo = $phpcsFile->findNext(T_EQUAL, ($stackPtr + 1)); + if ($skipTo !== false) { + return $skipTo; + } + + // If we're at the end of the file, just return. + return; + } + /* * Skip over type declarations for properties. * diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc index 5dfb75560a..07e54e2581 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc @@ -144,6 +144,14 @@ readonly class Properties { } } +// PHP 8.3 introduces typed constants. +class TypedConstants { + const MyClass|NULL|TRUE|FALSE MYCONST = FALSE; +} + +// Global constants can not be typed. +const MYCONST = TRUE; + // Last coding/parse error. // This has to be the last test in the file. function UnclosedCurly (): FALSE { diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc.fixed index 6b999cc456..723a7221e5 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc.fixed @@ -144,6 +144,14 @@ readonly class Properties { } } +// PHP 8.3 introduces typed constants. +class TypedConstants { + const MyClass|NULL|TRUE|FALSE MYCONST = false; +} + +// Global constants can not be typed. +const MYCONST = true; + // Last coding/parse error. // This has to be the last test in the file. function UnclosedCurly (): FALSE { diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php index 0d1ea8b2ab..f156263348 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php @@ -64,6 +64,8 @@ public function getErrorList($testFile='') 121 => 1, 125 => 1, 129 => 1, + 149 => 1, + 153 => 1, ]; case 'LowerCaseConstantUnitTest.js':