Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d5c53a

Browse files
Add solution for test container
1 parent cf8c9c6 commit 0d5c53a

File tree

11 files changed

+142
-41
lines changed

11 files changed

+142
-41
lines changed

‎extension.neon‎

Lines changed: 6 additions & 3 deletions
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
@@ -364,3 +361,9 @@ services:
364361
class: PHPStan\Symfony\SymfonyContainerResultCacheMetaExtension
365362
tags:
366363
- phpstan.resultCacheMetaExtension
364+
-
365+
class: PHPStan\Type\Symfony\Container\KernelBrowserGetContainerDynamicReturnTypeExtension
366+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
367+
-
368+
class: PHPStan\Type\Symfony\Container\KernelTestCaseGetContainerDynamicReturnTypeExtension
369+
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]

‎src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Symfony\ServiceMap;
1111
use PHPStan\TrinaryLogic;
1212
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\Symfony\Container\TestContainerType;
1314
use PHPStan\Type\Type;
1415
use function sprintf;
1516

@@ -43,11 +44,17 @@ public function processNode(Node $node, Scope $scope): array
4344

4445
$argType = $scope->getType($node->var);
4546

46-
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
47+
if (
48+
$argType instanceof TestContainerType
49+
|| (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType)->yes()
50+
) {
51+
return [];
52+
}
53+
4754
$isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
4855
$isServiceSubscriber = $this->isServiceSubscriber($argType, $scope);
4956
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
50-
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
57+
if ($isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
5158
return [];
5259
}
5360

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony\Container;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
9+
use PHPStan\Type\Type;
10+
11+
final class KernelBrowserGetContainerDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
12+
{
13+
14+
public function getClass(): string
15+
{
16+
return 'Symfony\Bundle\FrameworkBundle\KernelBrowser';
17+
}
18+
19+
public function isMethodSupported(MethodReflection $methodReflection): bool
20+
{
21+
return $methodReflection->getName() === 'getContainer';
22+
}
23+
24+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
25+
{
26+
return new TestContainerType();
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony\Container;
4+
5+
use PhpParser\Node\Expr\StaticCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
9+
use PHPStan\Type\Type;
10+
11+
final class KernelTestCaseGetContainerDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
12+
{
13+
14+
public function getClass(): string
15+
{
16+
return 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase';
17+
}
18+
19+
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
20+
{
21+
return $methodReflection->getName() === 'getContainer';
22+
}
23+
24+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
25+
{
26+
return new TestContainerType();
27+
}
28+
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony\Container;
4+
5+
use PHPStan\Reflection\ClassReflection;
6+
use PHPStan\Type\ObjectType;
7+
use PHPStan\Type\Type;
8+
9+
class TestContainerType extends ObjectType
10+
{
11+
12+
public function __construct(
13+
string $class = 'Symfony\Component\DependencyInjection\ContainerInterface',
14+
?Type $subtractedType = null,
15+
?ClassReflection $classReflection = null
16+
) {
17+
parent::__construct($class, $subtractedType, $classReflection);
18+
}
19+
20+
}

‎stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.stub‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,21 @@ 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+
90+
public static function getAdditionalConfigFiles(): array
91+
{
92+
return [
93+
__DIR__ . '/container-extensions.neon',
94+
];
95+
}
96+
8097
}

‎tests/Rules/Symfony/ExampleTest.php‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /