Skip to content

Test hydration modes #590

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

Merged
merged 4 commits into from
Jun 28, 2024
Merged
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
2 changes: 2 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ services:

-
class: PHPStan\Doctrine\Driver\DriverDetector
-
class: PHPStan\Type\Doctrine\HydrationModeReturnTypeResolver
-
class: PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension
-
Expand Down
143 changes: 143 additions & 0 deletions src/Type/Doctrine/HydrationModeReturnTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use Doctrine\ORM\AbstractQuery;
use Doctrine\Persistence\ObjectManager;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VoidType;

class HydrationModeReturnTypeResolver
{

public function getMethodReturnTypeForHydrationMode(
string $methodName,
int $hydrationMode,
Type $queryKeyType,
Type $queryResultType,
?ObjectManager $objectManager
): ?Type
{
$isVoidType = (new VoidType())->isSuperTypeOf($queryResultType);

if ($isVoidType->yes()) {
// A void query result type indicates an UPDATE or DELETE query.
// In this case all methods return the number of affected rows.
return new IntegerType();
}

if ($isVoidType->maybe()) {
// We can't be sure what the query type is, so we return the
// declared return type of the method.
return null;
}

switch ($hydrationMode) {
case AbstractQuery::HYDRATE_OBJECT:
break;
case AbstractQuery::HYDRATE_ARRAY:
$queryResultType = $this->getArrayHydratedReturnType($queryResultType, $objectManager);
break;
case AbstractQuery::HYDRATE_SIMPLEOBJECT:
$queryResultType = $this->getSimpleObjectHydratedReturnType($queryResultType);
break;
default:
return null;
}

if ($queryResultType === null) {
return null;
}

switch ($methodName) {
case 'getSingleResult':
return $queryResultType;
case 'getOneOrNullResult':
$nullableQueryResultType = TypeCombinator::addNull($queryResultType);
if ($queryResultType instanceof BenevolentUnionType) {
$nullableQueryResultType = TypeUtils::toBenevolentUnion($nullableQueryResultType);
}

return $nullableQueryResultType;
case 'toIterable':
return new IterableType(
$queryKeyType->isNull()->yes() ? new IntegerType() : $queryKeyType,
$queryResultType
);
default:
if ($queryKeyType->isNull()->yes()) {
return AccessoryArrayListType::intersectWith(new ArrayType(
new IntegerType(),
$queryResultType
));
}
return new ArrayType(
$queryKeyType,
$queryResultType
);
}
}

/**
* When we're array-hydrating object, we're not sure of the shape of the array.
* We could return `new ArrayTyp(new MixedType(), new MixedType())`
* but the lack of precision in the array keys/values would give false positive.
*
* @see https://github.com/phpstan/phpstan-doctrine/pull/412#issuecomment-1497092934
*/
private function getArrayHydratedReturnType(Type $queryResultType, ?ObjectManager $objectManager): ?Type
{
$mixedFound = false;
$queryResultType = TypeTraverser::map(
$queryResultType,
static function (Type $type, callable $traverse) use ($objectManager, &$mixedFound): Type {
$isObject = (new ObjectWithoutClassType())->isSuperTypeOf($type);
if ($isObject->no()) {
return $traverse($type);
}
if (
$isObject->maybe()
|| !$type instanceof TypeWithClassName
|| $objectManager === null
) {
$mixedFound = true;

return new MixedType();
}

/** @var class-string $className */
$className = $type->getClassName();
if (!$objectManager->getMetadataFactory()->hasMetadataFor($className)) {
return $traverse($type);
}

$mixedFound = true;

return new MixedType();
}
);

return $mixedFound ? null : $queryResultType;
}

private function getSimpleObjectHydratedReturnType(Type $queryResultType): ?Type
{
if ((new ObjectWithoutClassType())->isSuperTypeOf($queryResultType)->yes()) {
return $queryResultType;
}

return null;
}

}
151 changes: 13 additions & 138 deletions src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,12 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Doctrine\HydrationModeReturnTypeResolver;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VoidType;

final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
Expand All @@ -46,11 +35,16 @@ final class QueryResultDynamicReturnTypeExtension implements DynamicMethodReturn
/** @var ObjectMetadataResolver */
private $objectMetadataResolver;

/** @var HydrationModeReturnTypeResolver */
private $hydrationModeReturnTypeResolver;

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

public function getClass(): string
Expand Down Expand Up @@ -93,136 +87,17 @@ public function getTypeFromMethodCall(

$queryType = $scope->getType($methodCall->var);

return $this->getMethodReturnTypeForHydrationMode(
$methodReflection,
$hydrationMode,
$queryType->getTemplateType(AbstractQuery::class, 'TKey'),
$queryType->getTemplateType(AbstractQuery::class, 'TResult')
);
}

private function getMethodReturnTypeForHydrationMode(
MethodReflection $methodReflection,
Type $hydrationMode,
Type $queryKeyType,
Type $queryResultType
): ?Type
{
$isVoidType = (new VoidType())->isSuperTypeOf($queryResultType);

if ($isVoidType->yes()) {
// A void query result type indicates an UPDATE or DELETE query.
// In this case all methods return the number of affected rows.
return new IntegerType();
}

if ($isVoidType->maybe()) {
// We can't be sure what the query type is, so we return the
// declared return type of the method.
return null;
}

if (!$hydrationMode instanceof ConstantIntegerType) {
return null;
}

switch ($hydrationMode->getValue()) {
case AbstractQuery::HYDRATE_OBJECT:
break;
case AbstractQuery::HYDRATE_ARRAY:
$queryResultType = $this->getArrayHydratedReturnType($queryResultType);
break;
case AbstractQuery::HYDRATE_SIMPLEOBJECT:
$queryResultType = $this->getSimpleObjectHydratedReturnType($queryResultType);
break;
default:
return null;
}

if ($queryResultType === null) {
return null;
}

switch ($methodReflection->getName()) {
case 'getSingleResult':
return $queryResultType;
case 'getOneOrNullResult':
$nullableQueryResultType = TypeCombinator::addNull($queryResultType);
if ($queryResultType instanceof BenevolentUnionType) {
$nullableQueryResultType = TypeUtils::toBenevolentUnion($nullableQueryResultType);
}

return $nullableQueryResultType;
case 'toIterable':
return new IterableType(
$queryKeyType->isNull()->yes() ? new IntegerType() : $queryKeyType,
$queryResultType
);
default:
if ($queryKeyType->isNull()->yes()) {
return AccessoryArrayListType::intersectWith(new ArrayType(
new IntegerType(),
$queryResultType
));
}
return new ArrayType(
$queryKeyType,
$queryResultType
);
}
}

/**
* When we're array-hydrating object, we're not sure of the shape of the array.
* We could return `new ArrayTyp(new MixedType(), new MixedType())`
* but the lack of precision in the array keys/values would give false positive.
*
* @see https://github.com/phpstan/phpstan-doctrine/pull/412#issuecomment-1497092934
*/
private function getArrayHydratedReturnType(Type $queryResultType): ?Type
{
$objectManager = $this->objectMetadataResolver->getObjectManager();

$mixedFound = false;
$queryResultType = TypeTraverser::map(
$queryResultType,
static function (Type $type, callable $traverse) use ($objectManager, &$mixedFound): Type {
$isObject = (new ObjectWithoutClassType())->isSuperTypeOf($type);
if ($isObject->no()) {
return $traverse($type);
}
if (
$isObject->maybe()
|| !$type instanceof TypeWithClassName
|| $objectManager === null
) {
$mixedFound = true;

return new MixedType();
}

/** @var class-string $className */
$className = $type->getClassName();
if (!$objectManager->getMetadataFactory()->hasMetadataFor($className)) {
return $traverse($type);
}

$mixedFound = true;

return new MixedType();
}
return $this->hydrationModeReturnTypeResolver->getMethodReturnTypeForHydrationMode(
$methodReflection->getName(),
$hydrationMode->getValue(),
$queryType->getTemplateType(AbstractQuery::class, 'TKey'),
$queryType->getTemplateType(AbstractQuery::class, 'TResult'),
$this->objectMetadataResolver->getObjectManager()
);

return $mixedFound ? null : $queryResultType;
}

private function getSimpleObjectHydratedReturnType(Type $queryResultType): ?Type
{
if ((new ObjectWithoutClassType())->isSuperTypeOf($queryResultType)->yes()) {
return $queryResultType;
}

return null;
}

}
Loading