@@ -50,6 +50,7 @@ private function parseCode(string $code): void
50
50
$ stmts = $ parser ->parse ($ this ->code );
51
51
52
52
$ traverser = new PhpParser \NodeTraverser ;
53
+ $ traverser ->addVisitor (new PhpParser \NodeVisitor \ParentConnectingVisitor );
53
54
$ traverser ->addVisitor (new PhpParser \NodeVisitor \NameResolver (null , ['preserveOriginalNames ' => true ]));
54
55
$ this ->statements = $ traverser ->traverse ($ stmts );
55
56
}
@@ -67,7 +68,7 @@ public function extractMethodBodies(string $className): array
67
68
foreach ($ nodeFinder ->findInstanceOf ($ classNode , Node \Stmt \ClassMethod::class) as $ methodNode ) {
68
69
/** @var Node\Stmt\ClassMethod $methodNode */
69
70
if ($ methodNode ->stmts ) {
70
- $ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedBody ($ methodNode ->stmts , 2 );
71
+ $ res [$ methodNode ->name ->toString ()] = $ this ->getReformattedContents ($ methodNode ->stmts , 2 );
71
72
}
72
73
}
73
74
return $ res ;
@@ -81,12 +82,12 @@ public function extractFunctionBody(string $name): ?string
81
82
return $ node instanceof Node \Stmt \Function_ && $ node ->namespacedName ->toString () === $ name ;
82
83
});
83
84
84
- return $ this ->getReformattedBody ($ functionNode ->stmts , 1 );
85
+ return $ this ->getReformattedContents ($ functionNode ->stmts , 1 );
85
86
}
86
87
87
88
88
89
/** @param Node[] $statements */
89
- private function getReformattedBody (array $ statements , int $ level ): string
90
+ private function getReformattedContents (array $ statements , int $ level ): string
90
91
{
91
92
$ body = $ this ->getNodeContents (...$ statements );
92
93
$ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ statements ));
@@ -101,10 +102,13 @@ private function prepareReplacements(array $statements): array
101
102
(new NodeFinder )->find ($ statements , function (Node $ node ) use (&$ replacements , $ start ) {
102
103
if ($ node instanceof Node \Name \FullyQualified) {
103
104
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 );
104
108
$ replacements [] = [
105
109
$ node ->getStartFilePos () - $ start ,
106
110
$ node ->getEndFilePos () - $ start ,
107
- $ node ->toCodeString (),
111
+ Helpers:: tagName ( $ node ->toCodeString (), $ type ),
108
112
];
109
113
}
110
114
@@ -302,7 +306,7 @@ private function addPropertyToClass(ClassType $class, Node\Stmt\Property $node):
302
306
}
303
307
$ prop ->setType ($ node ->type ? $ this ->toPhp ($ node ->type ) : null );
304
308
if ($ item ->default ) {
305
- $ prop ->setValue (new Literal ($ this ->toPhp ( $ item ->default )));
309
+ $ prop ->setValue (new Literal ($ this ->getReformattedContents ([ $ item ->default ], 1 )));
306
310
}
307
311
$ prop ->setReadOnly (method_exists ($ node , 'isReadonly ' ) && $ node ->isReadonly ());
308
312
$ this ->addCommentAndAttributes ($ prop , $ node );
@@ -328,7 +332,8 @@ private function addMethodToClass(ClassType $class, Node\Stmt\ClassMethod $node)
328
332
private function addConstantToClass (ClassType $ class , Node \Stmt \ClassConst $ node ): void
329
333
{
330
334
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 ));
332
337
if ($ node ->isPrivate ()) {
333
338
$ const ->setPrivate ();
334
339
} elseif ($ node ->isProtected ()) {
@@ -359,7 +364,7 @@ private function addCommentAndAttributes($element, Node $node): void
359
364
foreach ($ group ->attrs as $ attribute ) {
360
365
$ args = [];
361
366
foreach ($ attribute ->args as $ arg ) {
362
- $ value = new Literal ($ this ->toPhp ( $ arg ));
367
+ $ value = new Literal ($ this ->getReformattedContents ([ $ arg], 0 ));
363
368
if ($ arg ->name ) {
364
369
$ args [$ arg ->name ->toString ()] = $ value ;
365
370
} else {
@@ -385,13 +390,13 @@ private function setupFunction($function, Node\FunctionLike $node): void
385
390
$ param ->setReference ($ item ->byRef );
386
391
$ function ->setVariadic ($ item ->variadic );
387
392
if ($ item ->default ) {
388
- $ param ->setDefaultValue (new Literal ($ this ->toPhp ( $ item ->default )));
393
+ $ param ->setDefaultValue (new Literal ($ this ->getReformattedContents ([ $ item ->default ], 2 )));
389
394
}
390
395
$ this ->addCommentAndAttributes ($ param , $ item );
391
396
}
392
397
$ this ->addCommentAndAttributes ($ function , $ node );
393
398
if ($ node ->stmts ) {
394
- $ function ->setBody ($ this ->getReformattedBody ($ node ->stmts , 2 ));
399
+ $ function ->setBody ($ this ->getReformattedContents ($ node ->stmts , 2 ));
395
400
}
396
401
}
397
402
0 commit comments