Skip to content

Commit 35abfb0

Browse files
committed
PHP 8.3 | Generic/LowerCaseConstant: add support for typed constants
Type declarations are explicitly not checked by this sniff. PHP 8.3 introduces typed constants. This means that the sniff now also needs to ensure that it doesn't flag non-lowercase `true`/`false`/`null` in the type declaration for a constant. Fixed now. Includes tests.
1 parent 7240235 commit 35abfb0

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function register()
8181
$targets[] = T_CLOSURE;
8282
$targets[] = T_FN;
8383

84+
// Register constant keyword to filter out type declarations.
85+
$targets[] = T_CONST;
86+
8487
return $targets;
8588

8689
}//end register()
@@ -101,6 +104,19 @@ public function process(File $phpcsFile, $stackPtr)
101104
{
102105
$tokens = $phpcsFile->getTokens();
103106

107+
// Skip over potential type declarations for constants.
108+
if ($tokens[$stackPtr]['code'] === T_CONST) {
109+
// Constant must always have a value assigned to it, so we can just look for the assignment
110+
// operator. Anything between the const keyword and the assignment can be safely ignored.
111+
$skipTo = $phpcsFile->findNext(T_EQUAL, ($stackPtr + 1));
112+
if ($skipTo !== false) {
113+
return $skipTo;
114+
}
115+
116+
// If we're at the end of the file, just return.
117+
return;
118+
}
119+
104120
/*
105121
* Skip over type declarations for properties.
106122
*

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ readonly class Properties {
144144
}
145145
}
146146

147+
// PHP 8.3 introduces typed constants.
148+
class TypedConstants {
149+
const MyClass|NULL|TRUE|FALSE MYCONST = FALSE;
150+
}
151+
152+
// Global constants can not be typed.
153+
const MYCONST = TRUE;
154+
147155
// Last coding/parse error.
148156
// This has to be the last test in the file.
149157
function UnclosedCurly (): FALSE {

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ readonly class Properties {
144144
}
145145
}
146146

147+
// PHP 8.3 introduces typed constants.
148+
class TypedConstants {
149+
const MyClass|NULL|TRUE|FALSE MYCONST = false;
150+
}
151+
152+
// Global constants can not be typed.
153+
const MYCONST = true;
154+
147155
// Last coding/parse error.
148156
// This has to be the last test in the file.
149157
function UnclosedCurly (): FALSE {

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function getErrorList($testFile='')
6464
121 => 1,
6565
125 => 1,
6666
129 => 1,
67+
149 => 1,
68+
153 => 1,
6769
];
6870

6971
case 'LowerCaseConstantUnitTest.js':

0 commit comments

Comments
 (0)