Skip to content

Commit 3512e21

Browse files
committed
Basic support for simple Symfony #[AutowireLocator] attribute
https://symfony.com/blog/new-in-symfony-6-4-autowirelocator-and-autowireiterator-attributes
1 parent c7b7e7f commit 3512e21

File tree

1 file changed

+105
-9
lines changed

1 file changed

+105
-9
lines changed

src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\MethodCall;
77
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;
813
use PHPStan\Rules\Rule;
914
use PHPStan\Rules\RuleErrorBuilder;
15+
use PHPStan\Symfony\ServiceDefinition;
1016
use PHPStan\Symfony\ServiceMap;
1117
use PHPStan\TrinaryLogic;
1218
use PHPStan\Type\ObjectType;
@@ -66,15 +72,29 @@ public function processNode(Node $node, Scope $scope): array
6672
}
6773

6874
$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+
];
7898
}
7999

80100
return [];
@@ -92,4 +112,80 @@ private function isServiceSubscriber(Type $containerType, Scope $scope): Trinary
92112
return $isContainerServiceSubscriber->or($serviceSubscriberInterfaceType->isSuperTypeOf($containedClassType));
93113
}
94114

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+
95191
}

0 commit comments

Comments
 (0)