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); 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); + } + +}