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
11 changes: 10 additions & 1 deletion src/Extensions/MysqliResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand All @@ -38,7 +40,14 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$methodName = strtolower($methodReflection->getName());
if ('fetch_assoc' === $methodName) {
if (count($methodCall->getArgs()) === 0) {
return $this->getRowType($resultType);
$rowType = $this->getRowType($resultType);
if ($rowType !== null) {
return TypeCombinator::union(
$rowType,
new NullType(),
new ConstantBooleanType(false)
);
}
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions tests/default/data/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function unionResult(mysqli $mysqli)
public function mysqliResultfetch(mysqli $mysqli)
{
$result = mysqli_query($mysqli, 'SELECT adaid FROM ada');
assertType('array{adaid: int<-32768, 32767>}', $result->fetch_assoc());
assertType('array{adaid: int<-32768, 32767>}|false|null', $result->fetch_assoc());

$result = mysqli_query($mysqli, 'SELECT adaid FROM ada');
assertType('list<array{int<-32768, 32767>}>', $result->fetch_all());
Expand All @@ -77,7 +77,7 @@ public function fetchAllColumns(mysqli $mysqli)
assertType('list<array{email: string, adaid: int<-32768, 32767>}>', $result->fetch_all(MYSQLI_ASSOC));
assertType('list<array{string, int<-32768, 32767>}>', $result->fetch_all(MYSQLI_NUM));
assertType('list<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $result->fetch_all(MYSQLI_BOTH));
assertType('array{email: string, adaid: int<-32768, 32767>}', $result->fetch_assoc());
assertType('array{email: string, adaid: int<-32768, 32767>}|false|null', $result->fetch_assoc());
foreach ($result as $row) {
assertType('array{email: string, adaid: int<-32768, 32767>}', $row);
}
Expand All @@ -93,7 +93,7 @@ public function fetchAllUnionResult(mysqli $mysqli)
assertType('list<array{adaid: int<-32768, 32767>}|array{email: string}>', $result->fetch_all(MYSQLI_ASSOC));
assertType('list<array{int<-32768, 32767>}|array{string}>', $result->fetch_all(MYSQLI_NUM));
assertType('list<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}|array{email: string, 0: string}>', $result->fetch_all(MYSQLI_BOTH));
assertType('array{adaid: int<-32768, 32767>}|array{email: string}', $result->fetch_assoc());
assertType('array{adaid: int<-32768, 32767>}|array{email: string}|false|null', $result->fetch_assoc());
}
}

Expand Down
28 changes: 14 additions & 14 deletions tests/sqlAst/config/.phpunit-phpstan-dba-mysqli.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading