Skip to content

Commit be7c18b

Browse files
staabmclxmstaab
andauthored
doctrine-dbal: fetchAssociative() and fetchNumeric() both can return false (#195)
Co-authored-by: Markus Staab <[email protected]>
1 parent 7bfd7f0 commit be7c18b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
1414
use PHPStan\Reflection\ParametersAcceptorSelector;
15+
use PHPStan\Type\Constant\ConstantBooleanType;
1516
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1617
use PHPStan\Type\Generic\GenericObjectType;
1718
use PHPStan\Type\IntegerType;
1819
use PHPStan\Type\MixedType;
1920
use PHPStan\Type\Type;
21+
use PHPStan\Type\TypeCombinator;
2022
use staabm\PHPStanDba\QueryReflection\QueryReflection;
2123
use staabm\PHPStanDba\QueryReflection\QueryReflector;
2224
use Traversable;
@@ -93,6 +95,9 @@ private function inferType(MethodReflection $methodReflection, Expr $queryExpr,
9395
return new GenericObjectType(Traversable::class, [new IntegerType(), $resultType]);
9496
}
9597

98+
// false is returned if no rows are found.
99+
$resultType = TypeCombinator::union($resultType, new ConstantBooleanType(false));
100+
96101
return $resultType;
97102
}
98103

tests/data/doctrine-dbal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public function fetchAssociative(Connection $conn)
5656
{
5757
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid = ?';
5858
$fetchResult = $conn->fetchAssociative($query, [1]);
59-
assertType('array{email: string, adaid: int<0, 4294967295>, gesperrt: int<-128, 127>, freigabe1u1: int<-128, 127>}', $fetchResult);
59+
assertType('array{email: string, adaid: int<0, 4294967295>, gesperrt: int<-128, 127>, freigabe1u1: int<-128, 127>}|false', $fetchResult);
6060
}
6161

6262
public function fetchNumeric(Connection $conn)
6363
{
6464
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid = ?';
6565
$fetchResult = $conn->fetchNumeric($query, [1]);
66-
assertType('array{string, int<0, 4294967295>, int<-128, 127>, int<-128, 127>}', $fetchResult);
66+
assertType('array{string, int<0, 4294967295>, int<-128, 127>, int<-128, 127>}|false', $fetchResult);
6767
}
6868

6969
public function iterateAssociative(Connection $conn)

0 commit comments

Comments
 (0)