Skip to content

Commit 63165b7

Browse files
pepakrizondrejmirtes
authored andcommitted
Resolve ObjectRepository<T> as a ObjectRepository<object>
1 parent 86d9fa1 commit 63165b7

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\Generic\GenericClassStringType;
1111
use PHPStan\Type\Generic\GenericObjectType;
1212
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\ObjectWithoutClassType;
1314
use PHPStan\Type\Type;
1415
use PHPStan\Type\TypeWithClassName;
1516

@@ -48,9 +49,7 @@ public function getTypeFromMethodCall(
4849
): Type
4950
{
5051
if (count($methodCall->args) === 0) {
51-
return ParametersAcceptorSelector::selectSingle(
52-
$methodReflection->getVariants()
53-
)->getReturnType();
52+
return $this->getDefaultReturnType($methodReflection);
5453
}
5554
$argType = $scope->getType($methodCall->args[0]->value);
5655
if ($argType instanceof ConstantStringType) {
@@ -59,16 +58,12 @@ public function getTypeFromMethodCall(
5958
} elseif ($argType instanceof GenericClassStringType) {
6059
$classType = $argType->getGenericType();
6160
if (!$classType instanceof TypeWithClassName) {
62-
return ParametersAcceptorSelector::selectSingle(
63-
$methodReflection->getVariants()
64-
)->getReturnType();
61+
return $this->getDefaultReturnType($methodReflection);
6562
}
6663

6764
$objectName = $classType->getClassName();
6865
} else {
69-
return ParametersAcceptorSelector::selectSingle(
70-
$methodReflection->getVariants()
71-
)->getReturnType();
66+
return $this->getDefaultReturnType($methodReflection);
7267
}
7368

7469
$repositoryClass = $this->metadataResolver->getRepositoryClass($objectName);
@@ -78,4 +73,17 @@ public function getTypeFromMethodCall(
7873
]);
7974
}
8075

76+
private function getDefaultReturnType(MethodReflection $methodReflection): Type
77+
{
78+
$type = ParametersAcceptorSelector::selectSingle(
79+
$methodReflection->getVariants()
80+
)->getReturnType();
81+
82+
if ($type instanceof GenericObjectType) {
83+
$type = new GenericObjectType($type->getClassName(), [new ObjectWithoutClassType()]);
84+
}
85+
86+
return $type;
87+
}
88+
8189
}

tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn-2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"ignorable": true
1616
},
1717
{
18-
"message": "Call to an undefined method Doctrine\\Persistence\\ObjectRepository<T>::unknownMethod().",
18+
"message": "Call to an undefined method Doctrine\\Persistence\\ObjectRepository<object>::unknownMethod().",
1919
"line": 64,
2020
"ignorable": true
2121
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Call to an undefined method object::unknownMethod().",
4+
"line": 71,
5+
"ignorable": true
6+
}
7+
]

tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public function doSomethingWithRepository(string $entityName): void
6262
$repository = $this->entityManager->getRepository($entityName);
6363
$repository->getClassName();
6464
$repository->unknownMethod();
65+
$entity = $repository->find(1);
66+
67+
if ($entity === null) {
68+
throw new RuntimeException('Sorry, but no...');
69+
}
70+
71+
$entity->unknownMethod();
6572
}
6673
}
6774

0 commit comments

Comments
 (0)