Skip to content

MultiAssignmentUsage ​

info on by default

Group: Control flow · PhpStorm name: MultiAssignmentUsageInspection

Two patterns where destructuring would be clearer:

  1. unpacking the foreach value variable with list(...) = $value as a separate statement, when foreach (... as list(...)) can do it directly (PHP 5.5+);
  2. consecutive statements pulling numbered elements out of the same array one at a time ($a = $row[0]; $b = $row[1];), which a single list(...) = $row replaces.

Example ​

php
<?php
foreach ($points as $idx => $point) {
    [$px, $py] = $point;
    echo $idx, $px, $py;
}
foreach ($pairs as $idx => $pair) {
    [$left, $right] = $pair;              // $pair is still read below
    if (is_array($pair)) {
        [$top, $bottom] = $pair;          // nested under a guard
    }
    list($m, $n) = $other;                // not a loop variable
    $fn = function () use ($pair) { list($u) = $pair; };   // closure boundary

    $width = $pair[0];
    // a plain comment
    /** @var int $height */
    $height = $pair[1];
    $depth = $pair[-1];

    $name = $pair['name'];
    $kind = $pair['kind'];                // string index
    $copy = [];
    $copy[0] = $pair[1];
    $copy[1] = $pair[0];                  // assigns into a non-variable
}

Reported:

  • line 3: Destructure directly in the foreach header.
  • line 17: Use one destructuring assignment from '$pair' instead.
  • line 18: Use one destructuring assignment from '$pair' instead.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore MultiAssignmentUsage

/**
 * @noinspection MultiAssignmentUsageInspection
 */

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.