From e0ec6d43affbc878e0659f66142848586870cd83 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 25 Jan 2022 23:44:08 +0100 Subject: [PATCH] doctrine-dbal: implement `Connection->iterateColumn()` --- src/DoctrineReflection/DoctrineReflection.php | 5 +++++ .../DoctrineConnectionFetchDynamicReturnTypeExtension.php | 2 +- tests/data/doctrine-dbal.php | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/DoctrineReflection/DoctrineReflection.php b/src/DoctrineReflection/DoctrineReflection.php index de2a29e99..497d55c16 100644 --- a/src/DoctrineReflection/DoctrineReflection.php +++ b/src/DoctrineReflection/DoctrineReflection.php @@ -30,6 +30,7 @@ public function fetchResultType(MethodReflection $methodReflection, Type $result $fetchType = QueryReflector::FETCH_TYPE_ONE; break; case 'fetchfirstcolumn': + case 'iteratecolumn': $fetchType = QueryReflector::FETCH_TYPE_FIRST_COL; break; case 'fetchnumeric': @@ -57,6 +58,10 @@ public function fetchResultType(MethodReflection $methodReflection, Type $result return $valueTypes[$i]; } if (QueryReflector::FETCH_TYPE_FIRST_COL === $fetchType) { + if (\in_array($usedMethod, ['iteratecolumn'], true)) { + return new GenericObjectType(Traversable::class, [new IntegerType(), $valueTypes[$i]]); + } + return new ArrayType(IntegerRangeType::fromInterval(0, null), $valueTypes[$i]); } diff --git a/src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php b/src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php index 818f6048d..26bacc38d 100644 --- a/src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php +++ b/src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php @@ -28,7 +28,7 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection): bool { - return \in_array(strtolower($methodReflection->getName()), ['fetchone', 'fetchfirstcolumn', 'fetchassociative', 'fetchallassociative', 'fetchnumeric', 'fetchallnumeric', 'iterateassociative', 'iteratenumeric', 'iterateallnumeric'], true); + return \in_array(strtolower($methodReflection->getName()), ['fetchone', 'fetchfirstcolumn', 'fetchassociative', 'fetchallassociative', 'fetchnumeric', 'fetchallnumeric', 'iteratecolumn', 'iterateassociative', 'iteratenumeric', 'iterateallnumeric'], true); } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type diff --git a/tests/data/doctrine-dbal.php b/tests/data/doctrine-dbal.php index 517c8d44d..43a28beee 100644 --- a/tests/data/doctrine-dbal.php +++ b/tests/data/doctrine-dbal.php @@ -83,6 +83,13 @@ public function iterateNumeric(Connection $conn) assertType('Traversable, int<-128, 127>, int<-128, 127>}>', $fetchResult); } + public function iterateColumn(Connection $conn) + { + $query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid = ?'; + $fetchResult = $conn->iterateColumn($query, [1]); + assertType('Traversable', $fetchResult); + } + public function fetchOne(Connection $conn) { $query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid = ?';