Skip to content

UselessUnset ​

info on by default

Group: Unused · PhpStorm name: UselessUnsetInspection

Calling unset() on a function parameter only destroys the function's local binding: the caller's value (whether passed by value or by reference) is unaffected, and the local goes away at return anyway. Such an unset() is almost always pointless.

Example ​

php
<?php
function consume(array &$queue, $item, ...$rest)
{
    static $seen;
    global $registry;

    unset($queue['head']);
    unset($seen, $registry);
    unset($item);
    unset($seen, $rest);
    unset(
        $queue,
        $item
    );

    foreach ($registry as $entry) {
        unset($entry);
    }

    $later = function () use ($item) {
        unset($item);
    };

    return;
    unset($item);
}

class Pool
{
    public function release($handle)
    {
        $handle = null;
        unset($handle);
    }
}

Reported:

  • line 9: Unsetting a parameter only drops the local variable; this unset() is pointless.
  • line 10: Unsetting a parameter only drops the local variable; this unset() is pointless.
  • line 12: Unsetting a parameter only drops the local variable; this unset() is pointless.
  • line 13: Unsetting a parameter only drops the local variable; this unset() is pointless.
  • line 33: Unsetting a parameter only drops the local variable; this unset() is pointless.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore UselessUnset

/**
 * @noinspection UselessUnsetInspection
 */

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