From 08b193c4aee0325fb000d3c8fd48a541bc158a61 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Mon, 15 Jan 2024 09:20:45 +0100 Subject: [PATCH] No failure when MappingException occurs --- .../CreateQueryDynamicReturnTypeExtension.php | 3 ++- ...yBuilderGetQueryDynamicReturnTypeExtension.php | 3 ++- .../Doctrine/data/QueryResult/createQuery.php | 7 +++++++ .../data/QueryResult/queryBuilderGetQuery.php | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) 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 3f4bd916..d4357def 100644 --- a/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php +++ b/tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php @@ -262,4 +262,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); + } + }