Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a25574b
Code Quality: Type wpdb query results from the columns selected.
westonruter Nov 25, 2026
bbaf0eb
Code Quality: Remove `@var` tags made redundant by the wpdb query types.
westonruter Nov 25, 2026
3b04d5e
Fix phpcs alignment gripes
westonruter Nov 26, 2026
c31d7bf
Code Quality: Make outer-joined columns nullable in wpdb query types.
westonruter Nov 26, 2026
bb4e1a8
Code Quality: Read quoted column aliases in wpdb query types.
westonruter Nov 26, 2026
7919bdc
Code Quality: Treat `prepare()` placeholders as unknown values in wpd…
westonruter Nov 26, 2026
ad2e08c
Code Quality: Type `ARRAY_N` rows with and without the multisite colu…
westonruter Nov 26, 2026
2dd2d5a
Code Quality: Collapse duplicate column names in positional wpdb row …
westonruter Nov 26, 2026
4fa83a9
Code Quality: Follow writes in enclosing conditions and loops for wpd…
westonruter Nov 26, 2026
a50bd58
Code Quality: Do not follow arrow function parameters for wpdb queries.
westonruter Nov 26, 2026
36cdd23
Code Quality: Name more unaliased select expressions as MySQL does.
westonruter Nov 26, 2026
6f529d8
Code Quality: Put a `return` in the wpdb query type extension on its …
westonruter Nov 26, 2026
8662982
Docs: Add serial commas to the wpdb query type documentation.
westonruter Nov 26, 2026
c5bcef2
Docs: Document the recent refinements to the wpdb query types.
westonruter Nov 26, 2026
af3f69d
Code Quality: Leave wpdb row types as declared for a non-constant `$o…
westonruter Nov 26, 2026
f3b773e
Code Quality: Leave column names holding unknown text unknown in wpdb…
westonruter Nov 26, 2026
ce7d48e
Code Quality: Follow writes in the statement of a wpdb query call.
westonruter Nov 26, 2026
7b51cf7
Code Quality: Type `OBJECT_K` keys from the first value of the row.
westonruter Nov 26, 2026
ec7b556
Code Quality: Do not take a table name extended by more text as the c…
westonruter Nov 26, 2026
21d024b
Code Quality: Include null in `get_col()` types for any column index.
westonruter Nov 26, 2026
ad1b63d
Code Quality: Read number and prefixed string literals whole in wpdb …
westonruter Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/wp-includes/class-wp-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ public static function get_instance( $id ) {

// A cached value that is not usable as a comment is treated as a cache miss.
if ( ! is_object( $_comment ) || ! isset( $_comment->comment_ID ) ) {
/** @var object{ comment_ID: string, comment_post_ID: string, comment_author: string, comment_author_email: string, comment_author_url: string, comment_author_IP: string, comment_date: string, comment_date_gmt: string, comment_content: string, comment_karma: string, comment_approved: string, comment_agent: string, comment_type: string, comment_parent: string, user_id: string }|null $_comment */
$_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );

if ( ! $_comment ) {
Expand Down
1 change: 0 additions & 1 deletion src/wp-includes/ms-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,6 @@ function wpmu_activate_signup(
) {
global $wpdb;

/** @var object{ signup_id: string, domain: string, path: string, title: string, user_login: string, user_email: string, registered: string, activated: string, active: string, activation_key: string, meta: string|null }|null $signup */
$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );

if ( empty( $signup ) ) {
Expand Down
1 change: 0 additions & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6562,7 +6562,6 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
post_status IN ('draft', 'pending', 'auto-draft') ASC, ID ASC
";

/** @var array<object{ ID: string, post_name: string, post_parent: string, post_type: string }> $pages */
$pages = $wpdb->get_results( $sql, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The escaping has been applied above via esc_sql().

$revparts = array_reverse( $parts );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2432,16 +2432,14 @@ protected function prefetch_reaction_summaries( $note_ids ) {
);

// Query 1: Get aggregated counts per emoji per note.
$query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content";
/** @var list<object{ comment_parent: decimal-int-string, comment_content: string, reaction_count: decimal-int-string }&stdClass>|null $counts */
$query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content";
$counts = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.

// Query 2: Get the current user's own reaction IDs (if logged in).
$my_reactions = array();
if ( $current_user_id ) {
$query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where";
$query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id );
/** @var list<object{ comment_ID: decimal-int-string, comment_parent: decimal-int-string, comment_content: string }&stdClass>|null $user_rows */
$query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where";
$query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id );
$user_rows = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.

if ( $user_rows ) {
Expand Down
1 change: 0 additions & 1 deletion src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2768,7 +2768,6 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
do_action( 'edited_terms', $term_id, $taxonomy, $args );
}

/** @var decimal-int-string|null $tt_id */
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );

if ( ! empty( $tt_id ) ) {
Expand Down
20 changes: 20 additions & 0 deletions tests/phpstan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ These identifiers are specific to WordPress, and can be ignored or baselined lik
| `wordpress.hookDocReferenceHookMissing` | The referenced file exists, but documents no hook of that name. Either the reference is stale, or the canonical docblock has moved. |
| `wordpress.hookParamCountMismatch` | The call passes a different number of arguments than the docblock documents `@param` tags for. Passing fewer risks an `ArgumentCountError` in a callback registered for the documented count; passing more silently drops the extra argument and leaves the documentation misleading. |

### Database query results

`WpdbQueryDynamicMethodReturnTypeExtension` types the results of `$wpdb->get_var()`, `get_row()`, `get_col()`, and `get_results()` from the columns their query selects, so that they need no inline `@var` tag. For example:

```php
$post = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE ID = %d", $id ) );
```

is typed `(object{ID: decimal-int-string, post_title: string}&stdClass)|null` rather than `stdClass|null`. Likewise, `ARRAY_A` and `ARRAY_N` rows become array shapes, `OBJECT_K` results are keyed by the first column's type, and `get_var()` and `get_col()` give the type of the column they return.

- **The columns of core tables are typed from the schema.** They are read from the `CREATE TABLE` statements in `wp_get_db_schema()`, so a column added there needs no change here. `wpdb` returns every value as a string, or null for SQL `NULL`, so an integer column is a `decimal-int-string`, a `DATETIME` column a `non-falsy-string`, and a column without `NOT NULL` is nullable. So is each column of a table on the outer side of a `LEFT JOIN` or `RIGHT JOIN`, which has no row to match for some rows of the result. The `spam` and `deleted` columns, which `users` only has on multisite, are optional, and an `ARRAY_N` row is typed both with and without them, since they shift the positions of the columns after them.
- **Select expressions are named and typed as MySQL does.** An alias names a column, whether a name or a quoted string, as does the text of an expression without one, like `COUNT(*)`, or the value of a string literal. Where columns share a name, the last one's value is the one in the row. `COUNT()` is a `decimal-int-string`, `SUM()` and `AVG()` are a nullable `numeric-string`, `MIN()` and `MAX()` of a column are the column's type or null, and a literal is that constant string. Anything else, including a column of a table that is not a core table, is `string|null`, which is what any value can be.
- **A query built up in variables is followed.** The query is reconstructed from the syntax tree, by `WpdbQueryVisitor`: literals, interpolation, `$wpdb->prepare()`, and concatenation are joined, each placeholder of a prepared query is a single unknown value, `$wpdb->posts` and the like name the table, and a variable is replaced by what the statements before the call assign and append to it. Only what the select list and the `FROM` clause need must be known, so a `WHERE` clause built from parameters does no harm. The variable is not followed through a loop which writes to it, through a branch which does more than append to it, or past an `if` condition which assigns it, and an arrow function's parameter is not followed outside it.

Where the columns cannot be determined, such as for `SELECT *` from a table that is not a core table, for a select list holding a variable, or for a query that is not `SELECT`, the method's declared return type is used as before. The cases in which a method returns null or an empty array are as declared either way, since they depend on its arguments rather than the query.

These types assume `wpdb` itself. A `db.php` drop-in that replaces it could return values of other types.

Since the schema is read from `schema.php` rather than through PHPStan's reflection, PHPStan does not know that the files with queries depend on it. After changing a column in `schema.php`, [clear the cache](#clear-the-cache-after-changing-anything-in-this-directory) so that the queries are analyzed again.

## Ignoring and baselining errors

As we adopt PHPStan iteratively, you may be faced with false positives due to legacy code, or code that is not worth changing at this time.
Expand Down
Loading
Loading