A variable is read in a function that never gives it a value

tx0/php:undefined-variabledefaultwarningreliability/api-misusedepth fastbug

What it finds

A variable read in a function that nowhere in the function gets a value: it is not a parameter, not assigned, not bound by global, static, use, foreach, list or catch, and not passed to a call that could assign it by reference. Reading it gives null and a warning, and usually means a typo or a variable from another function. Functions whose variables cannot be known from their text, such as those that call extract or include files, are left alone.

From the rule's source, undefined_variable.rs.

Example from the rule's tests
<?phpfunction total($items){    $sum = 0;    foreach ($items as $item) {        $sum += $item->price;    }    return $sum + $tax;tx0 reports: $tax is never given a value in this function} function defined_in_many_ways($text, $list){    global $config;    static $calls = 0;    preg_match('/x/', $text, $matches);    [$a, $b] = $list;    try {        work();    } catch (Exception $e) {        log_it($e);    }    $f = function () use ($config) {        return $config;    };    $g = fn($x) => $x + $calls;    if (isset($maybe)) {        echo $maybe;    }    return [$matches, $a, $b, $f, $g, $other ?? null];} function dynamic($vars){    extract($vars);    return $anything;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/php:undefined-variable" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:undefined-variable] reason="..."

tx0 explain tx0/php:undefined-variable shows the same in the terminal. Found a false positive? Report it.