WordPress / WordPress/WordPress-Coding-Standards

Support for PHP 8.3 typed class constants

Open
#2,546 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Helpers Focus: Modern PHP
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

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 (https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/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.

I already added safeguarding tests to two sniffs in #2547.

As far as I can check, no further changes to the sniffs are necessary, but two parts of the WPCS code still require changes. I'm creating this issue to document it.

ConstantsHelper::is_use_of_global_constant()

This helper method determines whether an arbitrary T_STRING token is the use of a global constant. It currently does not account for typed class constant syntax when checking if a variable is a class constant, leading to false positives.

For example, it incorrectly flags the following class constant as a global constant:

class MyClass {
    const string STYLESHEETPATH = 'something';
}

The method assumes that the first non-empty token before the constant name must be T_CONST for a constant to be a class constant, but this is not the case for typed class constants. The example above results in a false positive for the WordPress.WP.DiscouragedConstants as it discourages the use of the global STYLESHEETPATH constant.

A similar version of this method also exists in PHPCompatibility (https://github.com/PHPCompatibility/PHPCompatibility/blob/develop/PHPCompatibility/Helpers/MiscHelper.php#L45), and it will require similar changes. Since the version on PHPCompatibility is covered by tests, those changes will likely be implemented first in that repository and then ported here.

AbstractClassRestrictionsSniff

This abstract sniff class needs to be updated to check if the restricted classes are used as class constant types (and property types as well). Currently, it does not check for the use of the restricted classes in those places.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing ConstantsHelper::is_use_of_global_constant() with the tested PHPCompatibility MiscHelper version, then inspect AbstractClassRestrictionsSniff and the existing safeguarding tests from #2547. Update the two WPCS areas to recognize typed class constants and restricted classes used in constant or property types, and confirm the reported false positive is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.