diff --git a/phpstan.neon b/phpstan.neon index a690755c..66c99036 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,7 +6,7 @@ includes: parameters: excludes_analyse: - */tests/tmp/* - - */tests/*/ExampleContainer.php - - */tests/*/ExampleController.php - - */tests/*/ExampleServiceSubscriber.php + - */tests/*/Example*.php + - */tests/*/header_bag_get.php - */tests/*/request_get_content.php + - */tests/*/serializer.php diff --git a/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php b/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php index d95e7761..b9e978a6 100644 --- a/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php +++ b/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php @@ -49,8 +49,9 @@ public function processNode(Node $node, Scope $scope): array $argType = $scope->getType($node->var); $isTestContainerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer'))->isSuperTypeOf($argType); - $isServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType); - if ($isTestContainerType->yes() || $isServiceSubscriber->yes()) { + $isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType); + $isServiceSubscriber = (new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface'))->isSuperTypeOf($argType); + if ($isTestContainerType->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes()) { return []; } diff --git a/tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php b/tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php index f6329dc4..abe71fed 100644 --- a/tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php +++ b/tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php @@ -29,15 +29,33 @@ public function testGetPrivateService(): void ); } - public function testGetPrivateServiceInServiceSubscriber(): void + public function testGetPrivateServiceInLegacyServiceSubscriber(): void { if (!interface_exists('Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface')) { self::markTestSkipped('The test needs Symfony\Component\DependencyInjection\ServiceSubscriberInterface class.'); } + $this->analyse( + [ + __DIR__ . '/ExampleLegacyServiceSubscriber.php', + __DIR__ . '/ExampleLegacyServiceSubscriberFromAbstractController.php', + __DIR__ . '/ExampleLegacyServiceSubscriberFromLegacyController.php', + ], + [] + ); + } + + public function testGetPrivateServiceInServiceSubscriber(): void + { + if (!interface_exists('Symfony\Contracts\Service\ServiceSubscriberInterface')) { + self::markTestSkipped('The test needs Symfony\Contracts\Service\ServiceSubscriberInterface class.'); + } + $this->analyse( [ __DIR__ . '/ExampleServiceSubscriber.php', + __DIR__ . '/ExampleServiceSubscriberFromAbstractController.php', + __DIR__ . '/ExampleServiceSubscriberFromLegacyController.php', ], [] ); diff --git a/tests/Rules/Symfony/ExampleLegacyServiceSubscriber.php b/tests/Rules/Symfony/ExampleLegacyServiceSubscriber.php new file mode 100644 index 00000000..63f93679 --- /dev/null +++ b/tests/Rules/Symfony/ExampleLegacyServiceSubscriber.php @@ -0,0 +1,23 @@ +get('private'); + } + + /** + * @return string[] + */ + public static function getSubscribedServices(): array + { + return []; + } + +} diff --git a/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromAbstractController.php b/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromAbstractController.php new file mode 100644 index 00000000..a090ea6e --- /dev/null +++ b/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromAbstractController.php @@ -0,0 +1,24 @@ +get('private'); + } + + /** + * @return string[] + */ + public static function getSubscribedServices(): array + { + return []; + } + +} diff --git a/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromLegacyController.php b/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromLegacyController.php new file mode 100644 index 00000000..af6af3dd --- /dev/null +++ b/tests/Rules/Symfony/ExampleLegacyServiceSubscriberFromLegacyController.php @@ -0,0 +1,24 @@ +get('private'); + } + + /** + * @return string[] + */ + public static function getSubscribedServices(): array + { + return []; + } + +} diff --git a/tests/Rules/Symfony/ExampleServiceSubscriber.php b/tests/Rules/Symfony/ExampleServiceSubscriber.php index afd20128..c9a009d2 100644 --- a/tests/Rules/Symfony/ExampleServiceSubscriber.php +++ b/tests/Rules/Symfony/ExampleServiceSubscriber.php @@ -2,10 +2,9 @@ namespace PHPStan\Rules\Symfony; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; +use Symfony\Contracts\Service\ServiceSubscriberInterface; -final class ExampleServiceSubscriber extends Controller implements ServiceSubscriberInterface +final class ExampleServiceSubscriber implements ServiceSubscriberInterface { public function privateService(): void diff --git a/tests/Rules/Symfony/ExampleServiceSubscriberFromAbstractController.php b/tests/Rules/Symfony/ExampleServiceSubscriberFromAbstractController.php new file mode 100644 index 00000000..ea7c6b36 --- /dev/null +++ b/tests/Rules/Symfony/ExampleServiceSubscriberFromAbstractController.php @@ -0,0 +1,23 @@ +get('private'); + } + + /** + * @return string[] + */ + public static function getSubscribedServices(): array + { + return []; + } + +} diff --git a/tests/Rules/Symfony/ExampleServiceSubscriberFromLegacyController.php b/tests/Rules/Symfony/ExampleServiceSubscriberFromLegacyController.php new file mode 100644 index 00000000..73610870 --- /dev/null +++ b/tests/Rules/Symfony/ExampleServiceSubscriberFromLegacyController.php @@ -0,0 +1,24 @@ +get('private'); + } + + /** + * @return string[] + */ + public static function getSubscribedServices(): array + { + return []; + } + +}