|
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\ReflectionAttribute; |
10 |
| -use PHPStan\Reflection\ClassReflection; |
11 | 8 | use PHPStan\Rules\Rule;
|
12 | 9 | use PHPStan\Rules\RuleErrorBuilder;
|
| 10 | +use PHPStan\Symfony\AutowireLoaderServiceMapFactory; |
| 11 | +use PHPStan\Symfony\DefaultServiceMap; |
13 | 12 | use PHPStan\Symfony\ServiceMap;
|
14 | 13 | use PHPStan\TrinaryLogic;
|
15 | 14 | use PHPStan\Type\ObjectType;
|
16 | 15 | use PHPStan\Type\Type;
|
17 |
| -use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; |
18 |
| -use function class_exists; |
| 16 | +use function is_null; |
19 | 17 | use function sprintf;
|
20 | 18 |
|
21 | 19 | /**
|
@@ -78,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
|
78 | 76 | $isContainerInterfaceType = $isContainerType->yes() || $isPsrContainerType->yes();
|
79 | 77 | if (
|
80 | 78 | $isContainerInterfaceType &&
|
81 |
| - $this->isAutowireLocator($node, $scope, $serviceId) |
| 79 | + $this->isAutowireLocatorService($node, $scope, $serviceId) |
82 | 80 | ) {
|
83 | 81 | return [];
|
84 | 82 | }
|
@@ -107,61 +105,16 @@ private function isServiceSubscriber(Type $containerType, Scope $scope): Trinary
|
107 | 105 | return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType));
|
108 | 106 | }
|
109 | 107 |
|
110 |
| - private function isAutowireLocator(Node $node, Scope $scope, string $serviceId): bool |
| 108 | + private function isAutowireLocatorService(Node $node, Scope $scope, string $serviceId): bool |
111 | 109 | {
|
112 |
| - if (!class_exists('Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator')) { |
113 |
| - return false; |
114 |
| - } |
115 |
| - |
116 |
| - if ( |
117 |
| - !$node instanceof MethodCall |
118 |
| - ) { |
119 |
| - return false; |
120 |
| - } |
121 |
| - |
122 |
| - $nodeParentProperty = $node->var; |
123 |
| - |
124 |
| - if (!$nodeParentProperty instanceof Node\Expr\PropertyFetch) { |
125 |
| - return false; |
126 |
| - } |
| 110 | + $autowireLocatorServiceMapFactory = new AutowireLoaderServiceMapFactory($node, $scope); |
| 111 | + $autowireLocatorServiceMap = $autowireLocatorServiceMapFactory->create(); |
127 | 112 |
|
128 |
| - $nodeParentPropertyName = $nodeParentProperty->name; |
129 |
| - |
130 |
| - if (!$nodeParentPropertyName instanceof Node\Identifier) { |
131 |
| - return false; |
132 |
| - } |
133 |
| - |
134 |
| - $containerInterfacePropertyName = $nodeParentPropertyName->name; |
135 |
| - $scopeClassReflection = $scope->getClassReflection(); |
136 |
| - |
137 |
| - if (!$scopeClassReflection instanceof ClassReflection) { |
| 113 | + if (!$autowireLocatorServiceMap instanceof DefaultServiceMap) { |
138 | 114 | return false;
|
139 | 115 | }
|
140 | 116 |
|
141 |
| - $containerInterfacePropertyReflection = $scopeClassReflection |
142 |
| - ->getNativeProperty($containerInterfacePropertyName); |
143 |
| - $classPropertyReflection = $containerInterfacePropertyReflection->getNativeReflection(); |
144 |
| - $autowireLocatorAttributes = $classPropertyReflection->getAttributes(AutowireLocator::class); |
145 |
| - |
146 |
| - return $this->isAutowireLocatorService($autowireLocatorAttributes, $serviceId); |
147 |
| - } |
148 |
| - |
149 |
| - /** |
150 |
| - * @param array<int, FakeReflectionAttribute|ReflectionAttribute> $autowireLocatorAttributes |
151 |
| - */ |
152 |
| - private function isAutowireLocatorService(array $autowireLocatorAttributes, string $serviceId): bool |
153 |
| - { |
154 |
| - foreach ($autowireLocatorAttributes as $autowireLocatorAttribute) { |
155 |
| - /** @var AutowireLocator $autowireLocatorInstance */ |
156 |
| - $autowireLocator = $autowireLocatorAttribute->newInstance(); |
157 |
| - $autowireLocatorServices = $autowireLocator->value->getValues(); |
158 |
| - |
159 |
| - if (array_key_exists($serviceId, $autowireLocatorServices)) { |
160 |
| - return true; |
161 |
| - } |
162 |
| - } |
163 |
| - |
164 |
| - return false; |
| 117 | + return !is_null($autowireLocatorServiceMap->getService($serviceId)); |
165 | 118 | }
|
166 | 119 |
|
167 | 120 | }
|
0 commit comments