Skip to content

Commit

Permalink
Ignore ContainerBagInterface in ContainerInterfaceUnknownServiceRule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsakhnov authored and ondrejmirtes committed Aug 18, 2021
1 parent fe5f5d8 commit 2c24080
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function processNode(Node $node, Scope $scope): array
}

$argType = $scope->getType($node->var);
$isContainerBagType = (new ObjectType('Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface'))->isSuperTypeOf($argType);
if ($isContainerBagType->yes()) {
return [];
}

$isControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller'))->isSuperTypeOf($argType);
$isAbstractControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($argType);
$isContainerType = (new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface'))->isSuperTypeOf($argType);
Expand Down
14 changes: 14 additions & 0 deletions tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ public function testGetPrivateServiceInAbstractController(): void
);
}

public function testGetPrivateServiceInLegacyServiceSubscriber(): void
{
if (!interface_exists('Symfony\Contracts\Service\ServiceSubscriberInterface')) {
self::markTestSkipped('The test needs Symfony\Contracts\Service\ServiceSubscriberInterface class.');
}

$this->analyse(
[
__DIR__ . '/ExampleServiceSubscriber.php',
],
[]
);
}

}
7 changes: 7 additions & 0 deletions tests/Rules/Symfony/ExampleServiceSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public function privateService(): void
$this->get('private');
}

public function containerParameter(): void
{
/** @var \Symfony\Component\DependencyInjection\ParameterBag\ContainerBag $containerBag */
$containerBag = doFoo();
$containerBag->get('parameter_name');
}

/**
* @return string[]
*/
Expand Down

0 comments on commit 2c24080

Please sign in to comment.