1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix PdoStatementReturnTypeProvider (#4683)

* Fix PdoStatementReturnTypeProvider

Methods returning scalars may return null as well.

* Fix tests
This commit is contained in:
Benjamin Morel 2020-11-24 00:41:12 +01:00 committed by Daniil Gentili
parent 2ea96069eb
commit e9c608e7cb
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 20 additions and 9 deletions

View File

@ -37,20 +37,26 @@ class PdoStatementReturnTypeProvider implements \Psalm\Plugin\Hook\MethodReturnT
$fetch_mode = $first_arg_type->getSingleIntLiteral()->value;
switch ($fetch_mode) {
case \PDO::FETCH_ASSOC: // array<string,scalar>|false
case \PDO::FETCH_ASSOC: // array<string,scalar|null>|false
return new Type\Union([
new Type\Atomic\TArray([
Type::getString(),
Type::getScalar(),
new Type\Union([
new Type\Atomic\TScalar(),
new Type\Atomic\TNull()
])
]),
new Type\Atomic\TFalse(),
]);
case \PDO::FETCH_BOTH: // array<array-key,scalar>|false
case \PDO::FETCH_BOTH: // array<array-key,scalar|null>|false
return new Type\Union([
new Type\Atomic\TArray([
Type::getArrayKey(),
Type::getScalar()
new Type\Union([
new Type\Atomic\TScalar(),
new Type\Atomic\TNull()
])
]),
new Type\Atomic\TFalse(),
]);
@ -84,9 +90,14 @@ class PdoStatementReturnTypeProvider implements \Psalm\Plugin\Hook\MethodReturnT
new Type\Atomic\TFalse(),
]);
case \PDO::FETCH_NUM: // list<scalar>|false
case \PDO::FETCH_NUM: // list<scalar|null>|false
return new Type\Union([
new Type\Atomic\TList(Type::getScalar()),
new Type\Atomic\TList(
new Type\Union([
new Type\Atomic\TScalar(),
new Type\Atomic\TNull()
])
),
new Type\Atomic\TFalse(),
]);

View File

@ -603,7 +603,7 @@ class MethodCallTest extends TestCase
],
'pdoStatementFetchAssoc' => [
'<?php
/** @return array<string,scalar>|false */
/** @return array<string,null|scalar>|false */
function fetch_assoc() {
$p = new PDO("sqlite::memory:");
$sth = $p->prepare("SELECT 1");
@ -613,7 +613,7 @@ class MethodCallTest extends TestCase
],
'pdoStatementFetchBoth' => [
'<?php
/** @return array<scalar>|false */
/** @return array<null|scalar>|false */
function fetch_both() {
$p = new PDO("sqlite::memory:");
$sth = $p->prepare("SELECT 1");
@ -663,7 +663,7 @@ class MethodCallTest extends TestCase
],
'pdoStatementFetchNum' => [
'<?php
/** @return list<scalar>|false */
/** @return list<null|scalar>|false */
function fetch_named() {
$p = new PDO("sqlite::memory:");
$sth = $p->prepare("SELECT 1");