Skip to content
Closed
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ parameters:
- */tests/tmp/*
- */tests/*/ExampleContainer.php
- */tests/*/ExampleController.php
- */tests/*/ExampleServiceSubscriber.php
- */tests/*/request_get_content.php
5 changes: 5 additions & 0 deletions src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$isServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
if ($isServiceSubscriber->yes()) {
return [];
}

$serviceId = ServiceMap::getServiceIdFromNode($node->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
Expand Down
14 changes: 14 additions & 0 deletions tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ public function testGetPrivateService(): void
);
}

public function testGetPrivateServiceInServiceSubscriber(): void
{
if (!interface_exists('Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface')) {
self::markTestSkipped('The test needs Symfony\Component\DependencyInjection\ServiceSubscriberInterface class.');
}

$this->analyse(
[
__DIR__ . '/ExampleServiceSubscriber.php',
],
[]
);
}

}
52 changes: 52 additions & 0 deletions tests/Rules/Symfony/ExampleServiceSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Symfony;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class ExampleServiceSubscriber extends AbstractController
{

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

public function privateServiceInTestContainer(): void
{
/** @var \Symfony\Bundle\FrameworkBundle\Test\TestContainer $container */
$container = doFoo();
$container->get('private');
}

public function unknownService(): void
{
$this->get('unknown');
}

public function unknownGuardedServiceInsideContext(): void
{
if ($this->has('unknown')) { // phpcs:ignore
$this->get('unknown');
}
}

public function unknownGuardedServiceOutsideOfContext(): void
{
if (!$this->has('unknown')) {
return;
}
$this->get('unknown');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [
'private' => MyService::class,
];
}

}