Class generated using Reflection have fully-qualified names, because Reflection return all names as FQN. But method's and function's bodies are copied without fully-qualified name.
Nikic's NodeVisitor\NameResolver resolve name, but not change node's position in file, which used for copying body.
Steps To Reproduce
We have class for reflection:
namespace Abc;
abstract class MyClass
{
function method(AnotherClass $object): string
{
return AnotherClass::class;
}
}
after Nette\PhpGenerator\ClassType::withBodiesFrom(MyClass::class); we'll get:
abstract class Class7
{
function method(\Abc\AnotherClass $object): string
{
return AnotherClass::class;
}
}
Expected Behavior
We must get FQN in method's and global function's body too.
abstract class Class7
{
function method(\Abc\AnotherClass $object): string
{
return \Abc\AnotherClass::class;
}
}
Possible Solution
There are two way for problem solving:
We can use Nikic's PhpParser\PrettyPrinter for print traversed expression nodes from body. But prettyPrint() will changed original formatting, and printFormatPreserving() can work with whole file only... :(
Collect all Name Nodes from importing method/function, and replace them in extracted body's string by position. I used this way in my fork and I'll create PR little later
Version: 3.4.0
Bug Description
Class generated using Reflection have fully-qualified names, because Reflection return all names as FQN. But method's and function's bodies are copied without fully-qualified name. Nikic's NodeVisitor\NameResolver resolve name, but not change node's position in file, which used for copying body.
Steps To Reproduce
We have class for reflection:
after
Nette\PhpGenerator\ClassType::withBodiesFrom(MyClass::class);
we'll get:Expected Behavior
We must get FQN in method's and global function's body too.
Possible Solution
There are two way for problem solving: