Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ServiceProviderInterface #252

Open
wants to merge 2 commits into
base: 1.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ 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 = $this->isServiceSubscriber($argType, $scope);
$isServiceProvider = $this->isServiceProvider($argType, $scope);
// ServiceLocator is an implementation of ServiceProviderInterface but let's keep explicit rule for it to keep full BC
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
if (
$isTestContainerType->yes()
|| $isOldServiceSubscriber->yes()
|| $isServiceSubscriber->yes()
|| $isServiceProvider->yes()
|| $isServiceLocator->yes()
) {
return [];
}

Expand Down Expand Up @@ -87,14 +95,31 @@ public function processNode(Node $node, Scope $scope): array

private function isServiceSubscriber(Type $containerType, Scope $scope): TrinaryLogic
{
$serviceSubscriberInterfaceType = new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface');
$isContainerServiceSubscriber = $serviceSubscriberInterfaceType->isSuperTypeOf($containerType);
return $this->isInstanceOf(
new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface'),
$containerType,
$scope
);
}

private function isServiceProvider(Type $containerType, Scope $scope): TrinaryLogic
{
return $this->isInstanceOf(
new ObjectType('Symfony\Contracts\Service\ServiceProviderInterface'),
$containerType,
$scope
);
}

private function isInstanceOf(ObjectType $interfaceType, Type $containerType, Scope $scope): TrinaryLogic
{
$isImplementation = $interfaceType->isSuperTypeOf($containerType);
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $isContainerServiceSubscriber;
return $isImplementation;
}
$containedClassType = new ObjectType($classReflection->getName());
return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType));
return $isImplementation->or($interfaceType->isSuperTypeOf($containedClassType));
}

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

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

}
5 changes: 0 additions & 5 deletions tests/Rules/Symfony/container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
<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>