Skip to content

Commit 34b1940

Browse files
committed
Do not report private services in test container
1 parent 74da071 commit 34b1940

5 files changed

+19
-7
lines changed

src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function processNode(Node $node, Scope $scope): array
4343
$argType = $scope->getType($node->var);
4444
$isControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller'))->isSuperTypeOf($argType);
4545
$isContainerType = (new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface'))->isSuperTypeOf($argType);
46-
if (!$isControllerType->yes() && !$isContainerType->yes()) {
46+
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
47+
if ($isTestContainerType->yes() || (!$isControllerType->yes() && !$isContainerType->yes())) {
4748
return [];
4849
}
4950

tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testGetPrivateService(): void
2222
[
2323
[
2424
'Service "private" is private.',
25-
14,
25+
15,
2626
],
2727
]
2828
);

tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ public function testGetUnknownService(): void
2222
[
2323
[
2424
'Service "service.not.found" is not registered in the container.',
25-
20,
25+
21,
2626
],
2727
[
2828
'Service "PHPStan\Symfony\ServiceMap" is not registered in the container.',
29-
26,
29+
27,
3030
],
3131
[
3232
'Service "service.PHPStan\Symfony\ServiceMap" is not registered in the container.',
33-
38,
33+
39,
3434
],
3535
[
3636
'Service "PHPStan\Symfony\ExampleController" is not registered in the container.',
37-
44,
37+
45,
3838
],
3939
]
4040
);

tests/Symfony/ServiceMapTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testGetServiceIdFromNode(): void
4141
$broker = $this->createBroker();
4242
$printer = new Standard();
4343
$typeSpecifier = $this->createTypeSpecifier($printer, $broker);
44-
$scope = new Scope($broker, $printer, $typeSpecifier, ScopeContext::create(''));
44+
$scope = new Scope($this->createScopeFactory($broker, $typeSpecifier), $broker, $printer, $typeSpecifier, ScopeContext::create(''));
4545

4646
self::assertSame('foo', ServiceMap::getServiceIdFromNode(new String_('foo'), $scope));
4747
self::assertSame('bar', ServiceMap::getServiceIdFromNode(new ClassConstFetch(new Name(ExampleController::class), 'BAR'), $scope));

tests/Symfony/data/ExampleController.php

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Symfony;
44

55
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
67

78
final class ExampleController extends Controller
89
{
@@ -45,4 +46,14 @@ public function getSelfService(): void
4546
$service->noMethod();
4647
}
4748

49+
public function testContainer(): void
50+
{
51+
$service = $this->getTestContainer()->get('private');
52+
$service->noMethod();
53+
}
54+
55+
private function getTestContainer(): TestContainer
56+
{
57+
}
58+
4859
}

0 commit comments

Comments
 (0)