Skip to content

Commit 6f599c2

Browse files
committed
Prevent crashes for unknown classes
1 parent f7ce984 commit 6f599c2

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/Rules/Doctrine/ORM/MagicRepositoryMethodCallRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function processNode(Node $node, Scope $scope): array
4848
}
4949

5050
$methodName = $methodNameIdentifier->toString();
51+
if (!$this->broker->hasClass($calledOnType->getClassName())) {
52+
return [];
53+
}
54+
5155
$repositoryReflectionClass = $this->broker->getClass($calledOnType->getClassName());
5256
if ($repositoryReflectionClass->hasNativeMethod($methodName)) {
5357
return [];

src/Type/Doctrine/ObjectRepositoryDynamicReturnTypeExtension.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@ public function getTypeFromMethodCall(
5353
}
5454

5555
$methodName = $methodReflection->getName();
56-
$repositoryClassReflection = $this->broker->getClass($calledOnType->getClassName());
57-
if (
58-
(
56+
if ($this->broker->hasClass($calledOnType->getClassName())) {
57+
$repositoryClassReflection = $this->broker->getClass($calledOnType->getClassName());
58+
if (
5959
(
60-
strpos($methodName, 'findBy') === 0
61-
&& strlen($methodName) > strlen('findBy')
62-
) || (
63-
strpos($methodName, 'findOneBy') === 0
64-
&& strlen($methodName) > strlen('findOneBy')
60+
(
61+
strpos($methodName, 'findBy') === 0
62+
&& strlen($methodName) > strlen('findBy')
63+
) || (
64+
strpos($methodName, 'findOneBy') === 0
65+
&& strlen($methodName) > strlen('findOneBy')
66+
)
6567
)
66-
)
67-
&& $repositoryClassReflection->hasNativeMethod($methodName)
68-
) {
69-
return ParametersAcceptorSelector::selectFromArgs(
70-
$scope,
71-
$methodCall->args,
72-
$repositoryClassReflection->getNativeMethod($methodName)->getVariants()
73-
)->getReturnType();
68+
&& $repositoryClassReflection->hasNativeMethod($methodName)
69+
) {
70+
return ParametersAcceptorSelector::selectFromArgs(
71+
$scope,
72+
$methodCall->args,
73+
$repositoryClassReflection->getNativeMethod($methodName)->getVariants()
74+
)->getReturnType();
75+
}
7476
}
7577

7678
if (!$calledOnType instanceof ObjectRepositoryType) {

0 commit comments

Comments
 (0)