15
15
use OpenCodeModeling \CodeAst \NodeVisitor \ClassFile ;
16
16
use OpenCodeModeling \CodeAst \NodeVisitor \ClassImplements ;
17
17
use OpenCodeModeling \CodeAst \NodeVisitor \ClassNamespace ;
18
+ use OpenCodeModeling \CodeAst \NodeVisitor \ClassUseTrait ;
18
19
use OpenCodeModeling \CodeAst \NodeVisitor \NamespaceUse ;
19
20
use OpenCodeModeling \CodeAst \NodeVisitor \StrictType ;
20
21
use PhpParser \Node ;
@@ -50,6 +51,9 @@ final class ClassBuilder
50
51
/** @var string[] */
51
52
private $ namespaceUse = [];
52
53
54
+ /** @var string[] */
55
+ private $ traits = [];
56
+
53
57
/** @var ClassConstBuilder[] */
54
58
private $ constants = [];
55
59
@@ -125,6 +129,13 @@ public function setNamespaceUse(string ...$namespaces): self
125
129
return $ this ;
126
130
}
127
131
132
+ public function setUseTrait (string ...$ traits ): self
133
+ {
134
+ $ this ->traits = $ traits ;
135
+
136
+ return $ this ;
137
+ }
138
+
128
139
public function setConstants (ClassConstBuilder ...$ constants ): self
129
140
{
130
141
$ this ->constants = $ constants ;
@@ -183,6 +194,14 @@ public function getNamespaceUse(): array
183
194
return $ this ->namespaceUse ;
184
195
}
185
196
197
+ /**
198
+ * @return string[]
199
+ */
200
+ public function getUseTrait (): array
201
+ {
202
+ return $ this ->traits ;
203
+ }
204
+
186
205
/**
187
206
* @return ClassConstBuilder[]
188
207
*/
@@ -218,6 +237,10 @@ public function generate(): array
218
237
if ($ this ->implements ) {
219
238
$ visitors [] = new ClassImplements (...$ this ->implements );
220
239
}
240
+ if ($ this ->traits ) {
241
+ $ visitors [] = new ClassUseTrait (...$ this ->traits );
242
+ }
243
+
221
244
if (\count ($ this ->constants ) > 0 ) {
222
245
\array_push (
223
246
$ visitors ,
@@ -254,12 +277,19 @@ private function unpackNode(Node $node): void
254
277
}
255
278
break ;
256
279
case $ node instanceof Node \Stmt \UseUse:
257
- $ this ->namespaceUse [] = $ node ->name ->toString ();
280
+ $ this ->namespaceUse [] = $ node ->name instanceof Node \Name \FullyQualified
281
+ ? '\\' . $ node ->name ->toString ()
282
+ : $ node ->name ->toString ();
258
283
break ;
259
284
case $ node instanceof Node \Stmt \Class_:
260
285
$ this ->name = $ node ->name ->name ;
261
286
$ this ->final = $ node ->isFinal ();
262
- $ this ->extends = $ node ->extends ? $ node ->extends ->toString () : null ;
287
+
288
+ if ($ node ->extends !== null ) {
289
+ $ this ->extends = $ node ->extends instanceof Node \Name \FullyQualified
290
+ ? '\\' . $ node ->extends ->toString ()
291
+ : $ node ->extends ->toString ();
292
+ }
263
293
264
294
foreach ($ node ->stmts as $ stmt ) {
265
295
$ this ->unpackNode ($ stmt );
@@ -273,6 +303,16 @@ static function (Node\Name $name) {
273
303
$ node ->implements
274
304
);
275
305
break ;
306
+ case $ node instanceof Node \Stmt \TraitUse:
307
+ $ this ->traits = \array_map (
308
+ static function (Node \Name $ name ) {
309
+ return $ name instanceof Node \Name \FullyQualified
310
+ ? '\\' . $ name ->toString ()
311
+ : $ name ->toString ();
312
+ },
313
+ $ node ->traits
314
+ );
315
+ break ;
276
316
case $ node instanceof Node \Stmt \ClassConst:
277
317
$ this ->constants [] = ClassConstBuilder::fromNode ($ node );
278
318
break ;
0 commit comments