Make WordPress Core

Changeset 62694


Ignore:
Timestamp:
07/11/2026 06:37:31 PM (5 weeks ago)
Author:
westonruter
Message:

Code Quality: Improve return types for post functions.

Narrow the conditional @phpstan-return types of get_post(), get_page(), get_page_by_path(), get_children(), and wp_get_recent_posts() so that the ARRAY_A and ARRAY_N outputs are typed as non-empty-array instead of array. This is consistent with WP_Post::to_array(), which always returns at least the object's declared properties and so can never yield an empty array. Slight refactoring is done on wp_get_recent_posts() to support static analysis.

Additionally, wp_get_post_revision() had no conditional return type at all, so its result was seen as WP_Post|array|null regardless of the requested $output. Describing it precisely resolves 30 pre-existing Cannot access property $ID on array|WP_Post errors in its callers.

Furthermore, trackback_url_list() now bails when get_post() returns null. Previously it fell through to $postdata['post_excerpt'], emitting "Trying to access array offset on value of type null" warnings when called with an invalid post ID.

Developed in https://github.com/WordPress/wordpress-develop/pull/12485.
Follow-up to r62648.

See #64898.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-post.php

    r62648 r62694  
    389389         *
    390390         * @return array<string, mixed> Object as array.
     391         *
     392         * @phpstan-return non-empty-array<string, mixed>
    391393         */
    392394        public function to_array() {
    393                 /** @var array<string, mixed> $post */
     395                /** @var non-empty-array<string, mixed> $post */
    394396                $post = get_object_vars( $this );
    395397
  • trunk/src/wp-includes/post.php

    r62648 r62694  
    991991 * @phpstan-return (
    992992 *     $args is array{ fields: 'ids', ... } ? int[] : (
    993  *         $output is 'ARRAY_A' ? array<int, array<string, mixed>> : (
    994  *             $output is 'ARRAY_N' ? array<int, array<int, mixed>> : WP_Post[]
     993 *         $output is 'ARRAY_A' ? array<int, non-empty-array<string, mixed>> : (
     994 *             $output is 'ARRAY_N' ? array<int, non-empty-array<int, mixed>> : WP_Post[]
    995995 *         )
    996996 *     )
     
    10411041                $weeuns = array();
    10421042                foreach ( (array) $kids as $kid ) {
    1043                         $weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
     1043                        /** @var non-empty-array<string, mixed> $vars */
     1044                        $vars               = get_object_vars( $kids[ $kid->ID ] );
     1045                        $weeuns[ $kid->ID ] = $vars;
    10441046                }
    10451047                return $weeuns;
     
    10471049                $babes = array();
    10481050                foreach ( (array) $kids as $kid ) {
    1049                         $babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
     1051                        /** @var non-empty-array<string, mixed> $vars */
     1052                        $vars              = get_object_vars( $kids[ $kid->ID ] );
     1053                        $babes[ $kid->ID ] = array_values( $vars );
    10501054                }
    10511055                return $babes;
     
    11251129 * @phpstan-param 'raw'|'edit'|'db'|'display' $filter
    11261130 * @phpstan-return (
    1127  *     $output is 'ARRAY_A' ? array<string, mixed>|null : (
    1128  *         $output is 'ARRAY_N' ? array<int, mixed>|null : (
     1131 *     $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
     1132 *         $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
    11291133 *             WP_Post|null
    11301134 *         )
     
    44534457 * @phpstan-param 'OBJECT'|'ARRAY_A' $output
    44544458 * @phpstan-return (
    4455  *     $output is 'ARRAY_A' ? array<int, array<string, mixed>> : WP_Post[]|false
     4459 *     $output is 'ARRAY_A' ? array<int, non-empty-array<string, mixed>> : WP_Post[]|false
    44564460 * )
    44574461 */
     
    44864490        // Backward compatibility. Prior to 3.1 expected posts to be returned in array.
    44874491        if ( ARRAY_A === $output ) {
     4492                $posts = array();
    44884493                foreach ( $results as $key => $result ) {
    4489                         /** @var array<string, mixed> $object_vars */
    4490                         $object_vars     = get_object_vars( $result );
    4491                         $results[ $key ] = $object_vars;
    4492                 }
    4493                 return $results ? $results : array();
     4494                        /** @var non-empty-array<string, mixed> $object_vars */
     4495                        $object_vars   = get_object_vars( $result );
     4496                        $posts[ $key ] = $object_vars;
     4497                }
     4498                return $posts;
    44944499        }
    44954500
     
    61476152                $postdata = get_post( $post_id, ARRAY_A );
    61486153
     6154                if ( ! $postdata ) {
     6155                        return;
     6156                }
     6157
    61496158                // Form an excerpt.
    61506159                $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );
     
    62076216 * @phpstan-param 'raw'|'edit'|'db'|'display' $filter
    62086217 * @phpstan-return (
    6209  *     $output is 'ARRAY_A' ? array<string, mixed>|null : (
    6210  *         $output is 'ARRAY_N' ? array<int, mixed>|null : (
     6218 *     $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
     6219 *         $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
    62116220 *             WP_Post|null
    62126221 *         )
     
    62356244 * @phpstan-param string|string[]              $post_type
    62366245 * @phpstan-return (
    6237  *     $output is 'ARRAY_A' ? array<string, mixed>|null : (
    6238  *         $output is 'ARRAY_N' ? array<int, mixed>|null : (
     6246 *     $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
     6247 *         $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
    62396248 *             WP_Post|null
    62406249 *         )
  • trunk/src/wp-includes/revision.php

    r62178 r62694  
    424424 * @param string      $filter Optional sanitization filter. See sanitize_post(). Default 'raw'.
    425425 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
     426 *
     427 * @phpstan-param int|WP_Post $post
     428 * @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
     429 * @phpstan-param 'raw'|'edit'|'db'|'display' $filter
     430 * @phpstan-return (
     431 *     $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : (
     432 *         $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : (
     433 *             WP_Post|null
     434 *         )
     435 *     )
     436 * )
    426437 */
    427438function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
     
    439450                return $revision;
    440451        } elseif ( ARRAY_A === $output ) {
     452                /** @var non-empty-array<string, mixed> $_revision */
    441453                $_revision = get_object_vars( $revision );
    442454                return $_revision;
    443455        } elseif ( ARRAY_N === $output ) {
    444                 $_revision = array_values( get_object_vars( $revision ) );
     456                /** @var non-empty-array<string, mixed> $vars */
     457                $vars      = get_object_vars( $revision );
     458                $_revision = array_values( $vars );
    445459                return $_revision;
    446460        }
Note: See TracChangeset for help on using the changeset viewer.