Skip to content

Services with parameterised class are not resolved #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Wirone opened this issue Jan 5, 2022 · 3 comments · Fixed by #254
Closed

Services with parameterised class are not resolved #226

Wirone opened this issue Jan 5, 2022 · 3 comments · Fixed by #254

Comments

@Wirone
Copy link
Contributor

Wirone commented Jan 5, 2022

I've installed phpstan/phpstan-symfony extension and analysed current codebase with configured container. I see many errors like Property Foo::$bar (Bar) does not accept %foo.bar.service.class% on $this->bar = $this->container->get('foo.bar.service'); when service has definition with parameterised class:

foo.bar.service:
  class: '%foo.bar.service.class%'

Such service definition is totally correct. It seems like it can't be resolved properly.

FYI: We don't have full Symfony framework, we use symfony/dependency-injection and dump container similarly like framework does. I don't know if that matters, but I thought I should point it out.

@ondrejmirtes
Copy link
Member

Looks like %foo.bar.service.class% is not expanded to the actual object class.

The code in question is here

private function getGetTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->getArgs()[0])) {
return $returnType;
}
$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && (!$service->isSynthetic() || $service->getClass() !== null)) {
return new ObjectType($service->getClass() ?? $serviceId);
}
}
return $returnType;
}
so feel free to debug it and see what's wrong.

@Wirone
Copy link
Contributor Author

Wirone commented Feb 9, 2022

@ondrejmirtes I've started work on this but I struggle to prepare fix. I've added parameter and service to tests/Symfony/container.xml, added scenario to PHPStan\Symfony\DefaultServiceMapTest::getServiceProvider() and I see error:

vendor/bin/phpunit tests/Symfony/DefaultServiceMapTest.php --filter=testGetService#2
PHPUnit 9.5.13 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 00:00.035, Memory: 6.00 MB

There was 1 failure:

1) PHPStan\Symfony\DefaultServiceMapTest::testGetService with data set #2 ('withParameterisedClass', Closure Object (...))
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'Foo'
+'%app.class%'

./phpstan/phpstan-symfony/tests/Symfony/DefaultServiceMapTest.php:55
./phpstan/phpstan-symfony/tests/Symfony/DefaultServiceMapTest.php:17

But when I work with XDebug I don't see that it goes through mentioned ServiceDynamicReturnTypeExtension::getGetTypeFromMethodCall() method. I see that services are resolved in XmlServiceMapFactory and are stored in DefaultServiceMap. Should parameterised class be resolved on this level? If not, could you help me where should I start (create new rule, add test case to another existing test class)?

Wirone pushed a commit to Wirone/phpstan-symfony that referenced this issue Feb 9, 2022
Wirone pushed a commit to Wirone/phpstan-symfony that referenced this issue Feb 9, 2022
Wirone pushed a commit to Wirone/phpstan-symfony that referenced this issue Feb 9, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants