From 8aab012af152282ffcfc7a4b0298e919a0c2466f Mon Sep 17 00:00:00 2001 From: Martin Hujer Date: Thu, 10 Oct 2019 12:29:11 +0200 Subject: [PATCH] QueryBuilder with extra methods called on it is not reported correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. ``` --- .../ORM/EntityManagerIntegrationTest.php | 1 + .../ORM/data/queryBuilderExtraMethod-4.json | 2 + .../ORM/data/queryBuilderExtraMethod.php | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod-4.json create mode 100644 tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod.php diff --git a/tests/DoctrineIntegration/ORM/EntityManagerIntegrationTest.php b/tests/DoctrineIntegration/ORM/EntityManagerIntegrationTest.php index 7ed4a32e..da9d731e 100644 --- a/tests/DoctrineIntegration/ORM/EntityManagerIntegrationTest.php +++ b/tests/DoctrineIntegration/ORM/EntityManagerIntegrationTest.php @@ -18,6 +18,7 @@ public function dataTopics(): array ['entityManagerMergeReturn'], ['customRepositoryUsage'], ['queryBuilder'], + ['queryBuilderExtraMethod'], ]; } diff --git a/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod-4.json b/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod-4.json new file mode 100644 index 00000000..0d4f101c --- /dev/null +++ b/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod-4.json @@ -0,0 +1,2 @@ +[ +] diff --git a/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod.php b/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod.php new file mode 100644 index 00000000..94548336 --- /dev/null +++ b/tests/DoctrineIntegration/ORM/data/queryBuilderExtraMethod.php @@ -0,0 +1,49 @@ +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'); + } + +}