Skip to content

Commit c5f7d3d

Browse files
authored
Merge pull request #33 from keichinger/updated-servicesubscriberinterface
Honor new contracts namespace as-of Symfony 4.2 for ServiceSubscriberInterface
2 parents b6f96d7 + d9c56f7 commit c5f7d3d

9 files changed

+145
-9
lines changed

phpstan.neon

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ includes:
66
parameters:
77
excludes_analyse:
88
- */tests/tmp/*
9-
- */tests/*/ExampleContainer.php
10-
- */tests/*/ExampleController.php
11-
- */tests/*/ExampleServiceSubscriber.php
9+
- */tests/*/Example*.php
10+
- */tests/*/header_bag_get.php
1211
- */tests/*/request_get_content.php
12+
- */tests/*/serializer.php

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

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
);
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\Component\DependencyInjection\ServiceSubscriberInterface;
6+
7+
final class ExampleLegacyServiceSubscriber implements ServiceSubscriberInterface
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\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+
}

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)