Skip to content

Commit 6891c04

Browse files
committed
PSR12/FileHeader: bug fix - false positives on PHP 8.2+ readonly classes
As reported in #3799 (comment), the `FileHeader` sniff did not take the new PHP 8.2+ `readonly` OO modifier keyword into account. Fixed now. Includes test.
1 parent ed8e00d commit 6891c04

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
12331233
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.15.inc" role="test" />
12341234
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.16.inc" role="test" />
12351235
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.17.inc" role="test" />
1236+
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.18.inc" role="test" />
12361237
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
12371238
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
12381239
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc.fixed" role="test" />

src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
188188

189189
if (isset($commentOpeners[$tokens[$docToken]['code']]) === false
190190
&& isset(Tokens::$methodPrefixes[$tokens[$docToken]['code']]) === false
191+
&& $tokens[$docToken]['code'] !== T_READONLY
191192
) {
192193
// Check for an @var annotation.
193194
$annotation = false;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test;
6+
7+
/**
8+
* Sniff should recognize the PHP 8.2 `readonly` keyword as an OO prefix.
9+
*/
10+
readonly class Example
11+
{
12+
public function test() : void
13+
{
14+
echo '123';
15+
}
16+
}

0 commit comments

Comments
 (0)