Error Identifier: interface.duplicate
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);
// In file: src/Contracts/Logger.php
namespace App\Contracts;
interface Logger
{
public function log(string $message): void;
}
<?php declare(strict_types = 1);
// In file: src/Legacy/Logger.php
namespace App\Contracts;
interface Logger
{
public function log(string $message): void;
}
Why is it reported? #
The same interface is declared in multiple files within the analysed codebase. PHP does not allow two interfaces with the same fully-qualified name. When the autoloader loads one of them, the other becomes unreachable, and if both files are included, a fatal error occurs.
In the example above, App\Contracts\Logger is defined in two different files.
How to fix it #
Remove the duplicate declaration and keep only one definition of the interface:
-// Delete or rename the duplicate file src/Legacy/Logger.php
If both interfaces are needed, give them different names or place them in different namespaces:
<?php declare(strict_types = 1);
-namespace App\Contracts;
+namespace App\Legacy;
-interface Logger
+interface LegacyLogger
{
public function log(string $message): void;
}
How to ignore this error #
You can use the identifier interface.duplicate to ignore this error using a comment:
// @phpstan-ignore interface.duplicate
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.duplicate
Rules that report this error #
- PHPStan\Rules\Classes\DuplicateClassDeclarationRule [1]