Skip to content

Commit

Permalink
NamingConventions/ObjectNameDepth: support PHP 8.2+ readonly classes
Browse files Browse the repository at this point in the history
To check whether a class is deprecated, the sniff needs to skip over the potential class modifier keywords, like `final` and `abstract`.

As of PHP 8.2, it will also need to skip over the new `readonly` keyword.

Includes unit tests.
  • Loading branch information
jrfnl committed Nov 4, 2023
1 parent 77ce66c commit dc6695c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\ObjectDeclarations;
use WordPressCS\WordPress\Helpers\SnakeCaseHelper;
Expand Down Expand Up @@ -129,11 +130,8 @@ public function process( File $phpcsFile, $stackPtr ) {
}

// Check if the OO structure is deprecated.
$ignore = [
\T_ABSTRACT => \T_ABSTRACT,
\T_FINAL => \T_FINAL,
\T_WHITESPACE => \T_WHITESPACE,
];
$ignore = Collections::classModifierKeywords();
$ignore[ \T_WHITESPACE ] = \T_WHITESPACE;

$comment_end = $stackPtr;
for ( $comment_end = ( $stackPtr - 1 ); $comment_end >= 0; $comment_end-- ) {
Expand Down
15 changes: 15 additions & 0 deletions Yoast/Tests/NamingConventions/ObjectNameDepthUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ enum MyEnumName {} // OK.
enum Too_Long_Enum_Name: int {} // Error.
enum TooLongEnumName: int {} // Error.
enum Three_Word_Name_Mock: string implements Word_Interface {} // OK.

/**
* Class description, no @deprecated tag.
*
* @since x.x.x
*/
#[Some_Attribute]
readonly class Active_Class_With_Attribute_With_Too_Long_Class_Name {} // Error.

/**
* Class description.
*
* @deprecated x.x.x
*/
final readonly class Deprecated_Readonly_Class_With_Too_Long_Class_Name {} // OK.
1 change: 1 addition & 0 deletions Yoast/Tests/NamingConventions/ObjectNameDepthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getErrorList( string $testFile = '' ): array {
116 => 1,
135 => 1,
136 => 1,
145 => 1,
];

default:
Expand Down

0 comments on commit dc6695c

Please sign in to comment.