8000 Support for `ServiceProviderInterface` by Wirone · Pull Request #252 · phpstan/phpstan-symfony · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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>
0