Skip to content

Commit 1b1fc31

Browse files
committed
PHP 8.1 | PSR12/ConstantVisibility: allow for class constants to be final
PHP 8.1 introduces the ability to declare class constants as `final`. The tokenization of the `final` keyword in this context appears to be consistent PHPCS cross-version, so AFAICS no changes are needed to the PHP tokenizer. However, the `PSR12.Properties.ConstantVisibility` sniff does need to allow for them. Includes unit tests. Ref: https://wiki.php.net/rfc/final_class_const
1 parent 1cf73da commit 1b1fc31

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function process(File $phpcsFile, $stackPtr)
4747
return;
4848
}
4949

50-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
50+
$ignore = Tokens::$emptyTokens;
51+
$ignore[] = T_FINAL;
52+
53+
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
5154
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
5255
return;
5356
}

src/Standards/PSR12/Tests/Properties/ConstantVisibilityUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ class Foo {
55
}
66

77
const APPLICATION_ENV = 'development';
8+
9+
// Issue 3526, PHP 8.1 final constants.
10+
class SampleEnum
11+
{
12+
final const FOO = 'SAMPLE';
13+
14+
public final const BAR = 'SAMPLE';
15+
16+
final private const BAZ = 'SAMPLE';
17+
}

src/Standards/PSR12/Tests/Properties/ConstantVisibilityUnitTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function getErrorList()
4040
*/
4141
public function getWarningList()
4242
{
43-
return [4 => 1];
43+
return [
44+
4 => 1,
45+
12 => 1,
46+
];
4447

4548
}//end getWarningList()
4649

0 commit comments

Comments
 (0)