Deprecated Constant

ID

php.deprecated_constant

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Php

Tags

code_smell, deprecation

Description

Reports uses of a constant declared in the same file whose declaration is annotated with a @deprecated tag in the documentation block immediately preceding it. Three declaration forms are recognised: a global const X = …​;, a define('X', …​) call, and a class constant const X = …​; inside a class. The check is limited to the current file, so no cross-file resolution is required: bare reads such as echo X; are matched against global and define() constants, while static accesses such as Config::X are matched against class constants.

Rationale

A @deprecated tag marks a value that still exists today but is scheduled for removal, usually in favour of a documented replacement. Continuing to read it spreads dependence on a value that will disappear and postpones the migration. Surfacing each use site keeps the deprecation visible and guides callers to the supported constant.

<?php
/**
 * @deprecated 2.0 use MAX_ITEMS instead
 */
const OLD_MAX = 100;

const MAX_ITEMS = 250;

echo OLD_MAX;     // FLAW - reads a constant marked @deprecated
echo MAX_ITEMS;   // OK - uses the supported replacement

Remediation

Replace the use with the constant named in the @deprecated tag. When no replacement is documented, define a maintained equivalent and update the references, then remove the dependence on the deprecated constant.