Skip to content

IncrementDecrementOperationEquivalent ​

info on by default quick-fix

Group: 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 ​

OptionTypeDefaultEffect
PREFER_PREFIX_STYLEbooltrueFix produces ++$x / --$x.
PREFER_SUFFIX_STYLEboolfalseMutually 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
 */

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