Skip to content

Commit

Permalink
Correctly identify referenced enum values in enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AegirLeet committed Feb 2, 2024
1 parent e18a357 commit 8b754be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,10 @@ private function visitEnumDeclaration(
$fq_classlike_name,
);

if ($case_type && $case_type->isSingleEnumCase()) {
$case_type = Type\Atomic\TValueOf::getValueType($case_type, $this->codebase);
}

if ($case_type) {
if ($case_type->isSingleIntLiteral()) {
$enum_value = $case_type->getSingleIntLiteral()->value;
Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Type/MutableUnion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
Expand Down Expand Up @@ -174,6 +175,11 @@ final class MutableUnion implements TypeNode
*/
private array $literal_float_types = [];

/**
* @var array<string, TEnumCase>
*/
private array $enum_case_types = [];

/**
* True if the type was passed or returned by reference, or if the type refers to an object's
* property or an item in an array. Note that this is not true for locally created references
Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Internal\TypeVisitor\FromDocblockSetter;
use Psalm\Storage\ImmutableNonCloneableTrait;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
Expand Down Expand Up @@ -186,6 +187,11 @@ final class Union implements TypeNode
*/
private array $literal_float_types = [];

/**
* @var array<string, TEnumCase>
*/
private array $enum_case_types = [];

/**
* True if the type was passed or returned by reference, or if the type refers to an object's
* property or an item in an array. Note that this is not true for locally created references
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Type/UnionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Atomic\TConditional;
use Psalm\Type\Atomic\TEmptyMixed;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
Expand Down Expand Up @@ -97,6 +98,8 @@ public function __construct(array $types, array $properties = [])
&& ($type->as_type || $type instanceof TTemplateParamClass)
) {
$this->typed_class_strings[$key] = $type;
} elseif ($type instanceof TEnumCase) {
$this->enum_case_types[$key] = $type;
} elseif ($type instanceof TNever) {
$this->explicit_never = true;
}
Expand Down Expand Up @@ -1558,4 +1561,9 @@ public function visit(TypeVisitor $visitor): bool

return true;
}

public function isSingleEnumCase(): bool
{
return count($this->types) === 1 && count($this->enum_case_types) === 1;
}
}

0 comments on commit 8b754be

Please sign in to comment.