Menu
Error Identifier: new.enum
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);
enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
$suit = new Suit(); // error: Cannot instantiate enum Suit.
Why is it reported? #
Enums in PHP cannot be instantiated using the new keyword. Enum cases are predefined singleton instances and must be accessed directly via their case names (e.g., Suit::Hearts). Attempting to use new on an enum will result in a fatal error at runtime.
How to fix it #
Use the enum case directly instead of trying to instantiate it.
-$suit = new Suit();
+$suit = Suit::Hearts;
How to ignore this error #
You can use the identifier new.enum to ignore this error using a comment:
// @phpstan-ignore new.enum
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: new.enum
Rules that report this error #
- PHPStan\Rules\Classes\InstantiationRule [1]