Skip to content

Do not report private services in test container #5

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"satooshi/php-coveralls": "^1.0",
"slevomat/coding-standard": "^4.5.2",
"phpstan/phpstan-phpunit": "^0.10",
"symfony/framework-bundle": "^3.0 || ^4.0"
"symfony/framework-bundle": "^4.1"
},
"conflict": {
"symfony/framework-bundle": "<3.0"
Expand Down
20 changes: 19 additions & 1 deletion src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
$argType = $scope->getType($node->var);
$isControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller'))->isSuperTypeOf($argType);
$isContainerType = (new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface'))->isSuperTypeOf($argType);
if (!$isControllerType->yes() && !$isContainerType->yes()) {
if ((!$isControllerType->yes() && !$isContainerType->yes()) || $this->isTestContainer($scope)) {
return [];
}

Expand All @@ -58,4 +58,22 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

private function isTestContainer(Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if ($classReflection === null || !$classReflection->isSubclassOf('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase')) {
return false;
}

$testContainer = $this->serviceMap->getService('test.service_container');
if ($testContainer === null) {
return false;
}
$class = $testContainer->getClass();

return $testContainer->isPublic()
&& $class !== null
&& (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf(new ObjectType($class))->yes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public function testGetPrivateService(): void
);
}

public function testGetPrivateServiceOnTestContainer(): void
{
$this->analyse(
[__DIR__ . '/../../Symfony/data/KernelTestExample.php'],
[]
);
}

}
2 changes: 1 addition & 1 deletion tests/Symfony/ServiceMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGetServiceIdFromNode(): void
$broker = $this->createBroker();
$printer = new Standard();
$typeSpecifier = $this->createTypeSpecifier($printer, $broker);
$scope = new Scope($broker, $printer, $typeSpecifier, ScopeContext::create(''));
$scope = new Scope($this->createScopeFactory($broker, $typeSpecifier), $broker, $printer, $typeSpecifier, ScopeContext::create(''));

self::assertSame('foo', ServiceMap::getServiceIdFromNode(new String_('foo'), $scope));
self::assertSame('bar', ServiceMap::getServiceIdFromNode(new ClassConstFetch(new Name(ExampleController::class), 'BAR'), $scope));
Expand Down
15 changes: 15 additions & 0 deletions tests/Symfony/data/KernelTestExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace PHPStan\Symfony;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class KernelTestExample extends KernelTestCase
{

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

}
1 change: 1 addition & 0 deletions tests/Symfony/data/container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<service id="private" class="Foo" public="false"></service>
<service id="synthetic" class="Foo" synthetic="true"></service>
<service id="alias" class="Bar" alias="withClass"></service>
<service id="test.service_container" class="Symfony\Bundle\FrameworkBundle\Test\TestContainer"></service>
</services>
</container>