-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathContainerInterfacePrivateServiceRule.php
More file actions
129 lines (108 loc) · 4.47 KB
/
ContainerInterfacePrivateServiceRule.php
File metadata and controls
129 lines (108 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\ExternalFileDependencyRegistrar;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Symfony\ServiceMap;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function sprintf;
/**
* @implements Rule<MethodCall>
*/
final class ContainerInterfacePrivateServiceRule implements Rule
{
private ServiceMap $serviceMap;
private ?string $containerXmlPath;
private ?ExternalFileDependencyRegistrar $externalFileDependencyRegistrar;
public function __construct(
ServiceMap $symfonyServiceMap,
?string $containerXmlPath = null,
?ExternalFileDependencyRegistrar $externalFileDependencyRegistrar = null,
)
{
$this->serviceMap = $symfonyServiceMap;
$this->containerXmlPath = $containerXmlPath;
$this->externalFileDependencyRegistrar = $externalFileDependencyRegistrar;
}
public function getNodeType(): string
{
return MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
}
if ($node->name->name !== 'get' || !isset($node->getArgs()[0])) {
return [];
}
$argType = $scope->getType($node->var);
$isTestContainer = $this->isTestContainer($argType, $scope);
$isOldServiceSubscriber = (new ObjectType('Symfony\Component\DependencyInjection\ServiceSubscriberInterface'))->isSuperTypeOf($argType);
$isServiceSubscriber = $this->isServiceSubscriber($argType, $scope);
$isServiceLocator = (new ObjectType('Symfony\Component\DependencyInjection\ServiceLocator'))->isSuperTypeOf($argType);
if ($isTestContainer->yes() || $isOldServiceSubscriber->yes() || $isServiceSubscriber->yes() || $isServiceLocator->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);
$isPsrContainerType = (new ObjectType('Psr\Container\ContainerInterface'))->isSuperTypeOf($argType);
if (
!$isControllerType->yes()
&& !$isAbstractControllerType->yes()
&& !$isContainerType->yes()
&& !$isPsrContainerType->yes()
) {
return [];
}
$serviceId = $this->serviceMap::getServiceIdFromNode($node->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
if ($this->containerXmlPath !== null && $this->externalFileDependencyRegistrar !== null) {
$this->externalFileDependencyRegistrar->add($this->containerXmlPath);
}
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && !$service->isPublic()) {
return [
RuleErrorBuilder::message(sprintf('Service "%s" is private.', $serviceId))
->identifier('symfonyContainer.privateService')
->build(),
];
}
}
return [];
}
private function isServiceSubscriber(Type $containerType, Scope $scope): TrinaryLogic
{
$serviceSubscriberInterfaceType = new ObjectType('Symfony\Contracts\Service\ServiceSubscriberInterface');
$isContainerServiceSubscriber = $serviceSubscriberInterfaceType->isSuperTypeOf($containerType)->result;
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $isContainerServiceSubscriber;
}
$containedClassType = new ObjectType($classReflection->getName());
return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType)->result);
}
private function isTestContainer(Type $containerType, Scope $scope): TrinaryLogic
{
$testContainer = new ObjectType('Symfony\Bundle\FrameworkBundle\Test\TestContainer');
$isTestContainer = $testContainer->isSuperTypeOf($containerType)->result;
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $isTestContainer;
}
$containerInterface = new ObjectType('Symfony\Component\DependencyInjection\ContainerInterface');
$kernelTestCase = new ObjectType('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase');
$containedClassType = new ObjectType($classReflection->getName());
return $isTestContainer->or(
$containerInterface->isSuperTypeOf($containerType)->result->and(
$kernelTestCase->isSuperTypeOf($containedClassType)->result,
),
);
}
}