Error Identifier: requireExtends.finalClass
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);
final class BaseService
{
}
/**
* @phpstan-require-extends BaseService
*/
interface ServiceAware
{
}
Why is it reported? #
The @phpstan-require-extends PHPDoc tag references a class that is declared as final. A final class cannot be extended, so requiring that implementing classes extend it is impossible to satisfy. Any class that would implements ServiceAware could never also extends BaseService because PHP forbids extending final classes.
How to fix it #
If the class should be extendable, remove the final modifier:
<?php declare(strict_types = 1);
-final class BaseService
+class BaseService
{
}
/**
* @phpstan-require-extends BaseService
*/
interface ServiceAware
{
}
If the class must remain final, remove the @phpstan-require-extends tag and consider using @phpstan-require-implements with an interface instead:
<?php declare(strict_types = 1);
+interface BaseServiceInterface
+{
+}
+
/**
- * @phpstan-require-extends BaseService
+ * @phpstan-require-implements BaseServiceInterface
*/
interface ServiceAware
{
}
How to ignore this error #
You can use the identifier requireExtends.finalClass to ignore this error using a comment:
// @phpstan-ignore requireExtends.finalClass
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: requireExtends.finalClass