Skip to content

Commit

Permalink
Allow fetching private services from ServiceLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-bondar committed Apr 27, 2021
1 parent 8ae41d1 commit e4b9ea9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/Symfony/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function unknownGuardedServiceOutsideOfContext(): void
$this->get('unknown');
}

public function privateServiceFromServiceLocator(): void
{
$this->get('service_locator')->get('private');
}

}
5 changes: 5 additions & 0 deletions tests/Rules/Symfony/container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
<services>
<service id="private" class="Foo" public="false"></service>
<service id="public" class="Foo"></service>
<service id="service_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="true">
<argument type="collection">
<argument key="private" type="service" id="private"/>
</argument>
</service>
</services>
</container>

0 comments on commit e4b9ea9

Please sign in to comment.