prevent service subscribers overwriting already known service definitions (#194)

fixes #193
This commit is contained in:
Ben Davies 2021-07-22 06:03:06 +01:00 committed by GitHub
parent 7693ae692f
commit 7d7a06209c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -188,9 +188,11 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
$key = $arrayItem->key;
$serviceId = $key instanceof String_ ? $key->value : $className;
$service = new Service($serviceId, $className);
$service->setIsPublic(true);
self::$containerMeta->add($service);
if (null === self::$containerMeta->get($className)) {
$service = new Service($serviceId, $className);
$service->setIsPublic(true);
self::$containerMeta->add($service);
}
$codebase->queueClassLikeForScanning($className);
$fileStorage->referenced_classlikes[strtolower($className)] = $className;

View File

@ -81,3 +81,41 @@ Feature: Service Subscriber
| Type | Message |
| Trace | $entityManager: Doctrine\ORM\EntityManagerInterface |
And I see no other errors
Scenario: Asserting psalm recognizes return type of services defined in getSubscribedServices, already defined as an alias in containerXml
Given I have the following code
"""
<?php
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
class SomeController implements ServiceSubscriberInterface
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function __invoke()
{
/** @psalm-trace $kernel */
$kernel = $this->container->get('http_kernel');
}
public static function getSubscribedServices(): array
{
return [
'http_kernel' => HttpKernelInterface::class,
];
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| Trace | $kernel: Symfony\Component\HttpKernel\HttpKernel |
And I see no other errors