Skip to content

Commit 814f14f

Browse files
committed
Add AutowireLoader support for ContainerInterfaceUnknownServiceRule
1 parent 681d648 commit 814f14f

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
11+
use PHPStan\Symfony\AutowireLoaderServiceMapFactory;
12+
use PHPStan\Symfony\DefaultServiceMap;
1113
use PHPStan\Symfony\ServiceMap;
1214
use PHPStan\Type\ObjectType;
1315
use PHPStan\Type\Symfony\Helper;
@@ -66,19 +68,52 @@ public function processNode(Node $node, Scope $scope): array
6668
}
6769

6870
$serviceId = $this->serviceMap::getServiceIdFromNode($node->getArgs()[0]->value, $scope);
69-
if ($serviceId !== null) {
70-
$service = $this->serviceMap->getService($serviceId);
71-
$serviceIdType = $scope->getType($node->getArgs()[0]->value);
72-
if ($service === null && !$scope->getType(Helper::createMarkerNode($node->var, $serviceIdType, $this->printer))->equals($serviceIdType)) {
71+
if ($serviceId === null) {
72+
return [];
73+
}
74+
75+
$isContainerInterfaceType = $isContainerType->yes() || $isPsrContainerType->yes();
76+
if ($isContainerInterfaceType) {
77+
$autowireLoaderResult = $this->getAutowireLoaderResult($node, $scope, $serviceId);
78+
79+
if (is_array($autowireLoaderResult)) {
80+
return $autowireLoaderResult;
81+
}
82+
}
83+
84+
$service = $this->serviceMap->getService($serviceId);
85+
$serviceIdType = $scope->getType($node->getArgs()[0]->value);
86+
if ($service === null && !$scope->getType(Helper::createMarkerNode($node->var, $serviceIdType, $this->printer))->equals($serviceIdType)) {
87+
return [
88+
RuleErrorBuilder::message(sprintf('Service "%s" is not registered in the container.', $serviceId))
89+
->identifier('symfonyContainer.serviceNotFound')
90+
->build(),
91+
];
92+
}
93+
94+
return [];
95+
}
96+
97+
private function getAutowireLoaderResult(Node $node, Scope $scope, string $serviceId): ?array {
98+
$autowireLocatorServiceMapFactory = new AutowireLoaderServiceMapFactory($node, $scope);
99+
$autowireLocatorServiceMap = $autowireLocatorServiceMapFactory->create();
100+
101+
// Our container has a valid AutowireLoader attribute, else we would get a FakeServiceMap.
102+
if ($autowireLocatorServiceMap instanceof DefaultServiceMap) {
103+
$autowireLocatorService = $autowireLocatorServiceMap->getService($serviceId);
104+
105+
if ($autowireLocatorService === null) {
73106
return [
74-
RuleErrorBuilder::message(sprintf('Service "%s" is not registered in the container.', $serviceId))
75-
->identifier('symfonyContainer.serviceNotFound')
107+
RuleErrorBuilder::message(sprintf('Service "%s" is not registered in the AutowireLocator.', $serviceId))
108+
->identifier('symfonyContainer.undefinedService')
76109
->build(),
77110
];
78111
}
112+
113+
return [];
79114
}
80115

81-
return [];
116+
return null;
82117
}
83118

84119
}

0 commit comments

Comments
 (0)