MultiAssignmentUsage
info on by defaultGroup: Control flow · PhpStorm name: MultiAssignmentUsageInspection
Two patterns where destructuring would be clearer:
- unpacking the
foreachvalue variable withlist(...) = $valueas a separate statement, whenforeach (... as list(...))can do it directly (PHP 5.5+); - consecutive statements pulling numbered elements out of the same array one at a time (
$a = $row[0]; $b = $row[1];), which a singlelist(...) = $rowreplaces.
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
*/