Error Identifier: traitUse.deprecatedInterface
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);
namespace App;
/** @deprecated Use NewInterface instead */
interface OldInterface
{
public function doSomething(): void;
}
class Foo
{
use OldInterface;
}
Why is it reported? #
This error is reported by the phpstan/phpstan-deprecation-rules package.
The use statement inside a class body references an interface that is marked as @deprecated. While using an interface in a use statement is already incorrect (interfaces cannot be used as traits), the deprecation error is also reported because the referenced symbol is deprecated.
How to fix it #
Interfaces should be implemented using the implements keyword, not use. Replace the trait use with a proper implements declaration using the non-deprecated replacement:
<?php declare(strict_types = 1);
namespace App;
-class Foo
+class Foo implements NewInterface
{
- use OldInterface;
+ public function doSomething(): void
+ {
+ // ...
+ }
}
If the class is itself deprecated, the error will not be reported:
<?php declare(strict_types = 1);
namespace App;
+/** @deprecated */
class Foo
{
use OldInterface;
}
How to ignore this error #
You can use the identifier traitUse.deprecatedInterface to ignore this error using a comment:
// @phpstan-ignore traitUse.deprecatedInterface
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: traitUse.deprecatedInterface
Rules that report this error #
- PHPStan\Rules\Deprecations\RestrictedDeprecatedClassNameUsageExtension [1] phpstan/phpstan-deprecation-rules