Skip to content

Commit

Permalink
QueryBuilder with extra methods called on it is not reported correctly
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
mhujer committed Oct 10, 2019
1 parent 3fd0488 commit dfb46d7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function dataTopics(): array
['entityManagerMergeReturn'],
['customRepositoryUsage'],
['queryBuilder'],
['queryBuilderExtraMethod'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
49 changes: 49 additions & 0 deletions tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\DoctrineIntegration\ORM\QueryBuilder;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPStan\DoctrineIntegration\ORM\CustomRepositoryUsage\MyEntity;

class Foo
{

/**
* @var EntityManager
*/
private $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function doFoo(): Query
{
$queryBuilder = $this->entityManager->createQueryBuilder()
->select('e')
->from(MyEntity::class, 'e');

// @todo it works when the extra where is here
//$queryBuilder->andWhere('e.id != :excludedId');

// but it does not work when the where is added in extra method
$this->addExtraCondition($queryBuilder);

$query = $queryBuilder->getQuery();

$expectedDql = 'SELECT e FROM PHPStan\DoctrineIntegration\ORM\CustomRepositoryUsage\MyEntity e WHERE e.id != :excludedId';
$query->getDQL() === $expectedDql;
$queryBuilder->getDQL() === $expectedDql;

return $query;
}

private function addExtraCondition(QueryBuilder $queryBuilder): void
{
$queryBuilder->andWhere('e.id != :excludedId');
}

}

0 comments on commit dfb46d7

Please sign in to comment.