nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Method's and function's bodies are copied without name resolve #61

Closed djaf77 closed 4 years ago

djaf77 commented 4 years ago

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:

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:

  1. 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... :(
  2. 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