type fixes
This commit is contained in:
@@ -39,6 +39,8 @@ use PHPStan\Type\ObjectType;
|
|||||||
use PHPStan\Type\UnionType;
|
use PHPStan\Type\UnionType;
|
||||||
use PHPStan\Type\NullType;
|
use PHPStan\Type\NullType;
|
||||||
use PHPStan\Type\Generic\GenericObjectType;
|
use PHPStan\Type\Generic\GenericObjectType;
|
||||||
|
use PHPStan\Type\Generic\TemplateGenericObjectType;
|
||||||
|
|
||||||
use PhpParser\Node\Scalar\String_;
|
use PhpParser\Node\Scalar\String_;
|
||||||
|
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
@@ -49,12 +51,16 @@ use Espo\ORM\EntityManager;
|
|||||||
use Espo\Core\Utils\Util;
|
use Espo\Core\Utils\Util;
|
||||||
|
|
||||||
use Espo\ORM\Repository\RDBRepository;
|
use Espo\ORM\Repository\RDBRepository;
|
||||||
|
use Espo\ORM\Repository\Repository;
|
||||||
|
|
||||||
class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
|
class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
|
||||||
{
|
{
|
||||||
private $supportedMethodNameList = [
|
private $supportedMethodNameList = [
|
||||||
'getEntity',
|
'getEntity',
|
||||||
|
'getNewEntity',
|
||||||
|
'createEntity',
|
||||||
'getRDBRepository',
|
'getRDBRepository',
|
||||||
|
'getRepository',
|
||||||
];
|
];
|
||||||
|
|
||||||
private $entityNamespaceList = [
|
private $entityNamespaceList = [
|
||||||
@@ -84,10 +90,18 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
|
|||||||
return $this->getGetEntity($methodReflection, $methodCall, $scope);
|
return $this->getGetEntity($methodReflection, $methodCall, $scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($methodName === 'getNewEntity' || $methodName === 'createEntity') {
|
||||||
|
return $this->getGetEntityNotNull($methodReflection, $methodCall, $scope);
|
||||||
|
}
|
||||||
|
|
||||||
if ($methodName === 'getRDBRepository') {
|
if ($methodName === 'getRDBRepository') {
|
||||||
return $this->getGetRDBRepository($methodReflection, $methodCall, $scope);
|
return $this->getGetRDBRepository($methodReflection, $methodCall, $scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($methodName === 'getRepository') {
|
||||||
|
return $this->getGetRepository($methodReflection, $methodCall, $scope);
|
||||||
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Not supported method.");
|
throw new RuntimeException("Not supported method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +130,25 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getGetEntityNotNull(
|
||||||
|
MethodReflection $methodReflection,
|
||||||
|
MethodCall $methodCall,
|
||||||
|
Scope $scope
|
||||||
|
): Type {
|
||||||
|
|
||||||
|
$value = $methodCall->args[0]->value;
|
||||||
|
|
||||||
|
if (!$value instanceof String_) {
|
||||||
|
return new ObjectType(Entity::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$entityType = $value->value;
|
||||||
|
|
||||||
|
$className = $this->findEntityClassName($entityType) ?? Entity::class;
|
||||||
|
|
||||||
|
return new ObjectType($className);
|
||||||
|
}
|
||||||
|
|
||||||
private function findEntityClassName(string $entityType): ?string
|
private function findEntityClassName(string $entityType): ?string
|
||||||
{
|
{
|
||||||
foreach ($this->entityNamespaceList as $namespace) {
|
foreach ($this->entityNamespaceList as $namespace) {
|
||||||
@@ -151,4 +184,27 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
|
|||||||
|
|
||||||
return new ObjectType(RDBRepository::class);
|
return new ObjectType(RDBRepository::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getGetRepository(
|
||||||
|
MethodReflection $methodReflection,
|
||||||
|
MethodCall $methodCall,
|
||||||
|
Scope $scope
|
||||||
|
): Type {
|
||||||
|
|
||||||
|
$value = $methodCall->args[0]->value;
|
||||||
|
|
||||||
|
if (!$value instanceof ObjectType) {
|
||||||
|
return new ObjectType(Repository::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$entityType = $value->value;
|
||||||
|
|
||||||
|
$entityClassName = $this->findEntityClassName($entityType);
|
||||||
|
|
||||||
|
if ($entityClassName) {
|
||||||
|
return new TemplateGenericObjectType(Repository::class, [new ObjectType($entityClassName)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ObjectType(Repository::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user