Skip to content

Commit 5005288

Browse files
Do not narrow KernelTestCase::getContainer() return type to internal TestContainer
1 parent 582ddbc commit 5005288

File tree

7 files changed

+56
-41
lines changed

7 files changed

+56
-41
lines changed

extension.neon

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ parameters:
1313
- stubs/Psr/Cache/CacheItemInterface.stub
1414
- stubs/Psr/Cache/InvalidArgumentException.stub
1515
- stubs/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.stub
16-
- stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.stub
17-
- stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.stub
18-
- stubs/Symfony/Bundle/FrameworkBundle/Test/TestContainer.stub
1916
- stubs/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AuthenticatorFactoryInterface.stub
2017
- stubs/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/FirewallListenerFactoryInterface.stub
2118
- stubs/Symfony/Component/Console/Command.stub

src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public function processNode(Node $node, Scope $scope): array
4343

4444
$argType = $scope->getType($node->var);
4545

46-
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
46+
$isTestContainer = $this->isTestContainer($argType, $scope);
4747
$isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
4848
$isServiceSubscriber = $this->isServiceSubscriber($argType, $scope);
4949
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
50-
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
50+
if ($isTestContainer->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
5151
return [];
5252
}
5353

@@ -91,4 +91,25 @@ private function isServiceSubscriber(Type $containerType, Scope $scope): Trinary
9191
return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType)->result);
9292
}
9393

94+
private function isTestContainer(Type $containerType, Scope $scope): TrinaryLogic
95+
{
96+
$testContainer = new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer');
97+
$isTestContainer = $testContainer->isSuperTypeOf($containerType)->result;
98+
99+
$classReflection = $scope->getClassReflection();
100+
if ($classReflection === null) {
101+
return $isTestContainer;
102+
}
103+
104+
$containerInterface = new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface');
105+
$kernelTestCase = new ObjectType('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase');
106+
$containedClassType = new ObjectType($classReflection->getName());
107+
108+
return $isTestContainer->or(
109+
$containerInterface->isSuperTypeOf($containerType)->result->and(
110+
$kernelTestCase->isSuperTypeOf($containedClassType)->result,
111+
),
112+
);
113+
}
114+
94115
}

stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.stub

-13
This file was deleted.

stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.stub

-16
This file was deleted.

stubs/Symfony/Bundle/FrameworkBundle/Test/TestContainer.stub

-7
This file was deleted.

tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ public function testGetPrivateServiceInServiceSubscriber(): void
7777
);
7878
}
7979

80+
public function testGetPrivateServiceInTest(): void
81+
{
82+
$this->analyse(
83+
[
84+
__DIR__ . '/ExampleTest.php',
85+
],
86+
[],
87+
);
88+
}
89+
8090
}

tests/Rules/Symfony/ExampleTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
8+
abstract class ExampleTest extends KernelTestCase
9+
{
10+
11+
public function bar(): void
12+
{
13+
$container = self::getContainer();
14+
$container->get('private');
15+
}
16+
17+
public function foo(KernelBrowser $browser): void
18+
{
19+
$container = $browser->getContainer();
20+
$container->get('private');
21+
}
22+
23+
}

0 commit comments

Comments
 (0)