Skip to content

Commit a05e633

Browse files
committed
Repository should always respect the base repositoryClass from configuration
1 parent d9f64ae commit a05e633

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php

Lines changed: 18 additions & 3 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,7 +49,10 @@ public function getTypeFromMethodCall(
4849
): Type
4950
{
5051
if (count($methodCall->args) === 0) {
51-
return $this->getDefaultReturnType($scope, $methodCall->args, $methodReflection);
52+
return new GenericObjectType(
53+
$this->metadataResolver->getResolvedRepositoryClass(),
54+
[new ObjectWithoutClassType()]
55+
);
5256
}
5357
$argType = $scope->getType($methodCall->args[0]->value);
5458
if ($argType instanceof ConstantStringType) {
@@ -57,7 +61,10 @@ public function getTypeFromMethodCall(
5761
} elseif ($argType instanceof GenericClassStringType) {
5862
$classType = $argType->getGenericType();
5963
if (!$classType instanceof TypeWithClassName) {
60-
return $this->getDefaultReturnType($scope, $methodCall->args, $methodReflection);
64+
return new GenericObjectType(
65+
$this->metadataResolver->getResolvedRepositoryClass(),
66+
[$classType]
67+
);
6168
}
6269

6370
$objectName = $classType->getClassName();
@@ -84,11 +91,19 @@ public function getTypeFromMethodCall(
8491
*/
8592
private function getDefaultReturnType(Scope $scope, array $args, MethodReflection $methodReflection): Type
8693
{
87-
return ParametersAcceptorSelector::selectFromArgs(
94+
$defaultType = ParametersAcceptorSelector::selectFromArgs(
8895
$scope,
8996
$args,
9097
$methodReflection->getVariants()
9198
)->getReturnType();
99+
if ($defaultType instanceof GenericObjectType && count($defaultType->getTypes()) > 0) {
100+
return new GenericObjectType(
101+
$this->metadataResolver->getResolvedRepositoryClass(),
102+
[$defaultType->getTypes()[0]]
103+
);
104+
}
105+
106+
return $defaultType;
92107
}
93108

94109
}

src/Type/Doctrine/ObjectMetadataResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private function loadObjectManager(string $objectManagerLoader): ?ObjectManager
7070
return require $objectManagerLoader;
7171
}
7272

73-
private function getResolvedRepositoryClass(): string
73+
public function getResolvedRepositoryClass(): string
7474
{
7575
if ($this->resolvedRepositoryClass !== null) {
7676
return $this->resolvedRepositoryClass;

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<object>::unknownMethod().",
18+
"message": "Call to an undefined method Doctrine\\ORM\\EntityRepository<object>::unknownMethod().",
1919
"line": 64,
2020
"ignorable": true
2121
}

0 commit comments

Comments
 (0)