@@ -50,6 +50,7 @@ private function parseCode(string $code): void
5050 $ stmts = $ parser ->parse ($ this ->code );
5151
5252 $ traverser = new PhpParser \NodeTraverser ;
53+ $ traverser ->addVisitor (new PhpParser \NodeVisitor \ParentConnectingVisitor );
5354 $ traverser ->addVisitor (new PhpParser \NodeVisitor \NameResolver (null , ['preserveOriginalNames ' => true ]));
5455 $ this ->statements = $ traverser ->traverse ($ stmts );
5556 }
@@ -67,7 +68,7 @@ public function extractMethodBodies(string $className): array
6768 foreach ($ nodeFinder ->findInstanceOf ($ classNode , Node \Stmt \ClassMethod::class) as $ methodNode ) {
6869 /** @var Node\Stmt\ClassMethod $methodNode */
6970 if ($ methodNode ->stmts ) {
70- $ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedBody ($ methodNode ->stmts , 2 );
71+ $ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedContents ($ methodNode ->stmts , 2 );
7172 }
7273 }
7374 return $ res ;
@@ -81,12 +82,12 @@ public function extractFunctionBody(string $name): ?string
8182 return $ node instanceof Node \Stmt \Function_ && $ node ->namespacedName ->toString () === $ name ;
8283 });
8384
84- return $ this ->getReformattedBody ($ functionNode ->stmts , 1 );
85+ return $ this ->getReformattedContents ($ functionNode ->stmts , 1 );
8586 }
8687
8788
8889 /** @param Node[] $statements */
89- private function getReformattedBody (array $ statements , int $ level ): string
90+ private function getReformattedContents (array $ statements , int $ level ): string
9091 {
9192 $ body = $ this ->getNodeContents (...$ statements );
9293 $ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ statements ));
@@ -101,10 +102,13 @@ private function prepareReplacements(array $statements): array
101102 (new NodeFinder )->find ($ statements , function (Node $ node ) use (&$ replacements , $ start ) {
102103 if ($ node instanceof Node \Name \FullyQualified) {
103104 if ($ node ->getAttribute ('originalName ' ) instanceof Node \Name) {
105+ $ type = $ node ->getAttribute ('parent ' ) instanceof Node \Expr \ConstFetch
106+ ? PhpNamespace::NAME_CONSTANT
107+ : ($ node ->getAttribute ('parent ' ) instanceof Node \Expr \FuncCall ? PhpNamespace::NAME_FUNCTION : PhpNamespace::NAME_NORMAL );
104108 $ replacements [] = [
105109 $ node ->getStartFilePos () - $ start ,
106110 $ node ->getEndFilePos () - $ start ,
107- $ node ->toCodeString (),
111+ Helpers:: tagName ( $ node ->toCodeString (), $ type ),
108112 ];
109113 }
110114
@@ -302,7 +306,7 @@ private function addPropertyToClass(ClassType $class, Node\Stmt\Property $node):
302306 }
303307 $ prop ->setType ($ node ->type ? $ this ->toPhp ($ node ->type ) : null );
304308 if ($ item ->default ) {
305- $ prop ->setValue (new Literal ($ this ->toPhp ( $ item ->default )));
309+ $ prop ->setValue (new Literal ($ this ->getReformattedContents ([ $ item ->default ], 1 )));
306310 }
307311 $ prop ->setReadOnly (method_exists ($ node , 'isReadonly ' ) && $ node ->isReadonly ());
308312 $ this ->addCommentAndAttributes ($ prop , $ node );
@@ -328,7 +332,8 @@ private function addMethodToClass(ClassType $class, Node\Stmt\ClassMethod $node)
328332 private function addConstantToClass (ClassType $ class , Node \Stmt \ClassConst $ node ): void
329333 {
330334 foreach ($ node ->consts as $ item ) {
331- $ const = $ class ->addConstant ($ item ->name ->toString (), new Literal ($ this ->toPhp ($ item ->value )));
335+ $ value = $ this ->getReformattedContents ([$ item ->value ], 1 );
336+ $ const = $ class ->addConstant ($ item ->name ->toString (), new Literal ($ value ));
332337 if ($ node ->isPrivate ()) {
333338 $ const ->setPrivate ();
334339 } elseif ($ node ->isProtected ()) {
@@ -359,7 +364,7 @@ private function addCommentAndAttributes($element, Node $node): void
359364 foreach ($ group ->attrs as $ attribute ) {
360365 $ args = [];
361366 foreach ($ attribute ->args as $ arg ) {
362- $ value = new Literal ($ this ->toPhp ( $ arg ));
367+ $ value = new Literal ($ this ->getReformattedContents ([ $ arg], 0 ));
363368 if ($ arg ->name ) {
364369 $ args [$ arg ->name ->toString ()] = $ value ;
365370 } else {
@@ -385,13 +390,13 @@ private function setupFunction($function, Node\FunctionLike $node): void
385390 $ param ->setReference ($ item ->byRef );
386391 $ function ->setVariadic ($ item ->variadic );
387392 if ($ item ->default ) {
388- $ param ->setDefaultValue (new Literal ($ this ->toPhp ( $ item ->default )));
393+ $ param ->setDefaultValue (new Literal ($ this ->getReformattedContents ([ $ item ->default ], 2 )));
389394 }
390395 $ this ->addCommentAndAttributes ($ param , $ item );
391396 }
392397 $ this ->addCommentAndAttributes ($ function , $ node );
393398 if ($ node ->stmts ) {
394- $ function ->setBody ($ this ->getReformattedBody ($ node ->stmts , 2 ));
399+ $ function ->setBody ($ this ->getReformattedContents ($ node ->stmts , 2 ));
395400 }
396401 }
397402
0 commit comments