Skip to content

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

@h4kuna

Description

@h4kuna

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.

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