Skip to content

Commit b310c5a

Browse files
committed
WIP: TASK more satisfied enum->is type check with module id taken into account
previously enums where only compared by name and not import path (module id)
1 parent 11b81e6 commit b310c5a

File tree

12 files changed

+67
-36
lines changed

12 files changed

+67
-36
lines changed

src/Module/Loader/ModuleFile/ModuleFileLoader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace PackageFactory\ComponentEngine\Module\Loader\ModuleFile;
2424

2525
use PackageFactory\ComponentEngine\Module\LoaderInterface;
26+
use PackageFactory\ComponentEngine\Module\ModuleId;
2627
use PackageFactory\ComponentEngine\Parser\Ast\ComponentDeclarationNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2829
use PackageFactory\ComponentEngine\Parser\Ast\ImportNode;
@@ -54,9 +55,14 @@ public function resolveTypeOfImport(ImportNode $importNode): TypeInterface
5455
);
5556
}
5657

58+
$moduleId = ModuleId::fromSource($source);
59+
5760
return match ($export->declaration::class) {
5861
ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration),
59-
EnumDeclarationNode::class => EnumStaticType::fromEnumDeclarationNode($export->declaration),
62+
EnumDeclarationNode::class => EnumStaticType::fromModuleIdAndDeclaration(
63+
$moduleId,
64+
$export->declaration,
65+
),
6066
StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration)
6167
};
6268
}

src/TypeSystem/Type/EnumType/EnumStaticType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ final class EnumStaticType implements TypeInterface
3030

3131
public function toEnumInstanceType(): EnumType
3232
{
33-
return new EnumType($this->enumName, $this->membersWithType);
33+
return new EnumType($this->moduleId, $this->enumName, $this->membersWithType);
3434
}
3535
}

src/TypeSystem/Type/EnumType/EnumTrait.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@
2222

2323
namespace PackageFactory\ComponentEngine\TypeSystem\Type\EnumType;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\NumberLiteralNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\StringLiteralNode;
28-
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
29-
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
29+
use PackageFactory\ComponentEngine\TypeSystem\Resolver\NumberLiteral\NumberLiteralTypeResolver;
30+
use PackageFactory\ComponentEngine\TypeSystem\Resolver\StringLiteral\StringLiteralTypeResolver;
3031
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
3132

3233
trait EnumTrait
3334
{
3435
public function __construct(
36+
public readonly ?ModuleId $moduleId,
3537
public readonly string $enumName,
3638
private readonly array $membersWithType,
3739
) {
3840
}
3941

40-
public static function fromEnumDeclarationNode(EnumDeclarationNode $enumDeclarationNode): self
42+
public static function fromModuleIdAndDeclaration(ModuleId $moduleId, EnumDeclarationNode $enumDeclarationNode): self
4143
{
4244
$membersWithType = [];
4345

@@ -46,13 +48,16 @@ public static function fromEnumDeclarationNode(EnumDeclarationNode $enumDeclarat
4648
? $memberDeclarationNode->value::class
4749
: null
4850
) {
49-
StringLiteralNode::class => StringType::get(),
50-
NumberLiteralNode::class => NumberType::get(),
51+
NumberLiteralNode::class => (new NumberLiteralTypeResolver())
52+
->resolveTypeOf($memberDeclarationNode->value),
53+
StringLiteralNode::class => (new StringLiteralTypeResolver())
54+
->resolveTypeOf($memberDeclarationNode->value),
5155
null => null
5256
};
5357
}
5458

5559
return new self(
60+
moduleId: $moduleId,
5661
enumName: $enumDeclarationNode->enumName,
5762
membersWithType: $membersWithType
5863
);
@@ -77,9 +82,10 @@ public function getMemberType(string $memberName): EnumMemberType
7782

7883
public function is(TypeInterface $other): bool
7984
{
80-
// todo more satisfied check with namespace taken into account
8185
return match ($other::class) {
82-
EnumType::class, EnumStaticType::class => $this->enumName === $other->enumName,
86+
EnumType::class, EnumStaticType::class =>
87+
$this->moduleId === $other->moduleId
88+
&& $this->enumName === $other->enumName,
8389
default => false
8490
};
8591
}

test/Integration/PhpTranspilerIntegrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function testTranspiler(string $example): void
7272

7373
$transpiler = new ModuleTranspiler(
7474
loader: new ModuleFileLoader(),
75-
// Add some assumed types to the global scope
7675
globalScope: GlobalScope::get(),
7776
strategy: new ModuleTestStrategy()
7877
);

test/Unit/Target/Php/Transpiler/Access/AccessTranspilerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\Access;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\AccessNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
@@ -62,7 +63,8 @@ public function transpilesAccessNodes(string $accessAsString, string $expectedTr
6263
'struct A { b: B }'
6364
)
6465
),
65-
'SomeEnum' => EnumStaticType::fromEnumDeclarationNode(
66+
'SomeEnum' => EnumStaticType::fromModuleIdAndDeclaration(
67+
ModuleId::fromString("module-a"),
6668
EnumDeclarationNode::fromString(
6769
'enum SomeEnum { A B C }'
6870
)
@@ -81,4 +83,4 @@ public function transpilesAccessNodes(string $accessAsString, string $expectedTr
8183
$actualTranspilationResult
8284
);
8385
}
84-
}
86+
}

test/Unit/Target/Php/Transpiler/Identifier/IdentifierTranspilerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\Identifier;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\IdentifierNode;
2728
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
@@ -61,7 +62,8 @@ public function transpilesIdentifierNodesReferringToEnums(): void
6162
{
6263
$identifierTranspiler = new IdentifierTranspiler(
6364
scope: new DummyScope([
64-
'SomeEnum' => EnumStaticType::fromEnumDeclarationNode(
65+
'SomeEnum' => EnumStaticType::fromModuleIdAndDeclaration(
66+
ModuleId::fromString("module-a"),
6567
EnumDeclarationNode::fromString(
6668
'enum SomeEnum { A B C }'
6769
)
@@ -80,4 +82,4 @@ public function transpilesIdentifierNodesReferringToEnums(): void
8082
$actualTranspilationResult
8183
);
8284
}
83-
}
85+
}

test/Unit/Target/Php/Transpiler/Match/MatchTranspilerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\Match;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\MatchNode;
@@ -72,7 +73,8 @@ public function transpilesMatchNodes(string $matchAsString, string $expectedTran
7273
{
7374
$matchTranspiler = new MatchTranspiler(
7475
scope: new DummyScope([
75-
'SomeEnum' => EnumStaticType::fromEnumDeclarationNode(
76+
'SomeEnum' => EnumStaticType::fromModuleIdAndDeclaration(
77+
ModuleId::fromString("module-a"),
7678
EnumDeclarationNode::fromString(
7779
'enum SomeEnum { A B C }'
7880
)
@@ -91,4 +93,4 @@ public function transpilesMatchNodes(string $matchAsString, string $expectedTran
9193
$actualTranspilationResult
9294
);
9395
}
94-
}
96+
}

test/Unit/Target/Php/Transpiler/TypeReference/TypeReferenceTranspilerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\TypeReference;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\ComponentDeclarationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\StructDeclarationNode;
@@ -49,7 +50,8 @@ protected function getTypeReferenceTranspiler(): TypeReferenceTranspiler
4950
'Button' => ComponentType::fromComponentDeclarationNode(
5051
ComponentDeclarationNode::fromString('component Button { return "" }')
5152
),
52-
'DayOfWeek' => EnumStaticType::fromEnumDeclarationNode(
53+
'DayOfWeek' => EnumStaticType::fromModuleIdAndDeclaration(
54+
ModuleId::fromString("module-a"),
5355
EnumDeclarationNode::fromString('enum DayOfWeek {}')
5456
),
5557
'Link' => StructType::fromStructDeclarationNode(

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Resolver\Access;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\AccessNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
@@ -69,7 +70,8 @@ private function resolveAccessType(string $accessAsString, ScopeInterface $scope
6970
*/
7071
public function access(): void
7172
{
72-
$someEnum = EnumStaticType::fromEnumDeclarationNode(
73+
$someEnum = EnumStaticType::fromModuleIdAndDeclaration(
74+
ModuleId::fromString("module-a"),
7375
EnumDeclarationNode::fromString(
7476
'enum SomeEnum { A("Hi") }'
7577
)
@@ -101,7 +103,8 @@ public function invalidAccessResultsInError(string $accessAsString, string $expe
101103
{
102104
$this->expectExceptionMessage($expectedErrorMessage);
103105

104-
$someEnum = EnumStaticType::fromEnumDeclarationNode(
106+
$someEnum = EnumStaticType::fromModuleIdAndDeclaration(
107+
ModuleId::fromString("module-a"),
105108
EnumDeclarationNode::fromString(
106109
'enum SomeEnum { A }'
107110
)

test/Unit/TypeSystem/Resolver/Match/MatchTypeResolverTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Resolver\Match;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2627
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
2728
use PackageFactory\ComponentEngine\Parser\Ast\MatchNode;
2829
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
2930
use PackageFactory\ComponentEngine\TypeSystem\Resolver\Match\MatchTypeResolver;
3031
use PackageFactory\ComponentEngine\TypeSystem\Type\BooleanType\BooleanType;
3132
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumStaticType;
32-
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumType;
3333
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
3434
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
3535
use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType;
@@ -92,7 +92,8 @@ public function matchExamples(): array
9292
*/
9393
public function resolvesMatchToResultingType(string $matchAsString, TypeInterface $expectedType): void
9494
{
95-
$someStaticEnumType = EnumStaticType::fromEnumDeclarationNode(
95+
$someStaticEnumType = EnumStaticType::fromModuleIdAndDeclaration(
96+
ModuleId::fromString("module-a"),
9697
EnumDeclarationNode::fromString(
9798
'enum SomeEnum { A B C }'
9899
)
@@ -196,22 +197,20 @@ public function malformedEnumExamples(): iterable
196197
* @dataProvider malformedEnumExamples
197198
* @test
198199
*/
199-
public function malformedMatchCannotBeResolved(string $matchAsString, string $expectedErrorMessage)
200+
public function malformedMatchCannotBeResolved(string $matchAsString, string $expectedErrorMessage): void
200201
{
201202
$this->expectExceptionMessage($expectedErrorMessage);
202-
$someEnumDeclaration = EnumDeclarationNode::fromString(
203-
'enum SomeEnum { A B C }'
204-
);
205-
$someEnumType = EnumType::fromEnumDeclarationNode(
206-
$someEnumDeclaration
207-
);
208-
$someStaticEnumType = EnumStaticType::fromEnumDeclarationNode(
209-
$someEnumDeclaration
203+
$someStaticEnumType = EnumStaticType::fromModuleIdAndDeclaration(
204+
ModuleId::fromString("module-a"),
205+
EnumDeclarationNode::fromString(
206+
'enum SomeEnum { A B C }'
207+
)
210208
);
211209
$scope = new DummyScope([
212-
'someEnumValue' => $someEnumType,
213210
'SomeEnum' => $someStaticEnumType,
214-
'OtherEnum' => EnumStaticType::fromEnumDeclarationNode(
211+
'someEnumValue' => $someStaticEnumType->toEnumInstanceType(),
212+
'OtherEnum' => EnumStaticType::fromModuleIdAndDeclaration(
213+
ModuleId::fromString("module-a"),
215214
EnumDeclarationNode::fromString('enum OtherEnum { A }')
216215
)
217216

0 commit comments

Comments
 (0)