return type const

This commit is contained in:
Yuri Kuznetsov
2021-11-08 16:31:54 +02:00
parent 4c696c8b21
commit 380b3dde0a
@@ -31,7 +31,10 @@ namespace EspoDev\PHPStan\Extensions;
use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\ClassConstFetch;
use PHPStan\Analyser\Scope; use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Type; use PHPStan\Type\Type;
@@ -39,7 +42,6 @@ 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_;
@@ -111,17 +113,15 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
Scope $scope Scope $scope
): Type { ): Type {
$value = $methodCall->args[0]->value; $entityType = $this->getEntityTypeFromExpr($methodCall->args[0]->value);
if (!$value instanceof String_) { if (!$entityType) {
return new UnionType([ return new UnionType([
new ObjectType(Entity::class), new ObjectType(Entity::class),
new NullType(), new NullType(),
]); ]);
} }
$entityType = $value->value;
$className = $this->findEntityClassName($entityType) ?? Entity::class; $className = $this->findEntityClassName($entityType) ?? Entity::class;
return new UnionType([ return new UnionType([
@@ -136,14 +136,12 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
Scope $scope Scope $scope
): Type { ): Type {
$value = $methodCall->args[0]->value; $entityType = $this->getEntityTypeFromExpr($methodCall->args[0]->value);
if (!$value instanceof String_) { if (!$entityType) {
return new ObjectType(Entity::class); return new ObjectType(Entity::class);
} }
$entityType = $value->value;
$className = $this->findEntityClassName($entityType) ?? Entity::class; $className = $this->findEntityClassName($entityType) ?? Entity::class;
return new ObjectType($className); return new ObjectType($className);
@@ -168,14 +166,12 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
Scope $scope Scope $scope
): Type { ): Type {
$value = $methodCall->args[0]->value; $entityType = $this->getEntityTypeFromExpr($methodCall->args[0]->value);
if (!$value instanceof String_) { if (!$entityType) {
return new ObjectType(RDBRepository::class); return new ObjectType(RDBRepository::class);
} }
$entityType = $value->value;
$entityClassName = $this->findEntityClassName($entityType); $entityClassName = $this->findEntityClassName($entityType);
if ($entityClassName) { if ($entityClassName) {
@@ -191,20 +187,31 @@ class EntityManagerReturnType implements DynamicMethodReturnTypeExtension
Scope $scope Scope $scope
): Type { ): Type {
$value = $methodCall->args[0]->value; $entityType = $this->getEntityTypeFromExpr($methodCall->args[0]->value);
if (!$value instanceof ObjectType) { if (!$entityType) {
return new ObjectType(Repository::class); return new ObjectType(Repository::class);
} }
$entityType = $value->value;
$entityClassName = $this->findEntityClassName($entityType); $entityClassName = $this->findEntityClassName($entityType);
if ($entityClassName) { if ($entityClassName) {
return new TemplateGenericObjectType(Repository::class, [new ObjectType($entityClassName)]); return new GenericObjectType(Repository::class, [new ObjectType($entityClassName)]);
} }
return new ObjectType(Repository::class); return new ObjectType(Repository::class);
} }
private function getEntityTypeFromExpr(Expr $expr): ?string
{
if ($expr instanceof String_) {
return $expr->value;
}
if ($expr instanceof ClassConstFetch) {
return constant($expr->class . '::' . $expr->name);
}
return null;
}
} }