Skip to content

Commit

Permalink
PSR12/FileHeader: bug fix - false positives on PHP 8.2+ readonly classes
Browse files Browse the repository at this point in the history
As reported in squizlabs#3799 (comment), the `FileHeader` sniff did not take the new PHP 8.2+ `readonly` OO modifier keyword into account.

Fixed now.

Includes test.
  • Loading branch information
jrfnl committed May 3, 2023
1 parent ed8e00d commit 6891c04
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.15.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.16.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.17.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.18.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc.fixed" role="test" />
Expand Down
1 change: 1 addition & 0 deletions src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.18.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Test;

/**
* Sniff should recognize the PHP 8.2 `readonly` keyword as an OO prefix.
*/
readonly class Example
{
public function test() : void
{
echo '123';
}
}

0 comments on commit 6891c04

Please # to comment.