Code Quality: Add conditional return types to post functions.
Annotate the core post retrieval, sanitization, and insertion APIs with PHPStan conditional return types keyed on their mode arguments, so static analysis can resolve the concrete result at each call site:
get_post(), get_page(), get_page_by_path(), get_children(), and get_post_types() narrow on $output.
get_posts() and WP_Query::query() narrow on the fields argument.
get_post_field() and sanitize_post_field() narrow on $field.
wp_insert_post(), wp_update_post(), and wp_insert_attachment() narrow on $wp_error.
Matching @phpstan-param tags are added where the analyzed return depends on a narrowed input.
Separately, several previously generic or underspecified types are filled in where the precise type is known:
WP_Post::to_array() is typed array<string, mixed> rather than a bare array.
WP_Post::$filter is annotated with its recognized context values rather than a bare string.
WP_Post::filter() is corrected to return WP_Post|false (previously WP_Post), and its missing summary is documented.
sanitize_post()'s $post parameter is narrowed from object to stdClass|WP_Post.
Because these functions were previously typed as returning mixed or a bare object, the sharper types let analysis see through to hundreds of downstream call sites, for a net reduction of roughly 280 PHPStan errors across the tree.
A few supporting runtime changes make the types hold:
get_post() now guards against the false that WP_Post::filter( 'raw' ) can return for a since-deleted post.
WP_Post::get_instance() tightens its cache-hit check.
- post IDs are cast to
int where passed on.
Developed in https://github.com/WordPress/wordpress-develop/pull/12426.
Follow-up to r61789.
See #64898, #64896.