5
5
use PhpParser \Node ;
6
6
use PhpParser \Node \Expr \MethodCall ;
7
7
use PHPStan \Analyser \Scope ;
8
+ use PHPStan \BetterReflection \Reflection \Adapter \FakeReflectionAttribute ;
9
+ use PHPStan \BetterReflection \Reflection \Adapter \ReflectionProperty ;
10
+ use PHPStan \BetterReflection \Reflection \ReflectionAttribute ;
11
+ use PHPStan \Reflection \ClassReflection ;
12
+ use PHPStan \Reflection \Php \PhpPropertyReflection ;
8
13
use PHPStan \Rules \Rule ;
9
14
use PHPStan \Rules \RuleErrorBuilder ;
15
+ use PHPStan \Symfony \ServiceDefinition ;
10
16
use PHPStan \Symfony \ServiceMap ;
11
17
use PHPStan \TrinaryLogic ;
12
18
use PHPStan \Type \ObjectType ;
@@ -66,15 +72,29 @@ public function processNode(Node $node, Scope $scope): array
66
72
}
67
73
68
74
$ serviceId = $ this ->serviceMap ::getServiceIdFromNode ($ node ->getArgs ()[0 ]->value , $ scope );
69
- if ($ serviceId !== null ) {
70
- $ service = $ this ->serviceMap ->getService ($ serviceId );
71
- if ($ service !== null && !$ service ->isPublic ()) {
72
- return [
73
- RuleErrorBuilder::message (sprintf ('Service "%s" is private. ' , $ serviceId ))
74
- ->identifier ('symfonyContainer.privateService ' )
75
- ->build (),
76
- ];
77
- }
75
+ if ($ serviceId === null ) {
76
+ return [];
77
+ }
78
+
79
+ $ service = $ this ->serviceMap ->getService ($ serviceId );
80
+ if (!$ service instanceof ServiceDefinition) {
81
+ return [];
82
+ }
83
+
84
+ $ isContainerInterfaceType = $ isContainerType ->yes () || $ isPsrContainerType ->yes ();
85
+ if (
86
+ $ isContainerInterfaceType &&
87
+ $ this ->isAutowireLocator ($ node , $ scope , $ service )
88
+ ) {
89
+ return [];
90
+ }
91
+
92
+ if (!$ service ->isPublic ()) {
93
+ return [
94
+ RuleErrorBuilder::message (sprintf ('Service "%s" is private. ' , $ serviceId ))
95
+ ->identifier ('symfonyContainer.privateService ' )
96
+ ->build (),
97
+ ];
78
98
}
79
99
80
100
return [];
@@ -92,4 +112,80 @@ private function isServiceSubscriber(Type $containerType, Scope $scope): Trinary
92
112
return $ isContainerServiceSubscriber ->or ($ serviceSubscriberInterfaceType ->isSuperTypeOf ($ containedClassType ));
93
113
}
94
114
115
+ private function isAutowireLocator (Node $ node , Scope $ scope , ServiceDefinition $ service ): bool
116
+ {
117
+ if (
118
+ !$ node instanceof MethodCall
119
+ ) {
120
+ return false ;
121
+ }
122
+
123
+ $ nodeParentProperty = $ node ->var ;
124
+
125
+ if (!$ nodeParentProperty instanceof Node \Expr \PropertyFetch) {
126
+ return false ;
127
+ }
128
+
129
+ $ nodeParentPropertyName = $ nodeParentProperty ->name ;
130
+
131
+ if (!$ nodeParentPropertyName instanceof Node \Identifier) {
132
+ return false ;
133
+ }
134
+
135
+ $ containerInterfacePropertyName = $ nodeParentPropertyName ->name ;
136
+ $ classProperty = $ scope ->getClassReflection ();
137
+
138
+ if (!$ classProperty instanceof ClassReflection) {
139
+ return false ;
140
+ }
141
+
142
+ $ classProperty ->getProperty ($ containerInterfacePropertyName , $ scope );
143
+
144
+ if (!$ classProperty instanceof PhpPropertyReflection) {
145
+ return false ;
146
+ }
147
+
148
+ $ classPropertyReflection = $ classProperty ->getNativeReflection ();
149
+ $ autowireLocatorAttributes = $ classPropertyReflection
150
+ ->getAttributes ('Symfony\Component\DependencyInjection\Attribute\AutowireLocator ' );
151
+
152
+ return $ this ->isAutowireLocatorService ($ autowireLocatorAttributes , $ service );
153
+ }
154
+
155
+ /**
156
+ * @param array<FakeReflectionAttribute|ReflectionAttribute> $autowireLocatorAttributes
157
+ */
158
+ private function isAutowireLocatorService (array $ autowireLocatorAttributes , ServiceDefinition $ service ): bool
159
+ {
160
+ 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 $autowireLocatorService */
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 ->toString ();
176
+ break ;
177
+ default :
178
+ $ autowireLocatorServiceClass = null ;
179
+ }
180
+
181
+ if ($ service ->getId () === $ autowireLocatorServiceClass ) {
182
+ return true ;
183
+ }
184
+ }
185
+ }
186
+ }
187
+
188
+ return false ;
189
+ }
190
+
95
191
}
0 commit comments