Skip to content

PHP 8.3 typed class constants: add tests to two sniffs #2547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

rodrigoprimo
Copy link
Collaborator

@rodrigoprimo rodrigoprimo commented Jul 18, 2025

Typed class constants were introduced in PHP 8.3 (https://wiki.php.net/rfc/typed_class_constants). PHPCS started supporting it in version 3.9.0 (PHPCSStandards/PHP_CodeSniffer#321). Since WPCS requires PHPCS 3.13.x, with @jrfnl's help, I investigated what needed to be updated in the WPCS repository to accommodate this new syntax.

In this PR, I'm adding tests to WordPress.NamingConventions.PrefixAllGlobals and WordPress.WP.CapitalPDangit to safeguard that those two sniffs continue to handle type class constants correctly, as both sniffs use the T_CONST token. No changes were required to the code of those sniffs.

ConstantsHelper::is_use_of_global_constant() and AbstractClassRestrictionsSniff will need to be changed as well to support typed class constants. This will be addressed in another PR. For now, I documented this task in #2546.

…tant type

This commit modifies one of the existing class constants in the code used in the `WordPress.NamingConventions.PrefixAllGlobal` tests to have a type. This is just to ensure that there is a test for typed class constants added in PHP 8.3 as this sniff listens to the `T_CONST` token.

The sniff was already handling typed class constants correctly and no change to the sniff code is needed.
This commit adds a few tests using type class constants to the `WordPress.WP.CapitalPDangit` tests. This is just to ensure that the part of the sniff code that checks for `T_CONST` tokens to ignore constant declarations using the `const` keyword works correctly with typed class constants added in PHP 8.3.

The sniff was already handling typed class constants correctly, and no change to the sniff code is needed.
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rodrigoprimo ! LGTM.
One nitpick, not blocking.

@@ -231,3 +231,16 @@ echo Some\Enum\WordPress::ENUM_CONSTANT;

// Safeguard that the sniff doesn't act on anonymous classes.
$anon = new class() {};

/*
* Safeguard that PHP 8.3+ typed class constants are handled correctly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Safeguard that PHP 8.3+ typed class constants are handled correctly.
* Safeguard that PHP 8.3+ typed class constants are handled correctly (i.e. ignored).

@jrfnl jrfnl added this to the 3.2.0 milestone Jul 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants