Error Identifier: interface.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
* @phpstan-require-extends AllowedImplementation
*/
interface Contract
{
}
class AllowedImplementation implements Contract
{
}
class DisallowedImplementation implements Contract
{
}
Why is it reported? #
The interface or class restricts which types are allowed to extend or implement it using AllowedSubTypesClassReflectionExtension. The reported class is not in the list of allowed subtypes. This mechanism enforces closed hierarchies where only specific implementations are permitted, similar to sealed classes in other languages.
How to fix it #
If the class should be an allowed subtype, register it with the appropriate extension. Otherwise, use a different approach that does not require implementing the restricted interface:
-class DisallowedImplementation implements Contract
+class DisallowedImplementation
{
}
Or restructure the code to extend one of the allowed implementations:
-class DisallowedImplementation implements Contract
+class DisallowedImplementation extends AllowedImplementation
{
}
How to ignore this error #
You can use the identifier interface.disallowedSubtype to ignore this error using a comment:
// @phpstan-ignore interface.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: interface.disallowedSubtype
Rules that report this error #
- PHPStan\Rules\Classes\AllowedSubTypesRule [1]