Skip to content

Commit 5a33cc5

Browse files
committed
WIP: Task: add EnumInstanceType::isUnspecified and make getMemberName throw if isUnspecified
1 parent b4c48bc commit 5a33cc5

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

src/TypeSystem/Resolver/Match/MatchTypeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ private function resolveTypeOfEnumMatch(MatchNode $matchNode, EnumInstanceType $
113113
throw new \Error('@TODO Error: incompatible enum match: got ' . $enumMemberType->enumStaticType->enumName . ' expected ' . $subjectEnumType->enumStaticType->enumName);
114114
}
115115

116-
if (isset($referencedEnumMembers[$enumMemberType->memberName])) {
117-
throw new \Error('@TODO Error: Enum path ' . $enumMemberType->memberName . ' was already defined once in this match and cannot be used twice');
116+
if (isset($referencedEnumMembers[$enumMemberType->getMemberName()])) {
117+
throw new \Error('@TODO Error: Enum path ' . $enumMemberType->getMemberName() . ' was already defined once in this match and cannot be used twice');
118118
}
119119

120-
$referencedEnumMembers[$enumMemberType->memberName] = true;
120+
$referencedEnumMembers[$enumMemberType->getMemberName()] = true;
121121
}
122122
}
123123

src/TypeSystem/Type/EnumType/EnumInstanceType.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,39 @@ final class EnumInstanceType implements TypeInterface
2828
{
2929
private function __construct(
3030
public readonly EnumStaticType $enumStaticType,
31-
public readonly ?string $memberName
31+
private readonly ?string $memberName
3232
) {
3333
if ($memberName !== null && !$enumStaticType->hasMember($memberName)) {
34-
throw new \Exception('@TODO cannot access member ' . $memberName . ' of enum ' . $this->enumStaticType->enumName);
34+
throw new \Exception('@TODO cannot access member ' . $memberName . ' of enum ' . $enumStaticType->enumName);
3535
}
3636
}
3737

38-
public static function fromStaticEnumCreateUnspecificInstance(EnumStaticType $enumStaticType): self
38+
public static function createUnspecifiedEnumInstanceType(EnumStaticType $enumStaticType): self
3939
{
4040
return new self(
4141
enumStaticType: $enumStaticType,
4242
memberName: null
4343
);
4444
}
4545

46-
public static function fromStaticEnumAndMemberName(EnumStaticType $enumStaticType, string $enumMemberName): self
46+
public static function fromStaticEnumTypeAndMemberName(EnumStaticType $enumStaticType, string $enumMemberName): self
4747
{
4848
return new self(
4949
enumStaticType: $enumStaticType,
5050
memberName: $enumMemberName
5151
);
5252
}
5353

54+
public function isUnspecified(): bool
55+
{
56+
return $this->memberName === null;
57+
}
58+
59+
public function getMemberName(): string
60+
{
61+
return $this->memberName ?? throw new \Exception('@TODO Error cannot access memberName of unspecified instance');
62+
}
63+
5464
public function is(TypeInterface $other): bool
5565
{
5666
if ($other === $this) {

src/TypeSystem/Type/EnumType/EnumStaticType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function hasMember(string $memberName): bool
6161

6262
public function getMemberType(string $memberName): EnumInstanceType
6363
{
64-
return EnumInstanceType::fromStaticEnumAndMemberName(
64+
return EnumInstanceType::fromStaticEnumTypeAndMemberName(
6565
$this,
6666
$memberName
6767
);
@@ -81,6 +81,6 @@ public function is(TypeInterface $other): bool
8181

8282
public function toEnumInstanceType(): EnumInstanceType
8383
{
84-
return EnumInstanceType::fromStaticEnumCreateUnspecificInstance($this);
84+
return EnumInstanceType::createUnspecifiedEnumInstanceType($this);
8585
}
8686
}

test/Unit/TypeSystem/Resolver/Access/AccessTypeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function enumMemberAccessOnStaticEnum(): void
9090

9191
$this->assertTrue($accessType->enumStaticType->is($someEnum));
9292

93-
$this->assertEquals("A", $accessType->memberName);
93+
$this->assertEquals("A", $accessType->getMemberName());
9494
}
9595

9696
/**

test/Unit/TypeSystem/Type/EnumType/EnumStaticTypeTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function providesMemberType(): void
9595
$this->assertInstanceOf(EnumInstanceType::class, $enumMemberType);
9696

9797
$this->assertSame($enumStaticType, $enumMemberType->enumStaticType);
98-
$this->assertSame('A', $enumMemberType->memberName);
98+
$this->assertSame('A', $enumMemberType->getMemberName());
9999
}
100100

101101
/**
@@ -135,7 +135,7 @@ public function canBeTransformedIntoInstanceType(): void
135135

136136
$this->assertInstanceOf(EnumStaticType::class, $enumInstanceType->enumStaticType);
137137

138-
$this->assertNull($enumInstanceType->memberName);
138+
$this->assertTrue($enumInstanceType->isUnspecified());
139139
}
140140

141141
/**
@@ -152,6 +152,8 @@ public function canBeComparedToOther(): void
152152
$enumDeclarationNode
153153
);
154154

155+
$this->assertTrue($enumStaticType->is($enumStaticType));
156+
155157
$enumStaticType2 = EnumStaticType::fromModuleIdAndDeclaration(
156158
ModuleId::fromString("module-a"),
157159
$enumDeclarationNode

0 commit comments

Comments
 (0)