From e4b9ea96b78057a758ac648986d2339259448293 Mon Sep 17 00:00:00 2001 From: Andrey Bondar Date: Tue, 27 Apr 2021 12:18:16 +0300 Subject: [PATCH] Allow fetching private services from ServiceLocator --- src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php | 3 ++- tests/Rules/Symfony/ExampleController.php | 5 +++++ tests/Rules/Symfony/container.xml | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php b/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php index f97633c0..c471e6ba 100644 --- a/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php +++ b/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php @@ -54,7 +54,8 @@ public function processNode(Node $node, Scope $scope): array $isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType); $isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType); $isServiceSubscriber = (new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface'))->isSuperTypeOf($argType); - if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes()) { + $isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType); + if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) { return []; } diff --git a/tests/Rules/Symfony/ExampleController.php b/tests/Rules/Symfony/ExampleController.php index 65d1359b..edbdaaf5 100644 --- a/tests/Rules/Symfony/ExampleController.php +++ b/tests/Rules/Symfony/ExampleController.php @@ -39,4 +39,9 @@ public function unknownGuardedServiceOutsideOfContext(): void $this->get('unknown'); } + public function privateServiceFromServiceLocator(): void + { + $this->get('service_locator')->get('private'); + } + } diff --git a/tests/Rules/Symfony/container.xml b/tests/Rules/Symfony/container.xml index f21aeae3..f3261e0a 100644 --- a/tests/Rules/Symfony/container.xml +++ b/tests/Rules/Symfony/container.xml @@ -3,5 +3,10 @@ + + + + +