Automattic / Automattic/phpcs-neutron-standard

DisallowDefineSniff should only check for top-level define

Open
#44 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
93
Forks
9
PR merge metrics
No merged PRs in 30d

Description

`DisallowDefineSniff.php` adds and error (!) everytime a `define` is encountered and the reason (which I fully agree to) is that `const` should be preferred.

However, in PHP `const` is not allowed inside condition blocks, because those are only checked at runtime. For example a snippet like the following is invalid and throws a parse error:

```php
if (!defined('FOO')) {
const FOO = true;
}
```

Another limitation of `const` is that it does not accept expression as constant name. For example following is also invalid and throws a parse error:

```php
foreach(['FOO', 'BAR'] as $name) {
const $name = true;
}
```

So there are cases in which the usage of `define` is really necessary, and pretty much it turns out to be anytime the _level_ of `const` token is not `1`.

If you agree, I could send a PR for this.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading DisallowDefineSniff.php and trace how it determines the nesting level of each define call. Check the conditional and dynamic-name examples from the issue, then confirm that only top-level define usage is reported while nested usage remains allowed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.