Skip to content

Commit

Permalink
Improve typing for Result interface
Browse files Browse the repository at this point in the history
  • Loading branch information
devnix committed Feb 4, 2024
1 parent 0741bfb commit c02a487
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Cache/ArrayResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ArrayResult implements Result
private readonly int $columnCount;
private int $num = 0;

/** @param list<array<string, mixed>> $data */
/** @param list<non-empty-array<string, mixed>> $data */
public function __construct(private array $data)
{
$this->columnCount = $data === [] ? 0 : count($data[0]);
Expand Down Expand Up @@ -89,7 +89,7 @@ public function free(): void
$this->data = [];
}

/** @return array<string, mixed>|false */
/** @return non-empty-array<string,mixed>|false */
private function fetch(): array|false
{
if (! isset($this->data[$this->num])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public function fetchAllNumeric(string $query, array $params = [], array $types
* @param list<mixed>|array<string, mixed> $params
* @psalm-param WrapperParameterTypeArray $types
*
* @return list<array<string,mixed>>
* @return list<non-empty-array<string,mixed>>
*
* @throws Exception
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Driver/FetchUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\DBAL\Driver;

use function PHPStan\dumpType;

/** @internal */
final class FetchUtils
{
Expand All @@ -20,7 +22,7 @@ public static function fetchOne(Result $result): mixed
}

/**
* @return list<list<mixed>>
* @return list<non-empty-list<mixed>>
*
* @throws Exception
*/
Expand All @@ -36,7 +38,7 @@ public static function fetchAllNumeric(Result $result): array
}

/**
* @return list<array<string,mixed>>
* @return list<non-empty-array<string,mixed>>
*
* @throws Exception
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/IBMDB2/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(private readonly mixed $statement)

public function fetchNumeric(): array|false
{
/** @var non-empty-list<mixed>|false $row */
$row = @db2_fetch_array($this->statement);

if ($row === false && db2_stmt_error($this->statement) !== '02000') {
Expand All @@ -39,6 +40,7 @@ public function fetchNumeric(): array|false

public function fetchAssociative(): array|false
{
/** @var non-empty-array<string,mixed>|false $row */
$row = @db2_fetch_assoc($this->statement);

if ($row === false && db2_stmt_error($this->statement) !== '02000') {
Expand Down
10 changes: 10 additions & 0 deletions src/Driver/Mysqli/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function array_column;
use function array_combine;
use function array_fill;
use function assert;
use function count;

final class Result implements ResultInterface
Expand Down Expand Up @@ -78,6 +79,11 @@ public function __construct(private readonly mysqli_stmt $statement)
}
}

/**
* @return non-empty-list<mixed>|false
*
* @throws StatementError
*/
public function fetchNumeric(): array|false
{
try {
Expand All @@ -100,6 +106,8 @@ public function fetchNumeric(): array|false
$values[] = $v;
}

assert([] !== $values);

return $values;
}

Expand All @@ -111,6 +119,8 @@ public function fetchAssociative(): array|false
return false;
}

assert($this->columnNames !== []);

return array_combine($this->columnNames, $values);
}

Expand Down
28 changes: 21 additions & 7 deletions src/Driver/PgSQL/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function pg_field_type;
use function pg_free_result;
use function pg_num_fields;
use function PHPStan\dumpType;
use function substr;

use const PGSQL_ASSOC;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function fetchNumeric(): array|false
return false;
}

/** @var non-empty-list<mixed>|false $row */
$row = pg_fetch_row($this->result);
if ($row === false) {
return false;
Expand All @@ -68,6 +70,7 @@ public function fetchAssociative(): array|false
return false;
}

/** @var non-empty-array<string,mixed>|false $row */
$row = pg_fetch_assoc($this->result);
if ($row === false) {
return false;
Expand All @@ -93,7 +96,7 @@ public function fetchAllNumeric(): array

return array_map(
fn (array $row) => $this->mapNumericRow($row, $types),
pg_fetch_all($this->result, PGSQL_NUM),
$this->pgFetchAll($this->result, PGSQL_NUM),
);
}

Expand All @@ -108,7 +111,7 @@ public function fetchAllAssociative(): array

return array_map(
fn (array $row) => $this->mapAssociativeRow($row, $types),
pg_fetch_all($this->result, PGSQL_ASSOC),
$this->pgFetchAll($this->result, PGSQL_ASSOC),
);
}

Expand Down Expand Up @@ -184,10 +187,10 @@ private function fetchAssociativeColumnTypes(): array
}

/**
* @param list<string|null> $row
* @param non-empty-list<string|null> $row
* @param array<int, string> $types
*
* @return list<mixed>
* @return non-empty-list<mixed>
*/
private function mapNumericRow(array $row, array $types): array
{
Expand All @@ -201,10 +204,10 @@ private function mapNumericRow(array $row, array $types): array
}

/**
* @param array<string, string|null> $row
* @param array<string, string> $types
* @param non-empty-array<string, string|null> $row
* @param array<string, string> $types
*
* @return array<string, mixed>
* @return non-empty-array<string, mixed>
*/
private function mapAssociativeRow(array $row, array $types): array
{
Expand Down Expand Up @@ -237,4 +240,15 @@ private function mapType(string $postgresType, ?string $value): string|int|float
default => $value,
};
}

/**
* @return (
* $mode is PGSQL_NUM ? array<non-empty-list<string|null>> : array<non-empty-array<string, mixed>>
* )
*/
private function pgFetchAll(PgSqlResult $result, int $mode = PGSQL_ASSOC): array
{
/** @var array<non-empty-array<string, mixed>> */
return pg_fetch_all($result, $mode);
}
}
2 changes: 2 additions & 0 deletions src/Driver/SQLite3/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function fetchNumeric(): array|false
return false;
}

/** @var non-empty-list<mixed>|false */
return $this->result->fetchArray(SQLITE3_NUM);
}

Expand All @@ -36,6 +37,7 @@ public function fetchAssociative(): array|false
return false;
}

/** @var non-empty-array<string,mixed>|false */
return $this->result->fetchArray(SQLITE3_ASSOC);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Portability/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(bool $convertEmptyStringToNull, bool $rightTrimStrin
/**
* @param array<int,mixed>|false $row
*
* @return list<mixed>|false
* @return non-empty-list<mixed>|false
*/
public function convertNumeric(array|false $row): array|false
{
Expand All @@ -63,7 +63,7 @@ public function convertNumeric(array|false $row): array|false
/**
* @param array<string,mixed>|false $row
*
* @return array<string,mixed>|false
* @return non-empty-array<string, mixed>|false
*/
public function convertAssociative(array|false $row): array|false
{
Expand All @@ -78,7 +78,7 @@ public function convertOne(mixed $value): mixed
/**
* @param list<list<mixed>> $data
*
* @return list<list<mixed>>
* @return list<non-empty-list<mixed>>
*/
public function convertAllNumeric(array $data): array
{
Expand All @@ -88,7 +88,7 @@ public function convertAllNumeric(array $data): array
/**
* @param list<array<string,mixed>> $data
*
* @return list<array<string,mixed>>
* @return list<non-empty-array<string,mixed>>
*/
public function convertAllAssociative(array $data): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(private readonly DriverResult $result, private reado
/**
* Returns the next row of the result as a numeric array or FALSE if there are no more rows.
*
* @return list<mixed>|false
* @return non-empty-list<mixed>|false
*
* @throws Exception
*/
Expand All @@ -39,7 +39,7 @@ public function fetchNumeric(): array|false
/**
* Returns the next row of the result as an associative array or FALSE if there are no more rows.
*
* @return array<string,mixed>|false
* @return non-empty-array<string,mixed>|false
*
* @throws Exception
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ public function fetchOne(): mixed
/**
* Returns an array containing all of the result rows represented as numeric arrays.
*
* @return list<list<mixed>>
* @return list<non-empty-list<mixed>>
*
* @throws Exception
*/
Expand All @@ -85,7 +85,7 @@ public function fetchAllNumeric(): array
/**
* Returns an array containing all of the result rows represented as associative arrays.
*
* @return list<array<string,mixed>>
* @return list<non-empty-array<string,mixed>>
*
* @throws Exception
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/ArrayStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ArrayStatementTest extends TestCase
{
/** @var list<array<string, mixed>> */
/** @var list<non-empty-array<string, mixed>> */
private array $users = [
[
'username' => 'jwage',
Expand Down
2 changes: 1 addition & 1 deletion tests/Connection/CachedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testCachedQueryWithChangedImplementationIsExecutedTwice(): void
)->fetchAllAssociative());
}

/** @param list<array<string, mixed>> $data */
/** @param list<non-empty-array<string, mixed>> $data */
private function createConnection(int $expectedQueryCount, array $data): Connection
{
$connection = $this->createMock(Driver\Connection::class);
Expand Down

0 comments on commit c02a487

Please sign in to comment.