thephpleague / container

Small but powerful dependency injection container

Home Page:http://container.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong type-hint on ContainerAwareTrait::$container

Jalle19 opened this issue · comments

The $container field is type-hinted as \Psr\Container\ContainerInterface, which means you can't have a legal ServiceProvider implementation that does this:

class FooServiceProvider extends AbstractServiceProvider {
    protected $provides = [
        ValidatorInterface::class,
    ];

    public function register()
    {
        $this->container->share(ValidatorInterface::class, Validator::class);
    }
}

Both phpstan and PhpStorm complains that there is no method named share() on $this->container. I'm not sure if the correct fix is to change just the member variable's type-hint or the setContainer() and getContainer() methods too.

It doesn't seem to make sense to swap out the League\Container for some other PSR compatible container in a service provider specifically designed for this package, so maybe the whole ContainerAwareInterface needs some changes?

A complete change not necessarily to ContainerAwareInterface, but certainly to the service providers is definitely the way to go here, it's a breaking change though so allow me to pull it in to some other changes for a major release in the next few weeks

Yeah I suppose a protected function getLeagueContainer(): \Leage\Container in AbstractServiceProvider would do the trick, that way both type-hinting and API compatibility could be preserved

I looked at this again and the way I see it this library shouldn't type-hint anything to Psr\Container\ContainerInterface. Currently service providers expect the container passed to them to be a League container even though all type-hints only mention a PSR container. E.g. you can currently call $serviceProvider->setContainer($notLeagueContainer) without any static analysis errors, but as soon as the service provider is started it will fall apart since there are no add and share methods on Psr\Container\ContainerInterface.

The way I see it, League\Container\Container should implement the PSR container interface, and that's it. No type-hinting PSR anywhere.

https://travis-ci.org/digiaonline/graphql-php/jobs/461151053 here's an example of static analysis errors due to these type-hints

This is something I agree with changing, but obviously it will need to be a major release, since it's not functionally a problem, I will fold it in to what the natural next major release is

Any plans on when there will be a next major release?

Any updates on this issue? @philipobenito

Doing a minor release in the next few days with the view to doing a major in the next couple of weeks

@philipobenito How's the release going?

@philipobenito do you want a PR?

I fixed this in https://github.com/Jalle19/container/tree/fix-159. The branch is based on #178 so I didn't bother PRing it just yet. @philipobenito any chance you could take a look at these?

latest release provides {get,set}LeagueContainerMethods I'll be addressing the overall issue in the next major release

Any idea where you will be taking this in the next release? I'm thinking if the main purpose of a service provider is to configure all container services the main ->container property should directly typehint the league container. As long as setting container items is not part of a main psr interface I don't think service providers should try to be not coupled to a certain implementation.