Naming Single Char Var

ID

php.naming_single_char_var

Severity

info

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

low

Resource

Naming Convention

Language

Php

Tags

naming, naming_convention

Description

Reports variables introduced with a single-character name that is not one of the conventional short names. A variable is considered introduced at a parameter declaration, a plain assignment target ($x = …​) or a foreach loop variable. Read positions are not flagged, so each variable is reported once at its declaration, and the pseudo-variable $this is never flagged.

Rationale

A single-character name carries no meaning, forcing the reader to track what $a or $z holds across the surrounding code. Descriptive names make intent obvious and survive refactoring. A small set of conventional short names is tolerated: loop counters i, j, k, the exception variable e, and the type-hint shorthands c, b, d, f, l, o. The allowed list is configurable through the allowedNames property.

<?php
function run(array $items, $x): void   // FLAW - parameter $x is single char
{
    $a = 0;                            // FLAW - local $a is single char

    $total = 0;                        // OK - descriptive name

    foreach ($items as $z) {           // FLAW - foreach value $z is single char
        $total += $z;
    }

    for ($i = 0; $i < 3; $i++) {       // OK - $i is a conventional loop counter
        $total += $i;
    }
}

Remediation

Rename the variable to a descriptive name that states what it holds, for example $index, $result or $item. If a single-character name is a genuine local convention, add it to the allowedNames property.