Skip to content

Commit

Permalink
PHP 8.1 | PSR12/ConstantVisibility: allow for class constants to be `…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jrfnl committed Jan 5, 2022
1 parent 67c82d9 commit 7c2d3b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
$ignore = Tokens::$emptyTokens;
$ignore[] = T_FINAL;

$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ class Foo {
}

const APPLICATION_ENV = 'development';

// Issue 3526, PHP 8.1 final constants.
class SampleEnum
{
final const FOO = 'SAMPLE';

public final const BAR = 'SAMPLE';

final private const BAZ = 'SAMPLE';
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function getErrorList()
*/
public function getWarningList()
{
return [4 => 1];
return [
4 => 1,
12 => 1,
];

}//end getWarningList()

Expand Down

0 comments on commit 7c2d3b1

Please sign in to comment.