Skip to content

Support for ServiceProviderInterface #252

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

Open
wants to merge 2 commits into
base: 1.2.x
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Support for Symfony\Contracts\Service\ServiceProviderInterface
Accessing services from `ServiceProviderInterface` (which extends `ContainerInterface`) should not trigger "Service is private".
  • Loading branch information
Wirone committed Feb 8, 2022
commit 8ce5970ae07a6d052c35030a7bfe8fcc1861410c
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));
}

}