Menu

← Back to unaryOp.*

Error Identifier: unaryOp.invalid

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);

function doFoo(array $a): void
{
	-$a;
}

Why is it reported? #

A unary operator (+, -, or ~) is applied to a value whose type does not support that operation. PHP’s unary operators have specific type requirements:

  • + (unary plus) and - (unary minus) require a numeric type (int, float, or a string that can be converted to a number).
  • ~ (bitwise not) requires an int, float, or string.

Applying these operators to incompatible types like arrays, objects, or resources results in a runtime error.

How to fix it #

Ensure the operand has a type compatible with the unary operator.

-function doFoo(array $a): void
+function doFoo(int $a): void
 {
 	-$a;
 }

Or convert the value to an appropriate type before applying the operator:

 function doFoo(string $value): void
 {
-	-$value;
+	-(int) $value;
 }

How to ignore this error #

You can use the identifier unaryOp.invalid to ignore this error using a comment:

// @phpstan-ignore unaryOp.invalid
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: unaryOp.invalid

Rules that report this error #

  • PHPStan\Rules\Operators\InvalidUnaryOperationRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.