Skip to content

SuspiciousAssignments ​

error on by default

Group: Probable bugs · PhpStorm name: SuspiciousAssignmentsInspection

Groups six independent checks for assignments that are most likely mistakes: writes lost to a switch fall-through, compound operators that repeat their target ($n += $n + 1), parameters overwritten before ever being read, =+/=-/=! typos, values written twice in a row, and array destructuring of something that is not an array.

Example ​

php
<?php
function classify($kind) {
    switch ($kind) {
        case 'x':
            $label = 'ex';
        case 'y':
            $label = 'why';
            break;
        case 'p':
            $pair = 1;
        case 'q':
            [$pair, $rest] = [2, 3];
            return $pair;
        case 'r':
        case 's':
            $acc[] = $kind;
            $msg = 'm';
            $msg = strtoupper($msg);
            break;
    }
}

$total -= $total - 1;
$path .= $path . '/';
$path .= $path . '/' . 'x';
$total *= ($total * 3);

function normalize($name, $size, &$out) {
    $name = strtolower($name);
    $size = strlen($name);
    $out = [];
    return [$name, $size];
}

$delta =- $step;
$ok =! $failed;
$delta = -$step;
$other=+$step;

function flow($cond, $list) {
    if ($cond) { $mode = 'a'; }
    $mode = 'b';

    if ($cond) { $cfg["k"] = 1; return; }
    $cfg["k"] = 2;

    $count = 0;
    $count = count($list);

    $list[] = 1;
    $list[] = 2;
    $tmp = 'x';
    $tmp = "<{$tmp}>";
    return [$mode, $cfg, $count, $tmp];
}

function unpack_all(int $n, array $row, \ArrayObject $obj) {
    [$a, $b] = $row;
    list($c, $d) = $obj;
    [$e, $f] = $n;
    list($g) = new \DateTime();
}

/** @return int[]|false */
function sample_pair() { return [1, 2]; }

function failure_markers(array|false $found, ?array $cached, string|false $line, false $none) {
    [$p, $q] = $found;                   // E8: false ignored next to array
    [$r] = $cached;                      // E8: null ignored next to array
    [$s, $t] = sample_pair();            // E8: int[]|false
    [$u] = $line;   // string remains
    [$w] = $none;   // only false
}

Reported:

  • line 7: This write overwrites a value set in a previous case; a 'break' may be missing.
  • line 12: This write overwrites a value set in a previous case; a 'break' may be missing.
  • line 23: The target is repeated on the right-hand side of the compound assignment; likely a merge mistake.
  • line 24: The target is repeated on the right-hand side of the compound assignment; likely a merge mistake.
  • line 30: Parameter is overwritten before its value is used.
  • line 35: Did you mean '-='? Fix the operator or the spacing.
  • line 36: Did you mean '!='? Fix the operator or the spacing.
  • line 42: $mode is overwritten right after the 'if'; an 'else' may be missing.
  • line 48: $count is overwritten right after being assigned.
  • line 60: Destructuring a value that is not an array.
  • line 61: Destructuring a value that is not an array.
  • line 71: Destructuring a value that is not an array.
  • line 72: Destructuring a value that is not an array.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore SuspiciousAssignments

/**
 * @noinspection SuspiciousAssignmentsInspection
 */

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