Skip to content

Commit c340de8

Browse files
iamluclookyman
authored andcommitted
Disable PrivateServiceRule when the class implements ServiceSubscriberInterface
1 parent 00f6c9a commit c340de8

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Diff for: src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ public function processNode(Node $node, Scope $scope): array
4242
}
4343

4444
$argType = $scope->getType($node->var);
45+
46+
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
47+
$isServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
48+
if ($isTestContainerType->yes() || $isServiceSubscriber->yes()) {
49+
return [];
50+
}
51+
4552
$isControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller'))->isSuperTypeOf($argType);
4653
$isAbstractControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($argType);
4754
$isContainerType = (new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface'))->isSuperTypeOf($argType);
48-
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
49-
if ($isTestContainerType->yes() || (!$isControllerType->yes() && !$isAbstractControllerType->yes() && !$isContainerType->yes())) {
55+
if (!$isControllerType->yes() && !$isAbstractControllerType->yes() && !$isContainerType->yes()) {
5056
return [];
5157
}
5258

Diff for: tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@ public function testGetPrivateService(): void
2929
);
3030
}
3131

32+
public function testGetPrivateServiceInServiceSubscriber(): void
33+
{
34+
if (!interface_exists('Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface')) {
35+
self::markTestSkipped('The test needs Symfony\Component\DependencyInjection\ServiceSubscriberInterface class.');
36+
}
37+
38+
$this->analyse(
39+
[
40+
__DIR__ . '/ExampleServiceSubscriber.php',
41+
],
42+
[]
43+
);
44+
}
45+
3246
}

Diff for: tests/Rules/Symfony/ExampleServiceSubscriber.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
7+
8+
final class ExampleServiceSubscriber extends Controller implements ServiceSubscriberInterface
9+
{
10+
11+
public function privateService(): void
12+
{
13+
$this->get('private');
14+
}
15+
16+
/**
17+
* @return string[]
18+
*/
19+
public static function getSubscribedServices(): array
20+
{
21+
return [];
22+
}
23+
24+
}

0 commit comments

Comments
 (0)