Skip to content

DisallowDefineSniff should only check for top-level define #44

@gmazzap

Description

@gmazzap

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:

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions