Error Identifier: class.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(AllowedChild)
*/
abstract class Base
{
}
class AllowedChild extends Base
{
}
class DisallowedChild extends Base
{
}
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 class in question is not listed among the allowed subtypes.
In the example above, Base only allows AllowedChild as a subtype. DisallowedChild is not in the allowed list, so it is reported.
This mechanism is useful for simulating sealed classes or closed type hierarchies, ensuring that only a known set of types can extend or implement a given base type.
How to fix it #
Add the class to the list of allowed subtypes in the parent:
<?php declare(strict_types = 1);
/**
- * @phpstan-sealed(AllowedChild)
+ * @phpstan-sealed(AllowedChild, DisallowedChild)
*/
abstract class Base
{
}
Or extend a different class that allows subtyping:
<?php declare(strict_types = 1);
-class DisallowedChild extends Base
+class DisallowedChild extends AllowedChild
{
}
How to ignore this error #
You can use the identifier class.disallowedSubtype to ignore this error using a comment:
// @phpstan-ignore class.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: class.disallowedSubtype
Rules that report this error #
- PHPStan\Rules\Classes\AllowedSubTypesRule [1]