Skip to content

Describe database calls that fetch the next row - #7331

Open
zonuexe wants to merge 1 commit into
phpstan:2.4.xfrom
zonuexe:fix/database-fetch-metadata
Open

zonuexe wants to merge 1 commit into
phpstan:2.4.xfrom
zonuexe:fix/database-fetch-metadata

Conversation

@zonuexe

@zonuexe zonuexe commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

#7188 described the built-in calls that move a handle or an iterator and left database fetch calls for a follow-up. This is that follow-up: PDOStatement::fetch() and its siblings, the mysqli_result and mysqli_stmt fetch methods, pg_fetch_*() and odbc_fetch_*() have no entry in the function metadata, so PHPStan remembers their results, although each call returns the next row. This PR lists their effects, following the existing entry for SQLite3Result::fetchArray() and the mysqli_fetch_*() functions.

Impact

function countAfterHeader(\PDOStatement $stmt): int
{
	if ($stmt->fetch() === false) {
		return 0;
	}

	$count = 0;
	while ($stmt->fetch() !== false) {
		$count++;
	}

	return $count;
}

/** @return array<int|string, string|null>|null */
function secondRow(\PgSql\Result $result): ?array
{
	if (pg_fetch_assoc($result) === false) {
		return null;
	}

	$second = pg_fetch_assoc($result);
	if ($second === false) {
		return null;
	}

	return $second;
}

At level 8 (https://phpstan.org/r/378e96de-a5b8-44b7-9311-7b20fee17942, PHP 8.1 and newer):

10: Strict comparison using !== between mixed and false will always evaluate to true.
14: Unreachable statement - code above always terminates.
25: Strict comparison using === between array<int|string, string|null> and false will always evaluate to false.

With this PR none of these are reported. With an SQLite table of three rows, countAfterHeader() returns 2, and 0 for an empty result: https://3v4l.org/KT3Vf

Fix

  • PDOStatement::fetch(), ::fetchAll() and ::fetchObject() list io.db, nondeterministic and writes-this, like SQLite3Result::fetchArray(), plus '*' because fetchObject() and the PDO::FETCH_CLASS mode run the constructor of the class and PDO::FETCH_FUNC runs a callback. ::fetchColumn() and ::nextRowset() list io.db, nondeterministic and writes-this. With sqlite::memory:, each fetch() and fetchColumn() returns the next row, a second fetchAll() returns an empty array, and the constructor and the callback run: https://3v4l.org/069gK
  • pg_fetch_array(), pg_fetch_assoc(), pg_fetch_row() and pg_fetch_result() list nondeterministic and writes-args($result), and pg_fetch_object() lists '*' too, since it also runs a constructor. Without a row number they fetch the next row and move the row pointer of the result. They list no io.db: libpq keeps the whole result on the client, so they read no more from the server, like curl_multi_info_read() in Describe built-in calls that move a handle or an iterator #7188. $result is the parameter name in both the PHP 8 stubs and the legacy function map.
  • odbc_fetch_array(), odbc_fetch_object(), odbc_fetch_row(), odbc_fetch_into() and odbc_next_result() list io.db, nondeterministic and writes-args of the result: $statement in the PHP 8 stubs and $result or $result_id in the legacy function map. odbc_fetch_into() also writes $array ($result_array). odbc_fetch_object() returns a stdClass and runs no constructor.
  • mysqli_result::fetch_all(), ::fetch_array(), ::fetch_assoc(), ::fetch_column(), ::fetch_field(), ::fetch_object(), ::fetch_row(), mysqli_stmt::fetch(), mysqli_stmt::next_result() and mysqli::next_result() list '*', like mysqli_fetch_assoc() and the other function forms they are aliases of.

I had no MySQL, PostgreSQL or ODBC server to test against, so the mysqli, pgsql and odbc entries follow the manual and the function forms; only the PDO behaviour is checked at runtime.

Other changes this brings

  • In a @phpstan-pure function, these calls were reported as possibly impure (possiblyImpure.methodCall, possiblyImpure.functionCall) and are now reported as impure (impure.methodCall, impure.functionCall). An ignore or baseline entry for the old identifiers stops matching.
  • A fetch now forgets the other remembered calls on the same statement or result, as bzread() does for bzerrno() since Describe built-in calls that move a handle or an iterator #7188. That is right for $stmt->getColumnMeta(0), whose native_type follows the current row with pdo_sqlite (integer, string, null for the rows of one column), and for $stmt->errorCode(), which a failing nextRowset() sets to IM001; both are kept on 2.4.x. It loses some precision: $stmt->columnCount() narrowed to 1 is int<0, max> after $stmt->fetchColumn(), and the same happens to $stmt->rowCount(), $stmt->queryString, pg_num_rows($result) after pg_fetch_row($result), odbc_num_rows($result) after odbc_fetch_row($result) and $result->field_count after $result->fetch_assoc() (the mysqli methods list '*', which writes the receiver).
  • Unchanged: none of these calls on its own line is reported as having no effect, before or after.
  • In the reflection golden output, the 25 symbols above go from Has side-effects: Maybe to Yes. The 15 that are not mysqli methods also gain an Effects: line, and the nine that list writes-args without '*' gain Writes parameter lines.

Not changed here

  • pg_fetch_all() and pg_fetch_all_columns() return every row of the result, wherever the row pointer is.
  • The calls that only move the cursor, mysqli_result::data_seek(), ::field_seek(), mysqli_stmt::data_seek(), pg_result_seek() and SQLite3Result::reset(): their own results don't vary, and the fetch calls after them are no longer remembered.
  • mysqli_result::fetch_fields(), ::fetch_field_direct() and odbc_result(): their results don't change with a fetch, or depend only on the current row. The same goes for the other mysqli methods whose function forms list '*'.
  • pg_get_result($connection) and pg_get_notify($connection) return the next result or notification of the connection on each call and are still remembered. PDOStatement::closeCursor() and ::getIterator() have no entry either. These act on the connection or the cursor rather than fetch a row, and are left for a follow-up.
  • The fetch functions of PECL and removed extensions, such as oci_fetch_*(), sqlsrv_fetch*(), db2_fetch_*() and mysql_fetch_*().

Testing

  • side-effects-database-fetch.php (PHP 8.1+): for the five PDOStatement methods, the ten mysqli methods and the five pg_fetch_*() functions, a second identical call after a narrowing is not narrowed. Without the metadata change, all 20 assertions get the narrowed type.
  • side-effects-database-fetch-php84.php (PHP 8.4+, for Odbc\Result): the same for the five odbc_*() functions; all five fail without the change.
  • StrictComparisonOfDifferentTypesRuleTest::testDatabaseFetch: nothing is reported for the two functions above. Without the change, lines 15 and 30 of the data file are reported.

PDOStatement::fetch() and its siblings, the mysqli_result and
mysqli_stmt fetch methods, pg_fetch_*() and odbc_fetch_*() had no
entry, so their results were remembered although each call returns the
next row of the result.

The PDOStatement methods now list io.db, nondeterministic and
writes-this like SQLite3Result::fetchArray(), with '*' for fetch(),
fetchAll() and fetchObject(), which can run a constructor or a callback.
The odbc functions list io.db, nondeterministic and writes-args of the
result. The pgsql functions list nondeterministic and writes-args of the
result, but no io.db, since libpq keeps the whole result on the client;
pg_fetch_object() lists '*' too. The mysqli methods list '*', like the
mysqli_*() functions they are aliases of.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant