Skip to content

Commit

Permalink
fix: properly handle full namespaced enum type in docblock
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Jan 25, 2025
1 parent 716c8b3 commit eb8816c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/Type/Parser/Exception/Enum/EnumCaseNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use CuyZ\Valinor\Type\Parser\Exception\InvalidType;
use RuntimeException;
use UnitEnum;

/** @internal */
final class EnumCaseNotFound extends RuntimeException implements InvalidType
{
/**
* @param class-string<UnitEnum> $enumName
* @param class-string $enumName
*/
public function __construct(string $enumName, string $pattern)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Types/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ final class EnumType implements ClassType
*/
public function __construct(string $enumName, string $pattern, array $cases)
{
$this->enumName = $enumName;
// @phpstan-ignore assign.propertyType (it is still an enum class-string)
$this->enumName = ltrim($enumName, '\\');
$this->pattern = $pattern;

if (empty($cases)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Integration/Mapping/Object/EnumValuesMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function test_values_are_mapped_properly(): void
$source = [
'pureEnumWithFirstValue' => 'FOO',
'pureEnumWithSecondValue' => 'BAR',
'pureEnumWithFullNamespace' => 'FOO',
'pureEnumWithPattern' => 'BAZ',
'backedStringEnum' => 'foo',
'backedStringEnumWithPattern' => 'baz',
Expand All @@ -34,6 +35,7 @@ public function test_values_are_mapped_properly(): void

self::assertSame(PureEnum::FOO, $result->pureEnumWithFirstValue);
self::assertSame(PureEnum::BAR, $result->pureEnumWithSecondValue);
self::assertSame(PureEnum::FOO, $result->pureEnumWithFullNamespace);
self::assertSame(PureEnum::BAZ, $result->pureEnumWithPattern);
self::assertSame(BackedStringEnum::FOO, $result->backedStringEnum);
self::assertSame(BackedStringEnum::BAZ, $result->backedStringEnumWithPattern);
Expand Down Expand Up @@ -115,6 +117,9 @@ class EnumValues

public PureEnum $pureEnumWithSecondValue;

/** @var \CuyZ\Valinor\Tests\Fixture\Enum\PureEnum */
public mixed $pureEnumWithFullNamespace;

/** @var PureEnum::BA* */
public PureEnum $pureEnumWithPattern;

Expand All @@ -132,13 +137,15 @@ class EnumValues
class EnumValuesWithConstructor extends EnumValues
{
/**
* @param \CuyZ\Valinor\Tests\Fixture\Enum\PureEnum $pureEnumWithFullNamespace
* @param PureEnum::BA* $pureEnumWithPattern
* @param BackedStringEnum::BA* $backedStringEnumWithPattern
* @param BackedIntegerEnum::BA* $backedIntegerEnumWithPattern
*/
public function __construct(
PureEnum $pureEnumWithFirstValue,
PureEnum $pureEnumWithSecondValue,
mixed $pureEnumWithFullNamespace,
PureEnum $pureEnumWithPattern,
BackedStringEnum $backedStringEnum,
BackedStringEnum $backedStringEnumWithPattern,
Expand All @@ -147,6 +154,7 @@ public function __construct(
) {
$this->pureEnumWithFirstValue = $pureEnumWithFirstValue;
$this->pureEnumWithSecondValue = $pureEnumWithSecondValue;
$this->pureEnumWithFullNamespace = $pureEnumWithFullNamespace;
$this->pureEnumWithPattern = $pureEnumWithPattern;
$this->backedStringEnum = $backedStringEnum;
$this->backedIntegerEnum = $backedIntegerEnum;
Expand Down

0 comments on commit eb8816c

Please sign in to comment.