Error Identifier: enum.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(AllowedEnum)
*/
interface Permission
{
}
enum AllowedEnum: string implements Permission
{
case Read = 'read';
}
enum DisallowedEnum: string implements Permission
{
case Write = 'write';
}
Why is it reported? #
The parent class or interface restricts which types are allowed to extend or implement it using the @phpstan-sealed PHPDoc tag (or the AllowedSubTypes interface). The enum in question is not listed among the allowed subtypes.
In the example above, Permission only allows AllowedEnum as a subtype. DisallowedEnum implements Permission but is not in the allowed list, so it is reported.
This mechanism enforces closed type hierarchies, ensuring that only a known set of types can implement a given interface.
How to fix it #
Add the enum to the list of allowed subtypes:
<?php declare(strict_types = 1);
/**
- * @phpstan-sealed(AllowedEnum)
+ * @phpstan-sealed(AllowedEnum, DisallowedEnum)
*/
interface Permission
{
}
Or remove the interface implementation if the enum should not be part of this type hierarchy:
<?php declare(strict_types = 1);
-enum DisallowedEnum: string implements Permission
+enum DisallowedEnum: string
{
case Write = 'write';
}
How to ignore this error #
You can use the identifier enum.disallowedSubtype to ignore this error using a comment:
// @phpstan-ignore enum.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: enum.disallowedSubtype
Rules that report this error #
- PHPStan\Rules\Classes\AllowedSubTypesRule [1]