From 0f8ef45110e0b6b67df21d6302f0ae2c3c4a204f Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sat, 6 Nov 2021 21:57:49 +0200 Subject: [PATCH] Forbid properties on enums Fixes vimeo/psalm#6472 --- docs/running_psalm/issues/NoEnumProperties.md | 12 ++++++++++++ src/Psalm/Internal/Analyzer/ClassAnalyzer.php | 13 ++++++++++++- src/Psalm/Issue/NoEnumProperties.php | 9 +++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 docs/running_psalm/issues/NoEnumProperties.md create mode 100644 src/Psalm/Issue/NoEnumProperties.php 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 @@ +