Skip to content

IssetArgumentExistence ​

warning on by default

Group: Probable bugs · PhpStorm name: IssetArgumentExistenceInspection

isset($v), empty($v) and $v ?? … silently accept a variable that is never defined in the current function. When the first mention of a plain variable inside a function body is such an existence check, the variable can never be set at that point — usually a typo or a leftover after a refactoring.

Example ​

php
<?php
class Billing
{
    public function total($items)
    {
        $sum = $sum ?? 0;
        $hit = isset($cache, $items);
        $nil = empty($nil);
        $cb  = function ($rate) use ($items) {
            return [$rate ?? 1, isset($items), $_POST ?? null, isset($this)];
        };
        return [$sum, $hit, $nil, $cb];
    }

    public function known()
    {
        echo $label;
        return $label ?? 'none';
    }

    public function looping($rows)
    {
        foreach ($rows as $row) {
            $prev = isset($last) ? $last : null;
            $last = $row;
        }
    }

    public function jumping()
    {
        again:
        $r = isset($tries) ? $tries : 0;
        $tries = 1;
        goto again;
    }

    public function including()
    {
        include 'defaults.php';
        return isset($config); // reported upstream when IGNORE_INCLUDES = true; never by custos (D11)
    }

    public function arrow()
    {
        return fn() => $missing ?? 0;
    }
}
$top = $undefinedAtTop ?? 1;

Reported:

  • line 6: Variable '$sum' is not defined in this scope.
  • line 7: Variable '$cache' is not defined in this scope.
  • line 8: Variable '$nil' is not defined in this scope.

Options ​

OptionTypeDefaultEffect
IGNORE_INCLUDESbooltrueWhen true, include/require statements in the function are disregarded (reports still happen). When false, a function containing any include/require is skipped entirely (the included file could define the variable). The EA fixture runs with false.

Configure ​

In custos.json:

json
{
  "rules": {
    "IssetArgumentExistence": {
      "enabled": false,
      "options": {
        "IGNORE_INCLUDES": true
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore IssetArgumentExistence

/**
 * @noinspection IssetArgumentExistenceInspection
 */

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