From 15da5a394932f465dffd9b5f85c7fa6f42e476a5 Mon Sep 17 00:00:00 2001 From: Zeljko Mitic Date: Thu, 7 Apr 2022 15:08:19 +0200 Subject: [PATCH] Templated ServiceLocator to be used for tagged services --- .../ServiceLocator.stubphp | 29 ++++++++ .../ServiceLocator.stubphp | 30 ++++++++ .../acceptance/ServiceLocator.feature | 71 +++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 src/Stubs/5/Component/DependencyInjection/ServiceLocator.stubphp create mode 100644 src/Stubs/6/Component/DependencyInjection/ServiceLocator.stubphp create mode 100644 tests/acceptance/acceptance/ServiceLocator.feature diff --git a/src/Stubs/5/Component/DependencyInjection/ServiceLocator.stubphp b/src/Stubs/5/Component/DependencyInjection/ServiceLocator.stubphp new file mode 100644 index 00000000..a6ea41a6 --- /dev/null +++ b/src/Stubs/5/Component/DependencyInjection/ServiceLocator.stubphp @@ -0,0 +1,29 @@ + + */ + public function getProvidedServices(): array; +} diff --git a/src/Stubs/6/Component/DependencyInjection/ServiceLocator.stubphp b/src/Stubs/6/Component/DependencyInjection/ServiceLocator.stubphp new file mode 100644 index 00000000..500174f1 --- /dev/null +++ b/src/Stubs/6/Component/DependencyInjection/ServiceLocator.stubphp @@ -0,0 +1,30 @@ + + */ + public function getProvidedServices(): array; +} diff --git a/tests/acceptance/acceptance/ServiceLocator.feature b/tests/acceptance/acceptance/ServiceLocator.feature new file mode 100644 index 00000000..01bccb5b --- /dev/null +++ b/tests/acceptance/acceptance/ServiceLocator.feature @@ -0,0 +1,71 @@ +@symfony-5 @symfony-6 +Feature: ServiceLocator + + Background: + Given I have Symfony plugin enabled + + Scenario: ServiceLocator will return tagged service + Given I have the following code + """ + $strategies */ + private ServiceLocator $strategies; + + /** @param ServiceLocator $strategies */ + public function __construct(ServiceLocator $strategies) + { + $this->strategies = $strategies; + } + + public function doSomethingWithStrategy(): void + { + $strategy = $this->strategies->get('random_string'); + /** @psalm-trace $strategy */ + } + } + """ + When I run Psalm + Then I see these errors + | Type | Message | + | Trace | $strategy: StrategyInterface | + And I see no other errors + + Scenario: Fixed the return type of getProvidedServices() + Given I have the following code + """ + $strategies */ + private ServiceLocator $strategies; + + /** @param ServiceLocator $strategies */ + public function __construct(ServiceLocator $strategies) + { + $this->strategies = $strategies; + } + + public function getAll(): void + { + $names = $this->strategies->getProvidedServices(); + /** @psalm-trace $names */ + } + } + """ + When I run Psalm + Then I see these errors + | Type | Message | + | Trace | $names: array | + And I see no other errors