Skip to content

Commit

Permalink
No failure when MappingException occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored and ondrejmirtes committed Jan 15, 2024
1 parent 7604bc1 commit 08b193c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/Type/Doctrine/data/QueryResult/createQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function testQueryTypeSimpleArray(EntityManagerInterface $em): void
assertType('Doctrine\ORM\Query<null, array{simpleArrayColumn: list<string>}>', $query);
}

public function testMappingError(EntityManagerInterface $em): void
{
$query = $em->createQuery('SELECT u.foo FROM ' . CreateQuery::class . ' u');

assertType('Doctrine\ORM\Query<mixed, mixed>', $query);
}

public function testQueryResultTypeIsMixedWhenDQLIsNotKnown(EntityManagerInterface $em, string $dql): void
{
$query = $em->createQuery($dql);
Expand Down
15 changes: 15 additions & 0 deletions tests/Type/Doctrine/data/QueryResult/queryBuilderGetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,19 @@ public function testTemplatedClassString(EntityManagerInterface $em, string $man
assertType('list<QueryResult\Entities\Many>', $result);
}


/**
* @param class-string<self> $classString
*/
public function testNonEntityClassString(EntityManagerInterface $em, string $classString)
{
$result = $em->createQueryBuilder()
->select("m")
->from($classString, 'm')
->getQuery()
->getResult();

assertType('mixed', $result);
}

}

0 comments on commit 08b193c

Please sign in to comment.