1515use OpenCodeModeling \CodeAst \NodeVisitor \ClassFile ;
1616use OpenCodeModeling \CodeAst \NodeVisitor \ClassImplements ;
1717use OpenCodeModeling \CodeAst \NodeVisitor \ClassNamespace ;
18+ use OpenCodeModeling \CodeAst \NodeVisitor \ClassUseTrait ;
1819use OpenCodeModeling \CodeAst \NodeVisitor \NamespaceUse ;
1920use OpenCodeModeling \CodeAst \NodeVisitor \StrictType ;
2021use PhpParser \Node ;
@@ -50,6 +51,9 @@ final class ClassBuilder
5051 /** @var string[] */
5152 private $ namespaceUse = [];
5253
54+ /** @var string[] */
55+ private $ traits = [];
56+
5357 /** @var ClassConstBuilder[] */
5458 private $ constants = [];
5559
@@ -125,6 +129,13 @@ public function setNamespaceUse(string ...$namespaces): self
125129 return $ this ;
126130 }
127131
132+ public function setUseTrait (string ...$ traits ): self
133+ {
134+ $ this ->traits = $ traits ;
135+
136+ return $ this ;
137+ }
138+
128139 public function setConstants (ClassConstBuilder ...$ constants ): self
129140 {
130141 $ this ->constants = $ constants ;
@@ -183,6 +194,14 @@ public function getNamespaceUse(): array
183194 return $ this ->namespaceUse ;
184195 }
185196
197+ /**
198+ * @return string[]
199+ */
200+ public function getUseTrait (): array
201+ {
202+ return $ this ->traits ;
203+ }
204+
186205 /**
187206 * @return ClassConstBuilder[]
188207 */
@@ -218,6 +237,10 @@ public function generate(): array
218237 if ($ this ->implements ) {
219238 $ visitors [] = new ClassImplements (...$ this ->implements );
220239 }
240+ if ($ this ->traits ) {
241+ $ visitors [] = new ClassUseTrait (...$ this ->traits );
242+ }
243+
221244 if (\count ($ this ->constants ) > 0 ) {
222245 \array_push (
223246 $ visitors ,
@@ -254,12 +277,19 @@ private function unpackNode(Node $node): void
254277 }
255278 break ;
256279 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 ();
258283 break ;
259284 case $ node instanceof Node \Stmt \Class_:
260285 $ this ->name = $ node ->name ->name ;
261286 $ 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+ }
263293
264294 foreach ($ node ->stmts as $ stmt ) {
265295 $ this ->unpackNode ($ stmt );
@@ -273,6 +303,16 @@ static function (Node\Name $name) {
273303 $ node ->implements
274304 );
275305 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 ;
276316 case $ node instanceof Node \Stmt \ClassConst:
277317 $ this ->constants [] = ClassConstBuilder::fromNode ($ node );
278318 break ;
0 commit comments