Skip to content

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

@djaf77

Description

@djaf77

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions