1.2.727. Multiple Constant Definition

Some constants are defined several times in your code. This will lead to a fatal error, if they are defined during the same execution.

Multiple definitions may happens at bootstrap, when the application code is collecting information about the current environment. It may also happen at inclusion time, which one set of constant being loaded, while other definition are not, avoiding conflict. Both are false positive.

<?php

// OS is defined twice.
if (PHP_OS == 'Windows') {
    define('OS', 'Win');
} else {
    define('OS', 'Other');
}

?>

See also class.

1.2.727.1. Suggestions

  • Move the constants to a class, and include the right class based on control flow.

  • Give different names to the constants, and keep the condition close to utilisation.

  • Move the constants to an external configuration file : it will be easier to identify that those constants may change.

1.2.727.2. Specs

Short name

Constants/MultipleConstantDefinition

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

class

Examples

Dolibarr, OpenConf

Available in

Entreprise Edition, Community Edition, Exakat Cloud