WordPress / WordPress/WordPress-Coding-Standards
PrefixAllGlobals: false negatives when checking group constant declarations
Nobody has claimed this yet.
- 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:
Tested Against develop Branch?
- I have verified the issue still exists in the
developbranch of WordPressCS.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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