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 ($pairs as $idx => $pair) {
    [$left, $right] = $pair;
    if (is_array($pair)) {
        [$top, $bottom] = $pair;          // E2b: nested under a guard
    }
    list($m, $n) = $other;                // E2: not a loop variable
    $fn = function () use ($pair) { list($u) = $pair; };   // E2: closure boundary

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

    $name = $pair['name'];
    $kind = $pair['kind'];                // E4
    $copy = [];
    $copy[0] = $pair[1];
    $copy[1] = $pair[0];                  // E5
}

Reported:

  • line 3: Destructure directly in the foreach header.
  • line 13: Use one destructuring assignment from '$pair' instead.
  • line 14: 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. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.