Error Identifier: enum.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);
// file1.php
namespace App;
enum Status
{
case Active;
case Inactive;
}
// file2.php
namespace App;
enum Status
{
case Pending;
case Completed;
}
Why is it reported? #
The same enum name is declared multiple times within the same namespace across different files. PHP does not allow two types with the same fully qualified name. When an enum is declared more than once, PHP will throw a fatal error at runtime. This usually happens due to copy-paste mistakes, file inclusion issues, or when two files define the same enum and are both included in the project.
How to fix it #
Remove the duplicate enum declaration, keeping only one:
<?php declare(strict_types = 1);
// file2.php
namespace App;
-enum Status
-{
- case Pending;
- case Completed;
-}
If both declarations are intentionally different, rename one of them:
<?php declare(strict_types = 1);
// file2.php
namespace App;
-enum Status
+enum TaskStatus
{
case Pending;
case Completed;
}
How to ignore this error #
You can use the identifier enum.duplicate to ignore this error using a comment:
// @phpstan-ignore enum.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: enum.duplicate
Rules that report this error #
- PHPStan\Rules\Classes\DuplicateClassDeclarationRule [1]