|
7 | 7 | use Lookyman\PHPStan\Symfony\ServiceMap; |
8 | 8 | use PHPStan\Analyser\Scope; |
9 | 9 | use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Type\ObjectType; |
10 | 11 | use PhpParser\Node; |
11 | 12 | use PhpParser\Node\Arg; |
12 | 13 | use PhpParser\Node\Expr\MethodCall; |
@@ -34,16 +35,21 @@ public function getNodeType(): string |
34 | 35 | public function processNode(Node $node, Scope $scope): array |
35 | 36 | { |
36 | 37 | $services = $this->serviceMap->getServices(); |
37 | | - return $node instanceof MethodCall |
38 | | - && $node->name === 'get' |
39 | | - && $scope->getType($node->var)->getClass() === ContainerInterface::class |
40 | | - && isset($node->args[0]) |
41 | | - && $node->args[0] instanceof Arg |
42 | | - && $node->args[0]->value instanceof String_ |
43 | | - && \array_key_exists($node->args[0]->value->value, $services) |
44 | | - && !$services[$node->args[0]->value->value]['public'] |
45 | | - ? [\sprintf('Service "%s" is private.', $node->args[0]->value->value)] |
46 | | - : []; |
| 38 | + if ($node instanceof MethodCall && $node->name === 'get') { |
| 39 | + $type = $scope->getType($node->var); |
| 40 | + if (!$type instanceof ObjectType) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + return $type->getClassName() === ContainerInterface::class |
| 44 | + && isset($node->args[0]) |
| 45 | + && $node->args[0] instanceof Arg |
| 46 | + && $node->args[0]->value instanceof String_ |
| 47 | + && \array_key_exists($node->args[0]->value->value, $services) |
| 48 | + && !$services[$node->args[0]->value->value]['public'] |
| 49 | + ? [\sprintf('Service "%s" is private.', $node->args[0]->value->value)] |
| 50 | + : []; |
| 51 | + } |
| 52 | + return []; |
47 | 53 | } |
48 | 54 |
|
49 | 55 | } |
0 commit comments