Skip to content

Commit 7f47026

Browse files
authored
Fix synthetic service return type when class is available
1 parent 34f16fa commit 7f47026

6 files changed

+10
-1
lines changed

src/Type/Symfony/ServiceDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function getGetTypeFromMethodCall(
6868
$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope);
6969
if ($serviceId !== null) {
7070
$service = $this->serviceMap->getService($serviceId);
71-
if ($service !== null && !$service->isSynthetic()) {
71+
if ($service !== null && (!$service->isSynthetic() || $service->getClass() !== null)) {
7272
return new ObjectType($service->getClass() ?? $serviceId);
7373
}
7474
}

tests/Type/Symfony/container.xml

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838

3939
<services>
4040
<service id="foo" class="Foo"></service>
41+
<service id="synthetic" class="Synthetic" synthetic="true" />
4142
</services>
4243
</container>

tests/Type/Symfony/data/ExampleAbstractController.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ final class ExampleAbstractController extends AbstractController
1313
public function services(): void
1414
{
1515
assertType('Foo', $this->get('foo'));
16+
assertType('Synthetic', $this->get('synthetic'));
1617
assertType('object', $this->get('bar'));
1718
assertType('object', $this->get(doFoo()));
1819
assertType('object', $this->get());
1920

2021
assertType('true', $this->has('foo'));
22+
assertType('true', $this->has('synthetic'));
2123
assertType('false', $this->has('bar'));
2224
assertType('bool', $this->has(doFoo()));
2325
assertType('bool', $this->has());

tests/Type/Symfony/data/ExampleAbstractControllerWithoutContainer.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ final class ExampleAbstractControllerWithoutContainer extends AbstractController
1212
public function services(): void
1313
{
1414
assertType('object', $this->get('foo'));
15+
assertType('object', $this->get('synthetic'));
1516
assertType('object', $this->get('bar'));
1617
assertType('object', $this->get(doFoo()));
1718
assertType('object', $this->get());
1819

1920
assertType('bool', $this->has('foo'));
21+
assertType('bool', $this->has('synthetic'));
2022
assertType('bool', $this->has('bar'));
2123
assertType('bool', $this->has(doFoo()));
2224
assertType('bool', $this->has());

tests/Type/Symfony/data/ExampleController.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ final class ExampleController extends Controller
1313
public function services(): void
1414
{
1515
assertType('Foo', $this->get('foo'));
16+
assertType('Synthetic', $this->get('synthetic'));
1617
assertType('object', $this->get('bar'));
1718
assertType('object', $this->get(doFoo()));
1819
assertType('object', $this->get());
1920

2021
assertType('true', $this->has('foo'));
22+
assertType('true', $this->has('synthetic'));
2123
assertType('false', $this->has('bar'));
2224
assertType('bool', $this->has(doFoo()));
2325
assertType('bool', $this->has());

tests/Type/Symfony/data/ExampleControllerWithoutContainer.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ final class ExampleControllerWithoutContainer extends Controller
1212
public function services(): void
1313
{
1414
assertType('object', $this->get('foo'));
15+
assertType('object', $this->get('synthetic'));
1516
assertType('object', $this->get('bar'));
1617
assertType('object', $this->get(doFoo()));
1718
assertType('object', $this->get());
1819

1920
assertType('bool', $this->has('foo'));
21+
assertType('bool', $this->has('synthetic'));
2022
assertType('bool', $this->has('bar'));
2123
assertType('bool', $this->has(doFoo()));
2224
assertType('bool', $this->has());

0 commit comments

Comments
 (0)