diff --git a/docs/running_psalm/issues/NoEnumProperties.md b/docs/running_psalm/issues/NoEnumProperties.md new file mode 100644 index 000000000..fbdff2e08 --- /dev/null +++ b/docs/running_psalm/issues/NoEnumProperties.md @@ -0,0 +1,12 @@ +# NoEnumProperties + +Emitted when there a property defined on an enum, as PHP +does not allow user-defined properties on enums. + +```php +props as $prop) { + if ($storage->is_enum) { + if (IssueBuffer::accepts(new NoEnumProperties( + 'Enums cannot have properties', + new CodeLocation($this, $prop), + $fq_class_name + ))) { + // fall through + } + continue; + } if ($prop->default) { $member_stmts[] = $stmt; } @@ -525,7 +536,7 @@ class ClassAnalyzer extends ClassLikeAnalyzer } foreach ($class->stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\Property && !isset($stmt->type)) { + if ($stmt instanceof PhpParser\Node\Stmt\Property && !$storage->is_enum && !isset($stmt->type)) { $this->checkForMissingPropertyType($this, $stmt, $class_context); } elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) { foreach ($stmt->traits as $trait) { diff --git a/src/Psalm/Issue/NoEnumProperties.php b/src/Psalm/Issue/NoEnumProperties.php new file mode 100644 index 000000000..825c65420 --- /dev/null +++ b/src/Psalm/Issue/NoEnumProperties.php @@ -0,0 +1,9 @@ +