1.2.1241. Undefined Class Constants

Class constants that are used, but never defined. This yield a fatal error upon execution, but no feedback at compile level.

This analysis takes into account native PHP class constants, extensions and stubs. It also disambiguate enumeration cases.

Constants are searched in the typed class or interface, and their parent. They are not searched in the children, since the children are not necessarily available, unless the class is abstract. In particular, one of the children may not define the constant, and when such child is used, it will satisfy the type, but not the constant definition.

<?php

class foo {
    const A = 1;
}

function foo(Foo $f) {
     // here, C is not defined in the code and is reported
     echo foo::A.foo::B.foo::C;

     // This is also an undefined constant
     echo $f::B;
}

?>

See also Class constants.

1.2.1241.1. Suggestions

  • Fix the name of the constant

  • Add the constant to the current class or one of its parent

  • Update the constant’s visibility

1.2.1241.2. Specs

Short name

Classes/UndefinedConstants

Rulesets

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

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

High

Features

class-constant, undefined

Note

This issue may lint but will not run

Available in

Entreprise Edition, Community Edition, Exakat Cloud