Skip to content

CompactArguments ​

error on by default

Group: Probable bugs · PhpStorm name: CompactArgumentsInspection

compact('name') silently skips (or, since PHP 7.3, warns about) variables that do not exist at that point. A name that never appeared earlier in the function is usually a typo or a variable assigned too late.

Example ​

php
<?php
function summary($net, $tax)
{
    $gross = $net + $tax;
    return compact(
        'net',
        "tax",
        'gross',
        'discount',
        'discount',
        '$gross',
        "{$gross}",
        $net
    );
}

function lateAssignment($unit)
{
    $payload = compact('unit', 'count');
    $count = 3;
    return $payload;
}

class Report
{
    public function build()
    {
        $title = 'Q3';
        $render = function () use ($title) {
            return compact('title', 'footer');
        };
        return compact('title', 'render');
    }
}

$top = compact('nothing');

Reported:

  • line 10: Variable '$discount' may be undefined when compact() runs.
  • line 11: Variable '$$gross' may be undefined when compact() runs.
  • line 19: Variable '$count' may be undefined when compact() runs.
  • line 30: Variable '$footer' may be undefined when compact() runs.

Configure ​

In custos.json:

json
{
  "rules": {
    "CompactArguments": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore CompactArguments

/**
 * @noinspection CompactArgumentsInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.