Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#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, themysqli_resultandmysqli_stmtfetch methods,pg_fetch_*()andodbc_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 forSQLite3Result::fetchArray()and themysqli_fetch_*()functions.Impact
At level 8 (https://phpstan.org/r/378e96de-a5b8-44b7-9311-7b20fee17942, PHP 8.1 and newer):
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/KT3VfFix
PDOStatement::fetch(),::fetchAll()and::fetchObject()listio.db,nondeterministicandwrites-this, likeSQLite3Result::fetchArray(), plus'*'becausefetchObject()and thePDO::FETCH_CLASSmode run the constructor of the class andPDO::FETCH_FUNCruns a callback.::fetchColumn()and::nextRowset()listio.db,nondeterministicandwrites-this. Withsqlite::memory:, eachfetch()andfetchColumn()returns the next row, a secondfetchAll()returns an empty array, and the constructor and the callback run: https://3v4l.org/069gKpg_fetch_array(),pg_fetch_assoc(),pg_fetch_row()andpg_fetch_result()listnondeterministicandwrites-args($result), andpg_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 noio.db: libpq keeps the whole result on the client, so they read no more from the server, likecurl_multi_info_read()in Describe built-in calls that move a handle or an iterator #7188.$resultis 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()andodbc_next_result()listio.db,nondeterministicandwrites-argsof the result:$statementin the PHP 8 stubs and$resultor$result_idin the legacy function map.odbc_fetch_into()also writes$array($result_array).odbc_fetch_object()returns astdClassand 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()andmysqli::next_result()list'*', likemysqli_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
@phpstan-purefunction, 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.bzread()does forbzerrno()since Describe built-in calls that move a handle or an iterator #7188. That is right for$stmt->getColumnMeta(0), whosenative_typefollows the current row with pdo_sqlite (integer,string,nullfor the rows of one column), and for$stmt->errorCode(), which a failingnextRowset()sets toIM001; both are kept on 2.4.x. It loses some precision:$stmt->columnCount()narrowed to1isint<0, max>after$stmt->fetchColumn(), and the same happens to$stmt->rowCount(),$stmt->queryString,pg_num_rows($result)afterpg_fetch_row($result),odbc_num_rows($result)afterodbc_fetch_row($result)and$result->field_countafter$result->fetch_assoc()(the mysqli methods list'*', which writes the receiver).Has side-effects: MaybetoYes. The 15 that are not mysqli methods also gain anEffects:line, and the nine that listwrites-argswithout'*'gainWrites parameterlines.Not changed here
pg_fetch_all()andpg_fetch_all_columns()return every row of the result, wherever the row pointer is.mysqli_result::data_seek(),::field_seek(),mysqli_stmt::data_seek(),pg_result_seek()andSQLite3Result::reset(): their own results don't vary, and the fetch calls after them are no longer remembered.mysqli_result::fetch_fields(),::fetch_field_direct()andodbc_result(): their results don't change with a fetch, or depend only on the current row. The same goes for the othermysqlimethods whose function forms list'*'.pg_get_result($connection)andpg_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.oci_fetch_*(),sqlsrv_fetch*(),db2_fetch_*()andmysql_fetch_*().Testing
side-effects-database-fetch.php(PHP 8.1+): for the fivePDOStatementmethods, the tenmysqlimethods and the fivepg_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+, forOdbc\Result): the same for the fiveodbc_*()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.