From 31bf29aa157ec2830e4d27cd0e08c21efa2af88c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 7 Oct 2021 14:28:26 +0200 Subject: [PATCH 1/2] Do not specify a variable which is not a query builder --- .../QueryBuilder/QueryBuilderTypeSpecifyingExtension.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Type/Doctrine/QueryBuilder/QueryBuilderTypeSpecifyingExtension.php b/src/Type/Doctrine/QueryBuilder/QueryBuilderTypeSpecifyingExtension.php index 1cb5994c..acac6d5c 100644 --- a/src/Type/Doctrine/QueryBuilder/QueryBuilderTypeSpecifyingExtension.php +++ b/src/Type/Doctrine/QueryBuilder/QueryBuilderTypeSpecifyingExtension.php @@ -85,6 +85,11 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod $queryBuilderNode = $queryBuilderNode->var; } + // If the variable is not a query builder, there is nothing to specify + if (!(new ObjectType(QueryBuilder::class))->isSuperTypeOf($scope->getType($queryBuilderNode))->yes()) { + return new SpecifiedTypes([]); + } + $resultTypes = []; foreach ($queryBuilderTypes as $queryBuilderType) { $resultTypes[] = $queryBuilderType->append($node); From 64a4d615e70562c540256e245413a8f8dadf95b5 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 7 Oct 2021 21:14:09 +0200 Subject: [PATCH 2/2] Add test --- ...ueryBuilderTypeSpecifyingExtensionTest.php | 38 +++++++++++++++++++ tests/Type/Doctrine/data/bug-219.php | 33 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/Type/Doctrine/QueryBuilderTypeSpecifyingExtensionTest.php create mode 100644 tests/Type/Doctrine/data/bug-219.php diff --git a/tests/Type/Doctrine/QueryBuilderTypeSpecifyingExtensionTest.php b/tests/Type/Doctrine/QueryBuilderTypeSpecifyingExtensionTest.php new file mode 100644 index 00000000..a88d422f --- /dev/null +++ b/tests/Type/Doctrine/QueryBuilderTypeSpecifyingExtensionTest.php @@ -0,0 +1,38 @@ + + */ + public function dataFileAsserts(): iterable + { + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-219.php'); + } + + /** + * @dataProvider dataFileAsserts + * @param string $assertType + * @param string $file + * @param mixed ...$args + */ + public function testFileAsserts( + string $assertType, + string $file, + ...$args + ): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/../../../extension.neon']; + } + +} diff --git a/tests/Type/Doctrine/data/bug-219.php b/tests/Type/Doctrine/data/bug-219.php new file mode 100644 index 00000000..e6f594fa --- /dev/null +++ b/tests/Type/Doctrine/data/bug-219.php @@ -0,0 +1,33 @@ +entityManager = $entityManager; + } + + public function getEntityManager(): EntityManagerInterface + { + return $this->entityManager; + } + + public function update(): void + { + $this->getEntityManager()->getRepository(\stdClass::class)->createQueryBuilder('t')->update(); + + assertType('$this(Bug219\Test)', $this); + } + +}