Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/DoctrineReflection/DoctrineReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function fetchResultType(MethodReflection $methodReflection, Type $result
$usedMethod = strtolower($methodReflection->getName());

switch ($usedMethod) {
case 'fetchallkeyvalue':
case 'iteratekeyvalue':
$fetchType = QueryReflector::FETCH_TYPE_KEY_VALUE;
break;
case 'fetchone':
$fetchType = QueryReflector::FETCH_TYPE_ONE;
break;
Expand All @@ -47,12 +51,25 @@ public function fetchResultType(MethodReflection $methodReflection, Type $result
$fetchType = QueryReflector::FETCH_TYPE_BOTH;
}

if ((\in_array($fetchType, [QueryReflector::FETCH_TYPE_ONE, QueryReflector::FETCH_TYPE_FIRST_COL, QueryReflector::FETCH_TYPE_NUMERIC, QueryReflector::FETCH_TYPE_ASSOC])) && $resultRowType instanceof ConstantArrayType) {
if (QueryReflector::FETCH_TYPE_BOTH !== $fetchType && $resultRowType instanceof ConstantArrayType) {
$builder = ConstantArrayTypeBuilder::createEmpty();

$keyTypes = $resultRowType->getKeyTypes();
$valueTypes = $resultRowType->getValueTypes();

if (QueryReflector::FETCH_TYPE_KEY_VALUE === $fetchType) {
// $valueType contain 'BOTH' fetched values
if (\count($valueTypes) < 4) {
return null;
}

if (\in_array($usedMethod, ['fetchallkeyvalue'], true)) {
return new ArrayType($valueTypes[0], $valueTypes[2]);
}

return new GenericObjectType(Traversable::class, [$valueTypes[0], $valueTypes[2]]);
}

foreach ($keyTypes as $i => $keyType) {
if (QueryReflector::FETCH_TYPE_ONE === $fetchType) {
return $valueTypes[$i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@

final class DoctrineConnectionFetchDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
private const METHODS = [
'fetchone',
'fetchfirstcolumn',
'fetchassociative',
'fetchallassociative',
'fetchnumeric',
'fetchallnumeric',
'iteratecolumn',
'iterateassociative',
'iteratenumeric',
];

public function getClass(): string
{
return Connection::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return \in_array(strtolower($methodReflection->getName()), ['fetchone', 'fetchfirstcolumn', 'fetchassociative', 'fetchallassociative', 'fetchnumeric', 'fetchallnumeric', 'iteratecolumn', 'iterateassociative', 'iteratenumeric'], true);
return \in_array(strtolower($methodReflection->getName()), self::METHODS, true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
Expand Down
2 changes: 2 additions & 0 deletions src/Extensions/DoctrineResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ final class DoctrineResultDynamicReturnTypeExtension implements DynamicMethodRet
'fetchallnumeric',
'fetchassociative',
'fetchallassociative',
'fetchallkeyvalue',
'iteratenumeric',
'iterateassociative',
'iteratecolumn',
'iteratekeyvalue',
];

public function getClass(): string
Expand Down
1 change: 1 addition & 0 deletions src/QueryReflection/QueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface QueryReflector
public const FETCH_TYPE_ASSOC = 3;
public const FETCH_TYPE_NUMERIC = 4;
public const FETCH_TYPE_BOTH = 5;
public const FETCH_TYPE_KEY_VALUE = 6;

public function validateQueryString(string $queryString): ?Error;

Expand Down
6 changes: 6 additions & 0 deletions tests/data/doctrine-dbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function foo(Connection $conn)
$fetch = $result->fetchAllAssociative();
assertType('array<int<0, max>, array{email: string, adaid: int<0, 4294967295>, gesperrt: int<-128, 127>, freigabe1u1: int<-128, 127>}>', $fetch);

$fetch = $result->fetchAllKeyValue();
assertType('array<string, int<0, 4294967295>>', $fetch);

$fetch = $result->iterateNumeric();
assertType('Traversable<int, array{string, int<0, 4294967295>, int<-128, 127>, int<-128, 127>}>', $fetch);

Expand All @@ -42,6 +45,9 @@ public function foo(Connection $conn)

$fetch = $result->iterateColumn();
assertType('Traversable<int, string>', $fetch);

$fetch = $result->iterateKeyValue();
assertType('Traversable<string, int<0, 4294967295>>', $fetch);
}

public function executeQuery(Connection $conn, array $types, QueryCacheProfile $qcp)
Expand Down