10
10
use PHPStan \Reflection \ClassReflection ;
11
11
use PHPStan \Rules \Rule ;
12
12
use PHPStan \Rules \RuleErrorBuilder ;
13
- use PHPStan \Symfony \ServiceDefinition ;
14
13
use PHPStan \Symfony \ServiceMap ;
15
14
use PHPStan \TrinaryLogic ;
16
15
use PHPStan \Type \ObjectType ;
17
16
use PHPStan \Type \Type ;
18
17
use Symfony \Component \DependencyInjection \Attribute \AutowireLocator ;
19
18
use function class_exists ;
20
- use function get_class ;
21
19
use function sprintf ;
22
20
23
21
/**
@@ -77,20 +75,16 @@ public function processNode(Node $node, Scope $scope): array
77
75
return [];
78
76
}
79
77
80
- $ service = $ this ->serviceMap ->getService ($ serviceId );
81
- if (!$ service instanceof ServiceDefinition) {
82
- return [];
83
- }
84
-
85
78
$ isContainerInterfaceType = $ isContainerType ->yes () || $ isPsrContainerType ->yes ();
86
79
if (
87
80
$ isContainerInterfaceType &&
88
- $ this ->isAutowireLocator ($ node , $ scope , $ service )
81
+ $ this ->isAutowireLocator ($ node , $ scope , $ serviceId )
89
82
) {
90
83
return [];
91
84
}
92
85
93
- if (!$ service ->isPublic ()) {
86
+ $ service = $ this ->serviceMap ->getService ($ serviceId );
87
+ if ($ service !== null && !$ service ->isPublic ()) {
94
88
return [
95
89
RuleErrorBuilder::message (sprintf ('Service "%s" is private. ' , $ serviceId ))
96
90
->identifier ('symfonyContainer.privateService ' )
@@ -113,7 +107,7 @@ private function isServiceSubscriber(Type $containerType, Scope $scope): Trinary
113
107
return $ isContainerServiceSubscriber ->or ($ serviceSubscriberInterfaceType ->isSuperTypeOf ($ containedClassType ));
114
108
}
115
109
116
- private function isAutowireLocator (Node $ node , Scope $ scope , ServiceDefinition $ service ): bool
110
+ private function isAutowireLocator (Node $ node , Scope $ scope , string $ serviceId ): bool
117
111
{
118
112
if (!class_exists ('Symfony \\Component \\DependencyInjection \\Attribute \\AutowireLocator ' )) {
119
113
return false ;
@@ -149,41 +143,21 @@ private function isAutowireLocator(Node $node, Scope $scope, ServiceDefinition $
149
143
$ classPropertyReflection = $ containerInterfacePropertyReflection ->getNativeReflection ();
150
144
$ autowireLocatorAttributes = $ classPropertyReflection ->getAttributes (AutowireLocator::class);
151
145
152
- return $ this ->isAutowireLocatorService ($ autowireLocatorAttributes , $ service );
146
+ return $ this ->isAutowireLocatorService ($ autowireLocatorAttributes , $ serviceId );
153
147
}
154
148
155
149
/**
156
150
* @param array<int, FakeReflectionAttribute|ReflectionAttribute> $autowireLocatorAttributes
157
151
*/
158
- private function isAutowireLocatorService (array $ autowireLocatorAttributes , ServiceDefinition $ service ): bool
152
+ private function isAutowireLocatorService (array $ autowireLocatorAttributes , string $ serviceId ): bool
159
153
{
160
154
foreach ($ autowireLocatorAttributes as $ autowireLocatorAttribute ) {
161
- foreach ($ autowireLocatorAttribute ->getArgumentsExpressions () as $ autowireLocatorServices ) {
162
- if (!$ autowireLocatorServices instanceof Node \Expr \Array_) {
163
- continue ;
164
- }
165
-
166
- foreach ($ autowireLocatorServices ->items as $ autowireLocatorServiceNode ) {
167
- /** @var Node\Expr\ArrayItem $autowireLocatorServiceNode */
168
- $ autowireLocatorServiceExpr = $ autowireLocatorServiceNode ->value ;
169
-
170
- switch (get_class ($ autowireLocatorServiceExpr )) {
171
- case Node \Scalar \String_::class:
172
- $ autowireLocatorServiceClass = $ autowireLocatorServiceExpr ->value ;
173
- break ;
174
- case Node \Expr \ClassConstFetch::class:
175
- $ autowireLocatorServiceClass = $ autowireLocatorServiceExpr ->class instanceof Node \Name
176
- ? $ autowireLocatorServiceExpr ->class ->toString ()
177
- : null ;
178
- break ;
179
- default :
180
- $ autowireLocatorServiceClass = null ;
181
- }
182
-
183
- if ($ service ->getId () === $ autowireLocatorServiceClass ) {
184
- return true ;
185
- }
186
- }
155
+ /** @var AutowireLocator $autowireLocatorInstance */
156
+ $ autowireLocator = $ autowireLocatorAttribute ->newInstance ();
157
+ $ autowireLocatorServices = $ autowireLocator ->value ->getValues ();
158
+
159
+ if (array_key_exists ($ serviceId , $ autowireLocatorServices )) {
160
+ return true ;
187
161
}
188
162
}
189
163
0 commit comments