Skip to content

Commit 2d46724

Browse files
committed
Add support for symfony Controllers, replace getClassName with instanceOf
1 parent 8e8fa73 commit 2d46724

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Rules/ContainerInterfacePrivateServiceRule.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Type\ObjectType;
11+
use PHPStan\Type\ThisType;
1112
use PhpParser\Node;
1213
use PhpParser\Node\Arg;
1314
use PhpParser\Node\Expr\MethodCall;
@@ -34,8 +35,10 @@ public function processNode(Node $node, Scope $scope): array
3435
{
3536
if ($node instanceof MethodCall && $node->name === 'get') {
3637
$type = $scope->getType($node->var);
37-
if ($type instanceof ObjectType
38-
&& \in_array($type->getClassName(), ['Symfony\Component\DependencyInjection\ContainerInterface', 'Symfony\Bundle\FrameworkBundle\Controller\Controller'], \true)
38+
$baseController = new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
39+
$isInstanceOfController = $type instanceof ThisType && $baseController->isSuperTypeOf($type)->yes();
40+
$isContainerInterface = $type instanceof ObjectType && $type->getClassName() === 'Symfony\Component\DependencyInjection\ContainerInterface';
41+
if (($isContainerInterface || $isInstanceOfController)
3942
&& isset($node->args[0])
4043
&& $node->args[0] instanceof Arg
4144
) {

src/Rules/ContainerInterfaceUnknownServiceRule.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Type\ObjectType;
11+
use PHPStan\Type\ThisType;
1112
use PhpParser\Node;
1213
use PhpParser\Node\Arg;
1314
use PhpParser\Node\Expr\MethodCall;
@@ -34,8 +35,10 @@ public function processNode(Node $node, Scope $scope): array
3435
{
3536
if ($node instanceof MethodCall && $node->name === 'get') {
3637
$type = $scope->getType($node->var);
37-
if ($type instanceof ObjectType
38-
&& \in_array($type->getClassName(), ['Symfony\Component\DependencyInjection\ContainerInterface', 'Symfony\Bundle\FrameworkBundle\Controller\Controller'], \true)
38+
$baseController = new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\Controller');
39+
$isInstanceOfController = $type instanceof ThisType && $baseController->isSuperTypeOf($type)->yes();
40+
$isContainerInterface = $type instanceof ObjectType && $type->getClassName() === 'Symfony\Component\DependencyInjection\ContainerInterface';
41+
if (($isInstanceOfController || $isContainerInterface)
3942
&& isset($node->args[0])
4043
&& $node->args[0] instanceof Arg
4144
) {

0 commit comments

Comments
 (0)