Error Identifier: traitUse.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 Color: string
{
case Red = 'red';
case Green = 'green';
case Blue = 'blue';
}
class Palette
{
use Color;
}
Why is it reported? #
The use statement inside a class body is attempting to use an enum as if it were a trait. The use keyword in a class body is reserved exclusively for traits. Enums cannot be used with the use statement. This code will result in a fatal error at runtime.
How to fix it #
If the intent is to use the enum type, reference it as a property type or parameter type instead:
<?php declare(strict_types = 1);
class Palette
{
- use Color;
+ /** @var list<Color> */
+ private array $colors = [];
+
+ public function addColor(Color $color): void
+ {
+ $this->colors[] = $color;
+ }
}
If the intent is to share methods across classes, extract a trait:
<?php declare(strict_types = 1);
+trait ColorHelpers
+{
+ public function getDefaultColor(): Color
+ {
+ return Color::Red;
+ }
+}
+
class Palette
{
- use Color;
+ use ColorHelpers;
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Classes\ExistingClassInTraitUseRule [1]