Skip to content

Commit 2c24080

Browse files
alexsakhnovondrejmirtes
authored andcommitted
Ignore ContainerBagInterface in ContainerInterfaceUnknownServiceRule
1 parent fe5f5d8 commit 2c24080

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function processNode(Node $node, Scope $scope): array
5555
}
5656

5757
$argType = $scope->getType($node->var);
58+
$isContainerBagType = (new ObjectType('Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface'))->isSuperTypeOf($argType);
59+
if ($isContainerBagType->yes()) {
60+
return [];
61+
}
62+
5863
$isControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller'))->isSuperTypeOf($argType);
5964
$isAbstractControllerType = (new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($argType);
6065
$isContainerType = (new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface'))->isSuperTypeOf($argType);

tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,18 @@ public function testGetPrivateServiceInAbstractController(): void
6868
);
6969
}
7070

71+
public function testGetPrivateServiceInLegacyServiceSubscriber(): void
72+
{
73+
if (!interface_exists('Symfony\Contracts\Service\ServiceSubscriberInterface')) {
74+
self::markTestSkipped('The test needs Symfony\Contracts\Service\ServiceSubscriberInterface class.');
75+
}
76+
77+
$this->analyse(
78+
[
79+
__DIR__ . '/ExampleServiceSubscriber.php',
80+
],
81+
[]
82+
);
83+
}
84+
7185
}

tests/Rules/Symfony/ExampleServiceSubscriber.php

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public function privateService(): void
1212
$this->get('private');
1313
}
1414

15+
public function containerParameter(): void
16+
{
17+
/** @var \Symfony\Component\DependencyInjection\ParameterBag\ContainerBag $containerBag */
18+
$containerBag = doFoo();
19+
$containerBag->get('parameter_name');
20+
}
21+
1522
/**
1623
* @return string[]
1724
*/

0 commit comments

Comments
 (0)