-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ServiceSubscriberTrait #321
Comments
What's the nature of your issue? What output do you expect instead? What do you need better understanding of? |
I would like that here wouldn't have errors of type |
<?php
namespace App;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Service\Attribute\SubscribedService;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Service\ServiceSubscriberTrait;
class Foo implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;
#[SubscribedService]
private function logger(): LoggerInterface
{
return $this->container->get(__METHOD__);
}
} Symfony is using the method name as local name for the service inside injected service locator. So phpstan should not check for service with name It is related to #352. <?php
namespace App;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
class Foo implements ServiceSubscriberInterface
{
public function __construct(
private readonly ContainerInterface $locator
) {}
public static function getSubscribedServices(): iterable
{
return [
// inject LoggerInterface service and use the method name as local name for the service inside this class
self::class.'::logger' => LoggerInterface::class,
];
}
private function logger(): LoggerInterface
{
return $this->container->get(__METHOD__);
}
} |
Yeah, it would be nice to have phpstan-symfony understand this out of the box. My workaround for now: Just ignoring the error in parameters:
ignoreErrors:
- '#Service ".+::get[a-zA-Z0-9]+" is not registered in the container.#' Of course, that way you'd also miss invalid service names that accidentally match It would be better to ignore container calls from methods that have the |
Sorry, but are there any updates on the issue using the Fyi: Using this inside a Trait works, but thats not always the best solution. E.g: trait LoggerAwareTrait
{
#[SubscribedService]
protected function logger(): LoggerInterface
{
return $this->container->get(__CLASS__ . '::' . __FUNCTION__);
}
} |
I have an issue with ServiceSubscriberTrait (https://symfony.com/doc/5.4/service_container/service_subscribers_locators.html#service-subscriber-trait) on symfony 5.4 and PHP 7.4. Error message is like
Service "App\Service\MyService::router" is not registered in the container.
How to reproduce:
The output will be:
The text was updated successfully, but these errors were encountered: