Skip to content

NestedAssignmentsUsage ​

info off by default quick-fix

Group: Code style · PhpStorm name: NestedAssignmentsUsageInspection

Chained assignments such as $x = $y = 5 pack several writes into one expression; they are easy to misread and a typo (= instead of +/==) silently becomes another assignment. Separate statements are clearer.

Example ​

php
<?php
function setup() {
    $width = $height = 64;
    $first = $last = "none";
    $p = $q = $r = make_point(1, 2);
    $left = $right = $origin;
    $lo = $hi = -1;

    if ($m = $n = fetch()) {}   // reported, no fix

    $solo = 3;            // not chained
    $k = ($j = 2);        // parenthesised: not reported
    $acc .= $tmp = 'x';   // outer compound: not reported
}
php
<?php
function setup() {
    $height = 64;
    $width = 64;
    $last = "none";
    $first = "none";
    $r = make_point(1, 2);
    $q = $r;
    $p = $r;
    $right = $origin;
    $left = $origin;
    $hi = -1;
    $lo = -1;

    if ($m = $n = fetch()) {}   // reported, no fix

    $solo = 3;            // not chained
    $k = ($j = 2);        // parenthesised: not reported
    $acc .= $tmp = 'x';   // outer compound: not reported
}

Reported:

  • line 3: Split this chained assignment into separate assignments.
  • line 4: Split this chained assignment into separate assignments.
  • line 5: Split this chained assignment into separate assignments.
  • line 6: Split this chained assignment into separate assignments.
  • line 7: Split this chained assignment into separate assignments.
  • line 9: Split this chained assignment into separate assignments.

Configure ​

In custos.json:

json
{
  "rules": {
    "NestedAssignmentsUsage": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore NestedAssignmentsUsage

/**
 * @noinspection NestedAssignmentsUsageInspection
 */

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