1.2.853. Nonexistent Variable In compact()

Compact() doesn’t warn when it tries to work on an nonexistent variable. It just ignores the variable.

This behavior changed in PHP 7.3, and compact() now emits a warning when the compacted variable doesn’t exist. For performances reasons, this analysis only works inside methods and functions.

<?php

function foo($b = 2) {
    $a = 1;
    // $c doesn't exists, and is not compacted.
    return compact('a', 'b', 'c');
}
?>

See also compact and PHP RFC: Make compact function reports undefined passed variables.

1.2.853.1. Suggestions

  • Fix the name of variable in the compact() argument list

  • Remove the name of variable in the compact() argument list

1.2.853.2. Specs

Short name

Php/CompactInexistant

Rulesets

All, Changed Behavior, CompatibilityPHP73, Suggestions

Exakat since

1.2.9

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

High

Features

compact

Available in

Entreprise Edition, Exakat Cloud