Skip to content

Type-stubs for pg_fetch_all are incorrect #155

Description

@JoelNiemela

The current type-stubs for pg_fetch_all are as follows:

/**
 * @return array<int, array>
 * @refcount 1
 */
#[\Since('8.1')]
function pg_fetch_all(\PgSql\Result $result, int $mode = PGSQL_ASSOC): array
{
}

(See https://github.com/phpstan/php-8-stubs/blob/1f23686dca88ed6d9a96c673d16ce11a16746af4/stubs/ext/pgsql/pg_fetch_all.php)

But as far as I can tell, pg_fetch_all actually returns list<array>. I've tried to verify this by looking through the pg_fetch_all source, and I'm quite sure it will always return a list, but it's a bit too dense for me to be certain.

I've also run the following assert on our codebase, and at least for all our queries, it true:

$data = pg_fetch_all($result);
assert(array_is_list($data));

Side-note: this is much less important, but would it in theory be possible to do something like the following, to further restrict the type?

/**
 * @return ($mode is PGSQL_NUM
 *     ? list<list<string|null>>
 *     : ($mode is PGSQL_ASSOC
 *         ? list<array<string, ?null>>
 *         : list<array<array-key, ?string>>
 *     )
 * )
 */

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions