Skip to content

Improve return type of getArrayResult #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Doctrine\DescriptorNotRegisteredException;
use PHPStan\Type\Doctrine\DescriptorRegistry;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -48,11 +52,16 @@ final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturn
/** @var ObjectMetadataResolver */
private $objectMetadataResolver;

/** @var DescriptorRegistry */
private $descriptorRegistry;

public function __construct(
ObjectMetadataResolver $objectMetadataResolver
ObjectMetadataResolver $objectMetadataResolver,
DescriptorRegistry $descriptorRegistry
)
{
$this->objectMetadataResolver = $objectMetadataResolver;
$this->descriptorRegistry = $descriptorRegistry;
}

public function getClass(): string
Expand Down Expand Up @@ -183,10 +192,11 @@ private function getMethodReturnTypeForHydrationMode(
private function getArrayHydratedReturnType(Type $queryResultType): Type
{
$objectManager = $this->objectMetadataResolver->getObjectManager();
$descriptorRegistry = $this->descriptorRegistry;

return TypeTraverser::map(
$queryResultType,
static function (Type $type, callable $traverse) use ($objectManager): Type {
static function (Type $type, callable $traverse) use ($objectManager, $descriptorRegistry): Type {
$isObject = (new ObjectWithoutClassType())->isSuperTypeOf($type);
if ($isObject->no()) {
return $traverse($type);
Expand All @@ -199,9 +209,33 @@ static function (Type $type, callable $traverse) use ($objectManager): Type {
return new MixedType();
}

return $objectManager->getMetadataFactory()->hasMetadataFor($type->getClassName())
? new ArrayType(new MixedType(), new MixedType())
: $traverse($type);
if (!$objectManager->getMetadataFactory()->hasMetadataFor($type->getClassName())) {
return $traverse($type);
}

$metadata = $objectManager->getMetadataFactory()->getMetadataFor($type->getClassName());

$types = [];
$keys = [];
foreach ($metadata->fieldMappings as $fieldMapping) {
try {
$type = $descriptorRegistry->get($fieldMapping['type'])->getWritableToPropertyType();
} catch (DescriptorNotRegisteredException $exception) {
return new ArrayType(new MixedType(), new MixedType());
}

$nullable = isset($fieldMapping['nullable'])
? $fieldMapping['nullable'] === true
: false;
if ($nullable) {
$type = TypeCombinator::addNull($type);
}

$types[] = $type;
$keys[] = new ConstantStringType($fieldMapping['fieldName']);
}

return new ConstantArrayType($keys, $types);
}
);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Type/Doctrine/data/QueryResult/queryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,35 @@ public function testReturnTypeOfQueryMethodsWithExplicitArrayHydrationMode(Entit
');

assertType(
'list<array>',
'list<array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->getResult(AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'list<array>',
'list<array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->getArrayResult()
);
assertType(
'iterable<int, array>',
'iterable<int, array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->toIterable([], AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'list<array>',
'list<array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->execute(null, AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'list<array>',
'list<array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->executeIgnoreQueryCache(null, AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'list<array>',
'list<array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}>',
$query->executeUsingQueryCache(null, AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'array',
'array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}',
$query->getSingleResult(AbstractQuery::HYDRATE_ARRAY)
);
assertType(
'array|null',
'array{id: numeric-string, intColumn: int, stringColumn: string, stringNullColumn: string|null, datetimeColumn: DateTime, datetimeImmutableColumn: DateTimeImmutable}|null',
$query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY)
);

Expand Down