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

Multiple interfaces injection fails if they have the same concrete class #53110

Open
LucaBenini opened this issue Oct 11, 2024 · 1 comment
Open

Comments

@LucaBenini
Copy link

LucaBenini commented Oct 11, 2024

Laravel Version

11.26.0

PHP Version

8.3.12

Database Driver & Version

No response

Description

Description

If two independent interfaces are implemented by the same concrete class and used in a method binding only the first interface gets resolved.

Probable Cause

Following the issue in debug, it appears to be related to the interaction and logic behind alreadyInParameters and resolveMethodDependencies.

The method alreadyInParameters prevents the multiple injection of objects with the same class but compares the class name against the instanceof of the previous parameters after they have been resolved instead of the requested type of the parameters.

Steps To Reproduce

given the following classes:

interface IInterfaceA
{
    public function doSomethingA();
}
interface IInterfaceB
{
    public function doSomethingB();
}
class ConcreteClass implements IInterfaceA, IInterfaceB
{
    public function doSomethingA()
    {
        // Implementation for doSomethingA
    }

    public function doSomethingB()
    {
        // Implementation for doSomethingB
    }
}

and registered with:

$this->app->singleton(\App\Services\ConcreteClass::class, function (Application $app) {
            return new ConcreteClass();
        });
$this->app->bind(\App\Interfaces\IInterfaceA::class,\App\Services\ConcreteClass::class);
$this->app->bind(\App\Interfaces\IInterfaceB::class,\App\Services\ConcreteClass::class);

the route pointing to the following controller raise an ArgumentCountError

class SampleController extends Controller
{
	public function executeRequest(Request $request,IInterfaceA interfaceA, IInterfaceB interfaceB)
	{
	}
}
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants