Skip to content

Honor new contracts namespace as-of Symfony 4.2 for ServiceSubscriberInterface #33

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
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
6 changes: 3 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down
20 changes: 19 additions & 1 deletion tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
[]
);
Expand Down
23 changes: 23 additions & 0 deletions tests/Rules/Symfony/ExampleLegacyServiceSubscriber.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\Component\DependencyInjection\ServiceSubscriberInterface;

final class ExampleLegacyServiceSubscriber implements ServiceSubscriberInterface
{

public function privateService(): void
{
$this->get('private');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [];
}

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

namespace PHPStan\Rules\Symfony;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;

final class ExampleLegacyServiceSubscriberFromAbstractController extends AbstractController implements ServiceSubscriberInterface
{

public function privateService(): void
{
$this->get('private');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [];
}

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

namespace PHPStan\Rules\Symfony;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;

final class ExampleLegacyServiceSubscriberFromLegacyController extends Controller implements ServiceSubscriberInterface
{

public function privateService(): void
{
$this->get('private');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [];
}

}
5 changes: 2 additions & 3 deletions tests/Rules/Symfony/ExampleServiceSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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\Controller\AbstractController;

final class ExampleServiceSubscriberFromAbstractController extends AbstractController
{

public function privateService(): void
{
$this->get('private');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [];
}

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

namespace PHPStan\Rules\Symfony;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

final class ExampleServiceSubscriberFromLegacyController extends Controller implements ServiceSubscriberInterface
{

public function privateService(): void
{
$this->get('private');
}

/**
* @return string[]
*/
public static function getSubscribedServices(): array
{
return [];
}

}