Skip to content

Commit 9336900

Browse files
Kai Eichingerlookyman
Kai Eichinger
authored andcommitted
Honor new contracts namespace as-of Symfony 4.2 for ServiceSubscriberInterface
In Symfony 4.2 the ServiceSubscriberInterface has been moved into its own contracts namespace. This commit adds support for the new interface and adds more test-coverage for all possible cases/combinations
1 parent b6f96d7 commit 9336900

8 files changed

+141
-6
lines changed

Diff for: src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public function processNode(Node $node, Scope $scope): array
4949
$argType = $scope->getType($node->var);
5050

5151
$isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType);
52-
$isServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
53-
if ($isTestContainerType->yes() || $isServiceSubscriber->yes()) {
52+
$isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
53+
$isServiceSubscriber = (new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
54+
if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes()) {
5455
return [];
5556
}
5657

Diff for: tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,33 @@ public function testGetPrivateService(): void
2929
);
3030
}
3131

32-
public function testGetPrivateServiceInServiceSubscriber(): void
32+
public function testGetPrivateServiceInLegacyServiceSubscriber(): void
3333
{
3434
if (!interface_exists('Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface')) {
3535
self::markTestSkipped('The test needs Symfony\Component\DependencyInjection\ServiceSubscriberInterface class.');
3636
}
3737

38+
$this->analyse(
39+
[
40+
__DIR__ . '/ExampleLegacyServiceSubscriber.php',
41+
__DIR__ . '/ExampleLegacyServiceSubscriberFromAbstractController.php',
42+
__DIR__ . '/ExampleLegacyServiceSubscriberFromLegacyController.php',
43+
],
44+
[]
45+
);
46+
}
47+
48+
public function testGetPrivateServiceInServiceSubscriber(): void
49+
{
50+
if (!interface_exists('Symfony\Contracts\Service\ServiceSubscriberInterface')) {
51+
self::markTestSkipped('The test needs Symfony\Contracts\Service\ServiceSubscriberInterface class.');
52+
}
53+
3854
$this->analyse(
3955
[
4056
__DIR__ . '/ExampleServiceSubscriber.php',
57+
__DIR__ . '/ExampleServiceSubscriberFromAbstractController.php',
58+
__DIR__ . '/ExampleServiceSubscriberFromLegacyController.php',
4159
],
4260
[]
4361
);
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
6+
7+
final class ExampleLegacyServiceSubscriber implements ServiceSubscriberInterface
8+
{
9+
public function privateService(): void
10+
{
11+
$this->get('private');
12+
}
13+
14+
/**
15+
* @return string[]
16+
*/
17+
public static function getSubscribedServices(): array
18+
{
19+
return [];
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
7+
8+
final class ExampleLegacyServiceSubscriberFromAbstractController extends AbstractController implements ServiceSubscriberInterface
9+
{
10+
11+
public function privateService(): void
12+
{
13+
$this->get('private');
14+
}
15+
16+
/**
17+
* @return string[]
18+
*/
19+
public static function getSubscribedServices(): array
20+
{
21+
return [];
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
7+
8+
final class ExampleLegacyServiceSubscriberFromLegacyController extends Controller implements ServiceSubscriberInterface
9+
{
10+
11+
public function privateService(): void
12+
{
13+
$this->get('private');
14+
}
15+
16+
/**
17+
* @return string[]
18+
*/
19+
public static function getSubscribedServices(): array
20+
{
21+
return [];
22+
}
23+
24+
}

Diff for: tests/Rules/Symfony/ExampleServiceSubscriber.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace PHPStan\Rules\Symfony;
44

5-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
5+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
76

8-
final class ExampleServiceSubscriber extends Controller implements ServiceSubscriberInterface
7+
final class ExampleServiceSubscriber implements ServiceSubscriberInterface
98
{
109

1110
public function privateService(): void
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\Controller\AbstractController;
6+
7+
final class ExampleServiceSubscriberFromAbstractController extends AbstractController
8+
{
9+
10+
public function privateService(): void
11+
{
12+
$this->get('private');
13+
}
14+
15+
/**
16+
* @return string[]
17+
*/
18+
public static function getSubscribedServices(): array
19+
{
20+
return [];
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
7+
8+
final class ExampleServiceSubscriberFromLegacyController extends Controller implements ServiceSubscriberInterface
9+
{
10+
11+
public function privateService(): void
12+
{
13+
$this->get('private');
14+
}
15+
16+
/**
17+
* @return string[]
18+
*/
19+
public static function getSubscribedServices(): array
20+
{
21+
return [];
22+
}
23+
24+
}

0 commit comments

Comments
 (0)