|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Doctrine; |
| 4 | + |
| 5 | +use Doctrine\Common\CommonException; |
| 6 | +use Doctrine\DBAL\DBALException; |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Doctrine\ORM\ORMException; |
| 9 | +use Doctrine\ORM\Query; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Type\Constant\ConstantStringType; |
| 14 | +use PHPStan\Type\Doctrine\Query\QueryResultTypeBuilder; |
| 15 | +use PHPStan\Type\Doctrine\Query\QueryResultTypeWalker; |
| 16 | +use PHPStan\Type\Doctrine\Query\QueryType; |
| 17 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 18 | +use PHPStan\Type\Generic\GenericObjectType; |
| 19 | +use PHPStan\Type\IntersectionType; |
| 20 | +use PHPStan\Type\MixedType; |
| 21 | +use PHPStan\Type\Type; |
| 22 | +use PHPStan\Type\TypeTraverser; |
| 23 | +use PHPStan\Type\UnionType; |
| 24 | + |
| 25 | +/** |
| 26 | + * Infers TResult in Query<TResult> on EntityManagerInterface::createQuery() |
| 27 | + */ |
| 28 | +final class CreateQueryDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 29 | +{ |
| 30 | + |
| 31 | + /** @var ObjectMetadataResolver */ |
| 32 | + private $objectMetadataResolver; |
| 33 | + |
| 34 | + /** @var DescriptorRegistry */ |
| 35 | + private $descriptorRegistry; |
| 36 | + |
| 37 | + public function __construct(ObjectMetadataResolver $objectMetadataResolver, DescriptorRegistry $descriptorRegistry) |
| 38 | + { |
| 39 | + $this->objectMetadataResolver = $objectMetadataResolver; |
| 40 | + $this->descriptorRegistry = $descriptorRegistry; |
| 41 | + } |
| 42 | + |
| 43 | + public function getClass(): string |
| 44 | + { |
| 45 | + return EntityManagerInterface::class; |
| 46 | + } |
| 47 | + |
| 48 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 49 | + { |
| 50 | + return $methodReflection->getName() === 'createQuery'; |
| 51 | + } |
| 52 | + |
| 53 | + public function getTypeFromMethodCall( |
| 54 | + MethodReflection $methodReflection, |
| 55 | + MethodCall $methodCall, |
| 56 | + Scope $scope |
| 57 | + ): Type |
| 58 | + { |
| 59 | + $queryStringArgIndex = 0; |
| 60 | + $args = $methodCall->getArgs(); |
| 61 | + |
| 62 | + if (!isset($args[$queryStringArgIndex])) { |
| 63 | + return new GenericObjectType( |
| 64 | + Query::class, |
| 65 | + [new MixedType()] |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $argType = $scope->getType($args[$queryStringArgIndex]->value); |
| 70 | + |
| 71 | + return TypeTraverser::map($argType, function (Type $type, callable $traverse): Type { |
| 72 | + if ($type instanceof UnionType || $type instanceof IntersectionType) { |
| 73 | + return $traverse($type); |
| 74 | + } |
| 75 | + if ($type instanceof ConstantStringType) { |
| 76 | + $queryString = $type->getValue(); |
| 77 | + |
| 78 | + $em = $this->objectMetadataResolver->getObjectManager(); |
| 79 | + if (!$em instanceof EntityManagerInterface) { |
| 80 | + return new QueryType($queryString, null); |
| 81 | + } |
| 82 | + |
| 83 | + $typeBuilder = new QueryResultTypeBuilder(); |
| 84 | + |
| 85 | + try { |
| 86 | + $query = $em->createQuery($queryString); |
| 87 | + QueryResultTypeWalker::walk($query, $typeBuilder, $this->descriptorRegistry); |
| 88 | + } catch (ORMException | DBALException | CommonException $e) { |
| 89 | + return new QueryType($queryString, null); |
| 90 | + } |
| 91 | + |
| 92 | + return new QueryType($queryString, $typeBuilder->getResultType()); |
| 93 | + } |
| 94 | + return new GenericObjectType( |
| 95 | + Query::class, |
| 96 | + [new MixedType()] |
| 97 | + ); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments