Skip to content

Commit 58f4af1

Browse files
committed
Generated types
1 parent 1d235e7 commit 58f4af1

File tree

2 files changed

+190
-7
lines changed

2 files changed

+190
-7
lines changed

generator/Generator.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public static function generate(): void
2727
{
2828
require_once __DIR__ . '/../vendor/autoload.php';
2929
self::cleanUp();
30-
self::generateVisitor();
3130
self::generateTypes();
31+
self::generateVisitor();
32+
self::generateDefaultVisitor();
3233
}
3334

3435
private static function cleanUp(): void
@@ -43,6 +44,17 @@ private static function cleanUp(): void
4344
}
4445
}
4546

47+
private static function generateTypes(): void
48+
{
49+
foreach (self::types() as $type) {
50+
if ($type->properties === []) {
51+
self::writeClass(self::generateAtomicType($type));
52+
} else {
53+
self::writeClass(self::generateComplexType($type));
54+
}
55+
}
56+
}
57+
4658
private static function generateVisitor(): void
4759
{
4860
$visitor = (new InterfaceType('TypeVisitor'))
@@ -60,15 +72,31 @@ private static function generateVisitor(): void
6072
self::writeClass($visitor);
6173
}
6274

63-
private static function generateTypes(): void
75+
private static function generateDefaultVisitor(): void
6476
{
77+
$visitor = (new ClassType('DefaultTypeVisitor'))
78+
->setAbstract()
79+
->addImplement(TypeVisitor::class)
80+
->setComment(self::GENERATED_NOTICE . "\n@api\n@template-covariant TResult\n@implements TypeVisitor<TResult>");
81+
6582
foreach (self::types() as $type) {
66-
if ($type->properties === []) {
67-
self::writeClass(self::generateAtomicType($type));
68-
} else {
69-
self::writeClass(self::generateComplexType($type));
70-
}
83+
$visitor
84+
->addMethod($type->name)
85+
->setPublic()
86+
->setReturnType('mixed')
87+
->setBody('return $this->default($type);')
88+
->addParameter('type')->setType($type->className());
7189
}
90+
91+
$visitor
92+
->addMethod('default')
93+
->setPublic()
94+
->setAbstract()
95+
->setReturnType('mixed')
96+
->setComment('@return TResult')
97+
->addParameter('type')->setType(Type::class);
98+
99+
self::writeClass($visitor);
72100
}
73101

74102
private static function generateAtomicType(TypeSpec $type): EnumType

src/DefaultTypeVisitor.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typhoon\Type;
6+
7+
/**
8+
* This code is generated, do not edit it.
9+
*
10+
* @api
11+
* @template-covariant TResult
12+
* @implements TypeVisitor<TResult>
13+
*/
14+
abstract class DefaultTypeVisitor implements TypeVisitor
15+
{
16+
public function never(NeverT $type): mixed
17+
{
18+
return $this->default($type);
19+
}
20+
21+
public function void(VoidT $type): mixed
22+
{
23+
return $this->default($type);
24+
}
25+
26+
public function null(NullT $type): mixed
27+
{
28+
return $this->default($type);
29+
}
30+
31+
public function false(FalseT $type): mixed
32+
{
33+
return $this->default($type);
34+
}
35+
36+
public function true(TrueT $type): mixed
37+
{
38+
return $this->default($type);
39+
}
40+
41+
public function intRange(IntRangeT $type): mixed
42+
{
43+
return $this->default($type);
44+
}
45+
46+
public function floatRange(FloatRangeT $type): mixed
47+
{
48+
return $this->default($type);
49+
}
50+
51+
public function stringValue(StringValueT $type): mixed
52+
{
53+
return $this->default($type);
54+
}
55+
56+
public function string(StringT $type): mixed
57+
{
58+
return $this->default($type);
59+
}
60+
61+
public function resource(ResourceT $type): mixed
62+
{
63+
return $this->default($type);
64+
}
65+
66+
public function union(UnionT $type): mixed
67+
{
68+
return $this->default($type);
69+
}
70+
71+
public function intersection(IntersectionT $type): mixed
72+
{
73+
return $this->default($type);
74+
}
75+
76+
public function diff(DiffT $type): mixed
77+
{
78+
return $this->default($type);
79+
}
80+
81+
public function list(ListT $type): mixed
82+
{
83+
return $this->default($type);
84+
}
85+
86+
public function array(ArrayT $type): mixed
87+
{
88+
return $this->default($type);
89+
}
90+
91+
public function classString(ClassStringT $type): mixed
92+
{
93+
return $this->default($type);
94+
}
95+
96+
public function object(ObjectT $type): mixed
97+
{
98+
return $this->default($type);
99+
}
100+
101+
public function callable(CallableT $type): mixed
102+
{
103+
return $this->default($type);
104+
}
105+
106+
public function constant(ConstantT $type): mixed
107+
{
108+
return $this->default($type);
109+
}
110+
111+
public function classConstant(ClassConstantT $type): mixed
112+
{
113+
return $this->default($type);
114+
}
115+
116+
public function classConstantMask(ClassConstantMaskT $type): mixed
117+
{
118+
return $this->default($type);
119+
}
120+
121+
public function namedObject(NamedObjectT $type): mixed
122+
{
123+
return $this->default($type);
124+
}
125+
126+
public function alias(AliasT $type): mixed
127+
{
128+
return $this->default($type);
129+
}
130+
131+
public function self(SelfT $type): mixed
132+
{
133+
return $this->default($type);
134+
}
135+
136+
public function parent(ParentT $type): mixed
137+
{
138+
return $this->default($type);
139+
}
140+
141+
public function static(StaticT $type): mixed
142+
{
143+
return $this->default($type);
144+
}
145+
146+
public function template(TemplateT $type): mixed
147+
{
148+
return $this->default($type);
149+
}
150+
151+
/**
152+
* @return TResult
153+
*/
154+
abstract public function default(Type $type): mixed;
155+
}

0 commit comments

Comments
 (0)