Skip to content

Commit dfb46d7

Browse files
committed
QueryBuilder with extra methods called on it is not reported correctly
I understand that it is hard to evaluate code like this when the things happen in separate methods. The solution may be not to analyze the query builder in this case. And the message is not very helpful because the displayed portion is similar ``` Strict comparison using === between 'SELECT e FROM…' and 'SELECT e FROM…' will always evaluate to false. ```
1 parent 3fd0488 commit dfb46d7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

tests/DoctrineIntegration/ORM/EntityManagerIntegrationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function dataTopics(): array
1818
['entityManagerMergeReturn'],
1919
['customRepositoryUsage'],
2020
['queryBuilder'],
21+
['queryBuilderExtraMethod'],
2122
];
2223
}
2324

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[
2+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DoctrineIntegration\ORM\QueryBuilder;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use Doctrine\ORM\Query;
7+
use Doctrine\ORM\QueryBuilder;
8+
use PHPStan\DoctrineIntegration\ORM\CustomRepositoryUsage\MyEntity;
9+
10+
class Foo
11+
{
12+
13+
/**
14+
* @var EntityManager
15+
*/
16+
private $entityManager;
17+
18+
public function __construct(EntityManager $entityManager)
19+
{
20+
$this->entityManager = $entityManager;
21+
}
22+
23+
public function doFoo(): Query
24+
{
25+
$queryBuilder = $this->entityManager->createQueryBuilder()
26+
->select('e')
27+
->from(MyEntity::class, 'e');
28+
29+
// @todo it works when the extra where is here
30+
//$queryBuilder->andWhere('e.id != :excludedId');
31+
32+
// but it does not work when the where is added in extra method
33+
$this->addExtraCondition($queryBuilder);
34+
35+
$query = $queryBuilder->getQuery();
36+
37+
$expectedDql = 'SELECT e FROM PHPStan\DoctrineIntegration\ORM\CustomRepositoryUsage\MyEntity e WHERE e.id != :excludedId';
38+
$query->getDQL() === $expectedDql;
39+
$queryBuilder->getDQL() === $expectedDql;
40+
41+
return $query;
42+
}
43+
44+
private function addExtraCondition(QueryBuilder $queryBuilder): void
45+
{
46+
$queryBuilder->andWhere('e.id != :excludedId');
47+
}
48+
49+
}

0 commit comments

Comments
 (0)