Error Identifier: trait.disallowedSubtype
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-sealed AllowedUser
*/
interface UserProvider
{
}
trait UserTrait
{
}
class AllowedUser implements UserProvider
{
use UserTrait;
}
class DisallowedUser implements UserProvider
{
use UserTrait;
}
Why is it reported? #
A trait is used in a class that is not allowed to be a subtype of a sealed parent. The parent class or interface restricts which types can extend or implement it using the @phpstan-sealed PHPDoc tag. The class using this trait is not listed among the allowed subtypes.
Note: the trait. prefix in the identifier indicates that the disallowed subtype is a trait definition. This follows the same logic as class.disallowedSubtype but applies when a trait body is checked.
How to fix it #
Add the class to the list of allowed subtypes:
/**
- * @phpstan-sealed AllowedUser
+ * @phpstan-sealed AllowedUser|DisallowedUser
*/
interface UserProvider
{
}
Or remove the sealed parent from the class declaration:
-class DisallowedUser implements UserProvider
+class DisallowedUser
{
use UserTrait;
}
How to ignore this error #
You can use the identifier trait.disallowedSubtype to ignore this error using a comment:
// @phpstan-ignore trait.disallowedSubtype
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: trait.disallowedSubtype
Rules that report this error #
- PHPStan\Rules\Classes\AllowedSubTypesRule [1]