Skip to content

Fix false positive about private service in service subscriber #220

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

Merged
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
16 changes: 15 additions & 1 deletion src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Symfony\ServiceMap;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function sprintf;

/**
Expand Down Expand Up @@ -53,7 +55,7 @@ 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);
$isServiceSubscriber = $this->isServiceSubscriber($argType, $scope);
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
return [];
Expand Down Expand Up @@ -83,4 +85,16 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

private function isServiceSubscriber(Type $containerType, Scope $scope): TrinaryLogic
{
$serviceSubscriberInterfaceType = new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface');
$isContainerServiceSubscriber = $serviceSubscriberInterfaceType->isSuperTypeOf($containerType);
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $isContainerServiceSubscriber;
}
$containedClassType = new ObjectType($classReflection->getName());
return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType));
}

}
10 changes: 10 additions & 0 deletions tests/Rules/Symfony/ExampleServiceSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

namespace PHPStan\Rules\Symfony;

use Psr\Container\ContainerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

final class ExampleServiceSubscriber implements ServiceSubscriberInterface
{

/** @var ContainerInterface */
private $locator;

public function __construct(ContainerInterface $locator)
{
$this->locator = $locator;
}

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

public function containerParameter(): void
Expand Down