-
Notifications
You must be signed in to change notification settings - Fork 105
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
Test hydration modes #590
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.