Skip to content

Avoid reporting with TestContainer::get as internal #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ parameters:
- stubs/Psr/Cache/CacheItemInterface.stub
- stubs/Psr/Cache/InvalidArgumentException.stub
- stubs/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.stub
- stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.stub
- stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.stub
- stubs/Symfony/Bundle/FrameworkBundle/Test/TestContainer.stub
- stubs/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AuthenticatorFactoryInterface.stub
- stubs/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/FirewallListenerFactoryInterface.stub
- stubs/Symfony/Component/Console/Command.stub
Expand Down Expand Up @@ -364,3 +361,9 @@ services:
class: PHPStan\Symfony\SymfonyContainerResultCacheMetaExtension
tags:
- phpstan.resultCacheMetaExtension
-
class: PHPStan\Type\Symfony\Container\KernelBrowserGetContainerDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: PHPStan\Type\Symfony\Container\KernelTestCaseGetContainerDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]
11 changes: 9 additions & 2 deletions src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Symfony\ServiceMap;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Container\TestContainerType;
use PHPStan\Type\Type;
use function sprintf;

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

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

$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
if (
$argType instanceof TestContainerType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify this and not report an error when we simply have the upper-most ContainerInterface and are in a subclass of KernelTestCase?

The custom type is wrong since it doesn't implement equals, describe and isSuperTypeOf. But I think we don't need it at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify this and not report an error when we simply have the upper-most ContainerInterface and are in a subclass of KernelTestCase?

I think this is not fully true, but this might be good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be better with the last commit.

|| (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType)->yes()
) {
return [];
}

$isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
$isServiceSubscriber = $this->isServiceSubscriber($argType, $scope);
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
if ($isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->yes()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony\Container;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class KernelBrowserGetContainerDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Symfony\Bundle\FrameworkBundle\KernelBrowser';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getContainer';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return new TestContainerType();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony\Container;

use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class KernelTestCaseGetContainerDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase';
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getContainer';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
return new TestContainerType();
}

}
21 changes: 21 additions & 0 deletions src/Type/Symfony/Container/TestContainerType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony\Container;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

class TestContainerType extends ObjectType
{

public function __construct(
string $class = 'Symfony\Component\DependencyInjection\ContainerInterface',
?Type $subtractedType = null,
?ClassReflection $classReflection = null
)
{
parent::__construct($class, $subtractedType, $classReflection);
}

}
13 changes: 0 additions & 13 deletions stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.stub

This file was deleted.

16 changes: 0 additions & 16 deletions stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.stub

This file was deleted.

7 changes: 0 additions & 7 deletions stubs/Symfony/Bundle/FrameworkBundle/Test/TestContainer.stub

This file was deleted.

17 changes: 17 additions & 0 deletions tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,21 @@ public function testGetPrivateServiceInServiceSubscriber(): void
);
}

public function testGetPrivateServiceInTest(): void
{
$this->analyse(
[
__DIR__ . '/ExampleTest.php',
],
[],
);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/container-extensions.neon',
];
}

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

namespace PHPStan\Rules\Symfony;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

abstract class ExampleTest extends KernelTestCase
{

public function bar(): void
{
$container = self::getContainer();
$container->get('private');
}

public function foo(KernelBrowser $browser): void
{
$container = $browser->getContainer();
$container->get('private');
}

}
7 changes: 7 additions & 0 deletions tests/Rules/Symfony/container-extensions.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
-
class: PHPStan\Type\Symfony\Container\KernelBrowserGetContainerDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: PHPStan\Type\Symfony\Container\KernelTestCaseGetContainerDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]