Skip to content

Commit 96e1289

Browse files
committed
Add RecursiveTypeReplacer
1 parent d4cb917 commit 96e1289

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typhoon\Type\Visitor;
6+
7+
use Typhoon\Type\AliasT;
8+
use Typhoon\Type\ArrayElement;
9+
use Typhoon\Type\ArrayT;
10+
use Typhoon\Type\CallableT;
11+
use Typhoon\Type\ClassConstantMaskT;
12+
use Typhoon\Type\ClassConstantT;
13+
use Typhoon\Type\ClassStringT;
14+
use Typhoon\Type\ConstantT;
15+
use Typhoon\Type\DiffT;
16+
use Typhoon\Type\FalseT;
17+
use Typhoon\Type\FloatRangeT;
18+
use Typhoon\Type\IntersectionT;
19+
use Typhoon\Type\IntRangeT;
20+
use Typhoon\Type\ListT;
21+
use Typhoon\Type\NamedObjectT;
22+
use Typhoon\Type\NeverT;
23+
use Typhoon\Type\NullT;
24+
use Typhoon\Type\ObjectT;
25+
use Typhoon\Type\Parameter;
26+
use Typhoon\Type\ParentT;
27+
use Typhoon\Type\Property;
28+
use Typhoon\Type\ResourceT;
29+
use Typhoon\Type\SelfT;
30+
use Typhoon\Type\StaticT;
31+
use Typhoon\Type\StringT;
32+
use Typhoon\Type\StringValueT;
33+
use Typhoon\Type\TemplateT;
34+
use Typhoon\Type\TrueT;
35+
use Typhoon\Type\Type;
36+
use Typhoon\Type\TypeVisitor;
37+
use Typhoon\Type\UnionT;
38+
use Typhoon\Type\VoidT;
39+
40+
/**
41+
* @api
42+
* @implements TypeVisitor<Type>
43+
*/
44+
abstract class RecursiveTypeReplacer implements TypeVisitor
45+
{
46+
public function never(NeverT $type): mixed
47+
{
48+
return $type;
49+
}
50+
51+
public function void(VoidT $type): mixed
52+
{
53+
return $type;
54+
}
55+
56+
public function null(NullT $type): mixed
57+
{
58+
return $type;
59+
}
60+
61+
public function false(FalseT $type): mixed
62+
{
63+
return $type;
64+
}
65+
66+
public function true(TrueT $type): mixed
67+
{
68+
return $type;
69+
}
70+
71+
public function intRange(IntRangeT $type): mixed
72+
{
73+
return $type;
74+
}
75+
76+
public function floatRange(FloatRangeT $type): mixed
77+
{
78+
return $type;
79+
}
80+
81+
public function stringValue(StringValueT $type): mixed
82+
{
83+
return $type;
84+
}
85+
86+
public function string(StringT $type): mixed
87+
{
88+
return $type;
89+
}
90+
91+
public function resource(ResourceT $type): mixed
92+
{
93+
return $type;
94+
}
95+
96+
public function union(UnionT $type): mixed
97+
{
98+
return new UnionT(array_map(
99+
fn(Type $type): Type => $type->accept($this),
100+
$type->types,
101+
));
102+
}
103+
104+
public function intersection(IntersectionT $type): mixed
105+
{
106+
return new IntersectionT(array_map(
107+
fn(Type $type): Type => $type->accept($this),
108+
$type->types,
109+
));
110+
}
111+
112+
public function diff(DiffT $type): mixed
113+
{
114+
return new DiffT(
115+
minuend: $type->minuend->accept($this),
116+
subtrahend: $type->subtrahend->accept($this),
117+
);
118+
}
119+
120+
public function list(ListT $type): mixed
121+
{
122+
return new ListT(
123+
valueType: $type->valueType->accept($this),
124+
elements: array_map(
125+
fn(ArrayElement $element): ArrayElement => new ArrayElement(
126+
type: $element->type->accept($this),
127+
optional: $element->optional,
128+
),
129+
$type->elements,
130+
),
131+
);
132+
}
133+
134+
public function array(ArrayT $type): mixed
135+
{
136+
return new ArrayT(
137+
keyType: $type->keyType->accept($this),
138+
valueType: $type->valueType->accept($this),
139+
elements: array_map(
140+
fn(ArrayElement $element): ArrayElement => new ArrayElement(
141+
type: $element->type->accept($this),
142+
optional: $element->optional,
143+
),
144+
$type->elements,
145+
),
146+
);
147+
}
148+
149+
public function classString(ClassStringT $type): mixed
150+
{
151+
return new ClassStringT($type->accept($this));
152+
}
153+
154+
public function object(ObjectT $type): mixed
155+
{
156+
return new ObjectT(
157+
properties: array_map(
158+
fn(Property $property): Property => new Property(
159+
type: $property->type->accept($this),
160+
optional: $property->optional,
161+
),
162+
$type->properties,
163+
),
164+
);
165+
}
166+
167+
public function callable(CallableT $type): mixed
168+
{
169+
return new CallableT(
170+
templates: array_map(
171+
fn(TemplateT $template): TemplateT => new TemplateT(
172+
name: $template->name,
173+
variance: $template->variance,
174+
upperBound: $template->upperBound->accept($this),
175+
),
176+
$type->templates,
177+
),
178+
parameters: array_map(
179+
fn(Parameter $parameter): Parameter => new Parameter(
180+
type: $parameter->type->accept($this),
181+
hasDefault: $parameter->hasDefault,
182+
variadic: $parameter->variadic,
183+
byReference: $parameter->byReference,
184+
),
185+
$type->parameters,
186+
),
187+
returnType: $type->returnType->accept($this),
188+
);
189+
}
190+
191+
public function constant(ConstantT $type): mixed
192+
{
193+
return $type;
194+
}
195+
196+
public function classConstant(ClassConstantT $type): mixed
197+
{
198+
return new ClassConstantT(
199+
objectType: $type->objectType->accept($this),
200+
name: $type->name,
201+
);
202+
}
203+
204+
public function classConstantMask(ClassConstantMaskT $type): mixed
205+
{
206+
return new ClassConstantMaskT(
207+
objectType: $type->objectType->accept($this),
208+
namePrefix: $type->namePrefix,
209+
);
210+
}
211+
212+
public function namedObject(NamedObjectT $type): mixed
213+
{
214+
return new NamedObjectT(
215+
name: $type->name,
216+
templateArguments: array_map(
217+
fn(Type $type): Type => $type->accept($this),
218+
$type->templateArguments,
219+
),
220+
);
221+
}
222+
223+
public function alias(AliasT $type): mixed
224+
{
225+
return new AliasT(
226+
classType: $type->classType->accept($this),
227+
name: $type->name,
228+
templateArguments: array_map(
229+
fn(Type $type): Type => $type->accept($this),
230+
$type->templateArguments,
231+
),
232+
);
233+
}
234+
235+
public function self(SelfT $type): mixed
236+
{
237+
return new SelfT(
238+
resolvedObjectType: $type->resolvedObjectType?->accept($this),
239+
templateArguments: array_map(
240+
fn(Type $type): Type => $type->accept($this),
241+
$type->templateArguments,
242+
),
243+
);
244+
}
245+
246+
public function parent(ParentT $type): mixed
247+
{
248+
return new SelfT(
249+
resolvedObjectType: $type->resolvedObjectType?->accept($this),
250+
templateArguments: array_map(
251+
fn(Type $type): Type => $type->accept($this),
252+
$type->templateArguments,
253+
),
254+
);
255+
}
256+
257+
public function static(StaticT $type): mixed
258+
{
259+
return new StaticT(
260+
resolvedObjectType: $type->resolvedObjectType?->accept($this),
261+
templateArguments: array_map(
262+
fn(Type $type): Type => $type->accept($this),
263+
$type->templateArguments,
264+
),
265+
);
266+
}
267+
268+
public function template(TemplateT $type): mixed
269+
{
270+
return new TemplateT(
271+
name: $type->name,
272+
variance: $type->variance,
273+
upperBound: $type->upperBound->accept($this),
274+
);
275+
}
276+
}

0 commit comments

Comments
 (0)