From 6891c046f1b30af622aa5bca14ea27dc42e476d6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 3 May 2023 14:09:20 +0200 Subject: [PATCH] PSR12/FileHeader: bug fix - false positives on PHP 8.2+ readonly classes As reported in https://github.com/squizlabs/PHP_CodeSniffer/issues/3799#issuecomment-1532870980, the `FileHeader` sniff did not take the new PHP 8.2+ `readonly` OO modifier keyword into account. Fixed now. Includes test. --- package.xml | 1 + .../PSR12/Sniffs/Files/FileHeaderSniff.php | 1 + .../PSR12/Tests/Files/FileHeaderUnitTest.18.inc | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.18.inc diff --git a/package.xml b/package.xml index 9338b46e79..3336ca16e3 100644 --- a/package.xml +++ b/package.xml @@ -1233,6 +1233,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php index 8a8255cf47..c3d0d0cadc 100644 --- a/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php @@ -188,6 +188,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr) if (isset($commentOpeners[$tokens[$docToken]['code']]) === false && isset(Tokens::$methodPrefixes[$tokens[$docToken]['code']]) === false + && $tokens[$docToken]['code'] !== T_READONLY ) { // Check for an @var annotation. $annotation = false; diff --git a/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.18.inc b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.18.inc new file mode 100644 index 0000000000..be7c11ac91 --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.18.inc @@ -0,0 +1,16 @@ +