Error Identifier: requireImplements.nonObject
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);
/**
* @phpstan-require-implements int
*/
trait MyTrait // ERROR: PHPDoc tag @phpstan-require-implements contains non-object type int.
{
}
Why is it reported? #
The @phpstan-require-implements PHPDoc tag must reference an interface (object) type. Scalar types like int, string, bool, union types, or other non-object types cannot be implemented by a class and are therefore not valid in this tag.
How to fix it #
Replace the non-object type with an interface:
<?php declare(strict_types = 1);
+interface Countable
+{
+ public function count(): int;
+}
+
/**
- * @phpstan-require-implements int
+ * @phpstan-require-implements Countable
*/
trait MyTrait
{
}
If the requirement is about a specific type constraint that is not an interface, consider whether @phpstan-require-extends (for classes) or another mechanism is more appropriate.
How to ignore this error #
You can use the identifier requireImplements.nonObject to ignore this error using a comment:
// @phpstan-ignore requireImplements.nonObject
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: requireImplements.nonObject
Rules that report this error #
- PHPStan\Rules\PhpDoc\RequireImplementsDefinitionTraitRule [1]