website/errors/unaryPlus.nonNumeric.md
<?php declare(strict_types = 1);
function doFoo(?int $value): void
{
$result = +$value;
}
This error is reported by the phpstan-strict-rules package.
The unary + operator is applied to a non-numeric value. The unary plus operator is intended for numeric types (int, float). When applied to non-numeric types like null, string, or objects, PHP silently coerces the value, which can lead to unexpected results and hides type errors.
Ensure the operand is a numeric type before applying unary +:
function doFoo(?int $value): void
{
- $result = +$value;
+ if ($value !== null) {
+ $result = +$value;
+ }
}
Or cast the value explicitly if the coercion is intentional:
function doFoo(?int $value): void
{
- $result = +$value;
+ $result = (int) $value;
}