diff --git a/src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php b/src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php index 9f703951..023828df 100644 --- a/src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php +++ b/src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php @@ -9,6 +9,7 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; use Doctrine\ORM\Query; +use Doctrine\Persistence\Mapping\MappingException; use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; @@ -87,7 +88,7 @@ public function getTypeFromMethodCall( try { $query = $em->createQuery($queryString); QueryResultTypeWalker::walk($query, $typeBuilder, $this->descriptorRegistry); - } catch (ORMException | DBALException | NewDBALException | CommonException $e) { + } catch (ORMException | DBALException | NewDBALException | CommonException | MappingException $e) { return new QueryType($queryString, null, null); } catch (AssertionError $e) { return new QueryType($queryString, null, null); diff --git a/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php b/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php index b956b690..1d0a1809 100644 --- a/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php +++ b/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\DBALException; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; +use Doctrine\Persistence\Mapping\MappingException; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; @@ -195,7 +196,7 @@ private function getQueryType(string $dql): Type try { $query = $em->createQuery($dql); QueryResultTypeWalker::walk($query, $typeBuilder, $this->descriptorRegistry); - } catch (ORMException | DBALException | CommonException $e) { + } catch (ORMException | DBALException | CommonException | MappingException $e) { return new QueryType($dql, null); } catch (AssertionError $e) { return new QueryType($dql, null); diff --git a/tests/Type/Doctrine/data/QueryResult/createQuery.php b/tests/Type/Doctrine/data/QueryResult/createQuery.php index 1af492ce..e15515b7 100644 --- a/tests/Type/Doctrine/data/QueryResult/createQuery.php +++ b/tests/Type/Doctrine/data/QueryResult/createQuery.php @@ -32,6 +32,13 @@ public function testQueryTypeSimpleArray(EntityManagerInterface $em): void assertType('Doctrine\ORM\Query}>', $query); } + public function testMappingError(EntityManagerInterface $em): void + { + $query = $em->createQuery('SELECT u.foo FROM ' . CreateQuery::class . ' u'); + + assertType('Doctrine\ORM\Query', $query); + } + public function testQueryResultTypeIsMixedWhenDQLIsNotKnown(EntityManagerInterface $em, string $dql): void { $query = $em->createQuery($dql); diff --git a/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php b/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php index a010e9b6..d88ba764 100644 --- a/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php +++ b/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php @@ -282,4 +282,19 @@ public function testTemplatedClassString(EntityManagerInterface $em, string $man assertType('list', $result); } + + /** + * @param class-string $classString + */ + public function testNonEntityClassString(EntityManagerInterface $em, string $classString) + { + $result = $em->createQueryBuilder() + ->select("m") + ->from($classString, 'm') + ->getQuery() + ->getResult(); + + assertType('mixed', $result); + } + }