Skip to content

PowerOperatorCanBeUsed ​

warning on by default quick-fix PHP ≥ 5.6

Group: Language level migration · PhpStorm name: PowerOperatorCanBeUsedInspection

Since PHP 5.6 exponentiation has its own operator. pow($a, $b) can be written $a ** $b, which is shorter and avoids a function call.

Example ​

php
<?php
$area   = pow($side, 2);
$growth = pow($rate * 0.01 + 1, $years);
$scaled = \pow(2, $bits - 1);
$total  = $offset - pow($k, $m);
$safe   = pow($q ?? 0, $flag ? 2 : 3);
$keep   = pow($side);
$meth   = $calc->pow($side, 2);
php
<?php
$area   = $side ** 2;
$growth = ($rate * 0.01 + 1) ** $years;
$scaled = 2 ** ($bits - 1);
$total  = $offset - ($k ** $m);
$safe   = ($q ?? 0) ** ($flag ? 2 : 3);
$keep   = pow($side);
$meth   = $calc->pow($side, 2);

Reported:

  • line 2: Use '$side ** 2' (exponentiation operator) instead.
  • line 3: Use '($rate * 0.01 + 1) ** $years' (exponentiation operator) instead.
  • line 4: Use '2 ** ($bits - 1)' (exponentiation operator) instead.
  • line 5: Use '($k ** $m)' (exponentiation operator) instead.
  • line 6: Use '($q ?? 0) ** ($flag ? 2 : 3)' (exponentiation operator) instead.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore PowerOperatorCanBeUsed

/**
 * @noinspection PowerOperatorCanBeUsedInspection
 */

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