WordPress / WordPress/WordPress-Coding-Standards

PrefixAllGlobals: false negatives when checking group constant declarations

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

Nobody has claimed this yet.

Component: Extra Focus: WP interoperability Type: Bug
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Bug Description

The sniff WordPress.NamingConventions.PrefixAllGlobals does not take into consideration that multiple constants can be declared using a single const keyword. When there is more than one constant declaration, the sniff will check only the name of the first constant, potentially leading to false negatives.

Minimal Code Snippet

The issue happens when running this command:

vendor/bin/phpcs -s --standard=WordPress --sniffs=WordPress.NamingConventions.PrefixAllGlobals test.php

... over a file containing this code:

<?php

// phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] my_plugin

const MY_PLUGIN_CONSTANT = 'value', ANOTHER_CONSTANT = 'another_value';

I would expect to see a WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound error for the ANOTHER_CONSTANT constant, but no error is reported.

Error Code

WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound

Environment

Question Answer
PHP version 8.3.22
PHP_CodeSniffer version 3.13.2
WordPressCS version develop
PHPCSUtils version 1.1.0
PHPCSExtra version 1.4.0
WordPressCS install type git clone
IDE (if relevant) N/A

Additional Context (optional)

I believe the problem is related to the code below when the sniff gets the name of the constant without considering that there might be more than one constant declaration after T_CONST:

https://github.com/WordPress/WordPress-Coding-Standards/blob/4d0160f32f8537f3cdf2e301433cb15641083963/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php#L714-L733

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.

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 in WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php around lines 714-733, where the issue identifies constant names as the likely source. Run the provided vendor/bin/phpcs command against the minimal PHP snippet and verify that the non-prefixed ANOTHER_CONSTANT declaration produces WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.