Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
djaf77 opened this issue Jun 5, 2020 · 0 comments · Fixed by #62
Closed

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

djaf77 opened this issue Jun 5, 2020 · 0 comments · Fixed by #62

Comments

@djaf77
Copy link
Contributor

djaf77 commented Jun 5, 2020

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
djaf77 pushed a commit to superjobru/php-generator that referenced this issue Jun 5, 2020
@dg dg closed this as completed in #62 Jun 5, 2020
dg pushed a commit that referenced this issue Jun 5, 2020
dg pushed a commit that referenced this issue Jun 5, 2020
dg pushed a commit that referenced this issue Jun 19, 2020
dg pushed a commit that referenced this issue Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant