IncrementDecrementOperationEquivalent
info on by default quick-fixGroup: Code style · PhpStorm name: IncrementDecrementOperationEquivalentInspection
Adding or subtracting the literal 1 to a variable and storing it back ($n += 1, $n = $n - 1, …) is more idiomatically written with the increment or decrement operator.
Example
Options PREFER_PREFIX_STYLE = false, PREFER_SUFFIX_STYLE = true:
php
<?php
$page += 1;
$this->left -= 1;
$total = 1 + $total;
$total = $total + 1;
self::$depth = self::$depth - 1;
$total = 1 - $total;
$total = $total + 1.0;
$slots = [0, 0];
$slots[1] += 1;
while (true) { $n = $n - 1; }php
<?php
$page++;
$this->left--;
$total++;
$total++;
self::$depth--;
$total = 1 - $total;
$total = $total + 1.0;
$slots = [0, 0];
$slots[1]++;
while (true) { $n--; }Reported:
- line 3: Use '$page++' instead.
- line 4: Use '$this->left--' instead.
- line 5: Use '$total++' instead.
- line 6: Use '$total++' instead.
- line 7: Use 'self::$depth--' instead.
- line 12: Use '$slots[1]++' instead.
- line 14: Use '$n--' instead.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| PREFER_PREFIX_STYLE | bool | true | Fix produces ++$x / --$x. |
| PREFER_SUFFIX_STYLE | bool | false | Mutually exclusive with the above (radio choice); fix produces $x++ / $x-- whenever PREFER_PREFIX_STYLE is false. |
Configure
In custos.json:
json
{
"rules": {
"IncrementDecrementOperationEquivalent": {
"enabled": false,
"options": {
"PREFER_PREFIX_STYLE": true,
"PREFER_SUFFIX_STYLE": false
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore IncrementDecrementOperationEquivalent
/**
* @noinspection IncrementDecrementOperationEquivalentInspection
*/