1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Forbid properties on enums

Fixes vimeo/psalm#6472
This commit is contained in:
Bruce Weirdan 2021-11-06 21:57:49 +02:00
parent e42bc4a23f
commit 0f8ef45110
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
3 changed files with 33 additions and 1 deletions

View File

@ -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
<?php
enum Status {
public $prop;
}
```

View File

@ -35,6 +35,7 @@ use Psalm\Issue\MissingImmutableAnnotation;
use Psalm\Issue\MissingPropertyType;
use Psalm\Issue\MissingTemplateParam;
use Psalm\Issue\MutableDependency;
use Psalm\Issue\NoEnumProperties;
use Psalm\Issue\NonInvariantDocblockPropertyType;
use Psalm\Issue\NonInvariantPropertyType;
use Psalm\Issue\OverriddenPropertyAccess;
@ -430,6 +431,16 @@ class ClassAnalyzer extends ClassLikeAnalyzer
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
foreach ($stmt->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) {

View File

@ -0,0 +1,9 @@
<?php
namespace Psalm\Issue;
class NoEnumProperties extends ClassIssue
{
public const ERROR_LEVEL = -1;
public const SHORTCODE = 301;
}