Naming Constant

ID

php.naming_constant

Severity

info

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

low

Resource

Naming Convention

Language

Php

Tags

convention, naming_convention

Description

Reports a constant declaration whose name does not follow the UPPER_SNAKE_CASE convention recommended by PSR-1: the name must consist of uppercase letters, digits and underscores only, starting with a letter. Three declaration forms are covered: a global const X = …​;, a class constant const X = …​;, and a define('X', …​) call.

Rationale

PSR-1 recommends declaring class constants in all upper case with underscore separators, and the same convention is widely applied to global and define() constants. Naming a constant maxRetries or Default_Host makes it indistinguishable from a variable or a class at the point of use and obscures that the value is immutable. A uniform UPPER_SNAKE_CASE style keeps constants visually distinct.

<?php
const maxRetries = 3;             // FLAW — not UPPER_SNAKE_CASE
const MAX_RETRIES = 3;            // OK

define('default_locale', 'en');  // FLAW — not UPPER_SNAKE_CASE
define('DEFAULT_LOCALE', 'en');  // OK

class Config
{
    const defaultPort = 8080;    // FLAW — not UPPER_SNAKE_CASE
    const DEFAULT_PORT = 8080;   // OK
}

Remediation

Rename the constant to UPPER_SNAKE_CASE: upper-case every letter and separate words with underscores (maxRetries becomes MAX_RETRIES). The accepted pattern is configurable through the constantNamePattern property.