Error Identifier: pow.rightNonNumeric
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
$base = 2;
$exponent = null;
$result = $base ** $exponent;
Why is it reported? #
This error is reported by the phpstan-strict-rules extension.
The right operand of the ** (exponentiation) operator is not a numeric type. PHP will attempt to coerce the value to a number, which can produce unexpected results or warnings. Strict type checking ensures that only int or float values are used in arithmetic operations.
How to fix it #
Ensure the right operand is a numeric type:
$base = 2;
-$exponent = null;
+$exponent = 3;
$result = $base ** $exponent;
If the value comes from an external source, validate or cast it before use:
-$result = $base ** $input;
+$result = $base ** (int) $input;
How to ignore this error #
You can use the identifier pow.rightNonNumeric to ignore this error using a comment:
// @phpstan-ignore pow.rightNonNumeric
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: pow.rightNonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule [1] phpstan/phpstan-strict-rules