phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Allow service subscriber to have non-service name keys under certain circumstances.

Open Basster opened this issue 2 years ago • 1 comments

You can define service subscribers with non-service name keys, e.g.

class SubmissionStrategyFactory implements ServiceSubscriberInterface
{
    public function __construct(private readonly ContainerInterface $locator)
    {
    }

    public function getStrategy(IllustrationSubmission $submission): SubmissionStrategyInterface
    {
        if ($submission->getIllustrationFile()) {
            return $this->locator->get('upload');
        }

        return $this->locator->get('url');
    }

    public static function getSubscribedServices(): iterable
    {
        yield 'upload' => UploadStrategy::class;
        yield 'url' => UrlStrategy::class;
    }
}

phpstan currently reports this as Service "upload" is not registered in the container.

This PR allows non-service argument names when the argument is a container and its called within a ServiceLocator.

Maybe, this is also related to #89

Basster avatar May 08 '23 17:05 Basster

As is, this won't solve #89. For that, you would have to do the same detection of ServiceSubscriberInterface in the ContainerInterfacePrivateServiceRule.

stof avatar Sep 28 '23 09:09 stof