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

Parent method with static type, has a badly evaluated return type for factory method in container #295

Closed
h4kuna opened this issue Sep 15, 2023 · 0 comments

Comments

@h4kuna
Copy link
Contributor

h4kuna commented Sep 15, 2023

Version: v3.1.3

Bug Description

If i extends class, where parent has method with self or static return type. I use this method for a single line notation, then in container is returned type of parent instead my class. I get exception Service of type Ipsum: Service of type Bar required by $bar in Ipsum::__construct() not found. Did you add it to configuration file?

Steps To Reproduce

Classes for use case

class Foo
{
	public function any(): static
	{
		return $this;
	}
}

class Bar extends Foo
{
}

class Ipsum {
	public function __construct(private Bar $bar)
	{
	}
}
services:
	- Bar()::any()
	- Ipsum

Factory method in container:

public function createService010(): Foo # here i expect Bar
{
	return (new Bar)->any(); # this is right!
}

Standard use

services:
	-
		factory: Bar()
		setup:
			- any()

Factory method in container:

public function createService010(): Bar
{
	$service = new Bar;
	$service->any();
	return $service;
}

Expected Behavior

The returned type of method is type of my class.

public function createService010(): Bar
{
	return (new Bar)->any();
}

Possible Solution

I try debugging and found Resolver.php#L116 where $entity is array ['Bar', 'any'] and returned ReflectionMethod with class Foo. If i fix it that second place Resolver.php#L133, where it is changed from Bar::any to Foo::any. I haven't context, knowlage and time to fix it.

dg added a commit to nette/utils that referenced this issue Sep 27, 2023
dg added a commit to nette/utils that referenced this issue Oct 29, 2023
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

No branches or pull requests

1 participant