Make WordPress Core

Changeset 65864


Ignore:
Timestamp:
12/02/2026 06:53:35 PM (2 days ago)
Author:
jorgefilipecosta
Message:

Abilities API: Resolve the PHPStan strict baseline errors in the core abilities.

Narrows the types in the content, settings and users abilities so src/wp-includes/abilities passes the strict PHPStan configuration without baseline entries. Removes the 20 entries and the cast.double, cast.string and foreach.nonIterable baselines they emptied.
Documents the types that rest_is_boolean() and rest_is_integer() accept and assert, and checks post type capabilities through a helper that requires them to be strings. The content query also checks that the page size is an integer, and it primes caches for and formats only the WP_Post results. Settings exposure names are cast to a string only when they are scalar.

Props jorgefilipecosta.
Fixes #66912.

Location:
trunk
Files:
11 edited

Legend:

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

    r65824 r65864  
    304304         */
    305305        private function can_query_status( string $status, WP_Post_Type $post_type_object ): bool {
    306                 return in_array( $status, array( 'publish', 'private' ), true ) || is_post_status_viewable( $status ) || current_user_can( $post_type_object->cap->edit_posts );
     306                return in_array( $status, array( 'publish', 'private' ), true ) || is_post_status_viewable( $status ) || $this->current_user_can_for_post_type( $post_type_object, 'edit_posts' );
     307        }
     308
     309        /**
     310         * Checks whether the current user has a capability of a post type, such as `edit_posts`.
     311         *
     312         * @since 7.3.0
     313         *
     314         * @param WP_Post_Type $post_type_object The post type.
     315         * @param string       $capability       The name of the capability in the post type's `cap` object.
     316         * @return bool Whether the current user has the capability.
     317         */
     318        private function current_user_can_for_post_type( WP_Post_Type $post_type_object, string $capability ): bool {
     319                $cap = $post_type_object->cap->$capability ?? null;
     320
     321                return is_string( $cap ) && current_user_can( $cap );
    307322        }
    308323
    … …  
    607622                        'posts_per_page'         => $per_page,
    608623                        'paged'                  => $page,
    609                         'perm'                   => current_user_can( $post_type_object->cap->read_private_posts ) ? '' : 'readable',
     624                        'perm'                   => $this->current_user_can_for_post_type( $post_type_object, 'read_private_posts' ) ? '' : 'readable',
    610625                        'ignore_sticky_posts'    => true,
    611626                        'update_post_meta_cache' => $prime_post_caches,
    … …  
    647662                 * paging, the query returns every post on one page.
    648663                 */
    649                 $query_per_page = (int) $query->get( 'posts_per_page' );
    650                 if ( $query->get( 'nopaging' ) || $query_per_page < 1 ) {
     664                $query_per_page = $query->get( 'posts_per_page' );
     665                if ( $query->get( 'nopaging' ) || ! is_int( $query_per_page ) || $query_per_page < 1 ) {
    651666                        $total_pages = $total > 0 ? 1 : 0;
    652667                } else {
    … …  
    669684                 * with setup_postdata(), which reads the author.
    670685                 */
    671                 update_post_parent_caches( $query->posts );
    672                 update_post_author_caches( $query->posts );
     686                $query_posts = array_filter( $query->posts ?? array(), static fn( $post ): bool => $post instanceof WP_Post );
     687                update_post_parent_caches( $query_posts );
     688                update_post_author_caches( $query_posts );
    673689
    674690                $posts = array();
    675                 foreach ( $query->posts as $post ) {
     691                foreach ( $query_posts as $post ) {
    676692                        /*
    677693                         * Skip posts of other post types, which query filters can add, such as a
    … …  
    679695                         * is_main_query() check.
    680696                         */
    681                         if ( ! $post instanceof WP_Post || $post_type !== $post->post_type ) {
     697                        if ( $post_type !== $post->post_type ) {
    682698                                continue;
    683699                        }
    … …  
    969985         * @since 7.2.0
    970986         *
    971          * @return array<string, mixed> Post field definitions.
     987         * @return array<string, array<string, mixed>> Post field definitions.
    972988         */
    973989        private function get_post_properties(): array {
    … …  
    11241140                        $paths[] = $name;
    11251141
    1126                         foreach ( array_keys( $schema['properties'] ?? array() ) as $sub_field ) {
     1142                        foreach ( array_keys( (array) ( $schema['properties'] ?? array() ) ) as $sub_field ) {
    11271143                                $paths[] = "{$name}.{$sub_field}";
    11281144                        }
    … …  
    15511567         * @since 7.2.0
    15521568         *
    1553          * @param bool        $required Whether the post currently requires a password.
    1554          * @param WP_Post|null $post     The post being checked, or null if no post is found.
     1569         * @param mixed $required Whether the post currently requires a password.
     1570         * @param mixed $post     The post being checked, or null if no post is found.
    15551571         * @return bool Whether the post still requires a password.
    15561572         */
    … …  
    16931709         * @since 7.2.0
    16941710         *
     1711         * @global array<string, positive-int> $wp_actions Stores the number of times each action was triggered.
     1712         *
    16951713         * @param array<string, mixed> $previous_context The loop globals that get_post_context() returned.
    16961714         * @param bool                 $the_post_fired   Whether the `the_post` action had fired before the ability set up the post.
    16971715         */
    16981716        private function restore_post_context( array $previous_context, bool $the_post_fired ): void {
     1717                global $wp_actions;
     1718
    16991719                $previous_post = $previous_context['post'] ?? null;
    17001720
    … …  
    17361756                         */
    17371757                        if ( ! $previous_post instanceof WP_Post && ! $the_post_fired ) {
    1738                                 unset( $GLOBALS['wp_actions']['the_post'] );
     1758                                unset( $wp_actions['the_post'] );
    17391759                        }
    17401760                }
  • trunk/src/wp-includes/abilities/class-wp-abilities-settings.php

    r65861 r65864  
    708708
    709709                        $option_name  = (string) $option_name;
    710                         $exposed_name = empty( $show['name'] ) ? $option_name : (string) $show['name'];
     710                        $exposed_name = ( empty( $show['name'] ) || ! is_scalar( $show['name'] ) ) ? $option_name : (string) $show['name'];
    711711
    712712                        if ( isset( $settings[ $exposed_name ] ) ) {
    … …  
    753753         *
    754754         * @param array<string, mixed> $args The setting registration arguments.
    755          * @return array<string, mixed> The exposure arguments, with optional `name` and `schema` keys.
     755         * @return array<mixed> The exposure arguments, with optional `name` and `schema` keys.
    756756         */
    757757        private function get_exposure_args( array $args ): array {
    … …  
    772772         *
    773773         * @param array<string, mixed> $args The setting registration arguments.
    774          * @param array<string, mixed> $show The exposure arguments, see get_exposure_args().
     774         * @param array<mixed>        $show The exposure arguments, see get_exposure_args().
    775775         * @return array<string, mixed> The value JSON Schema.
    776776         */
    777         private function get_value_schema( array $args, $show ): array {
     777        private function get_value_schema( array $args, array $show ): array {
    778778                $schema = array(
    779779                        'type' => $args['type'],
  • trunk/src/wp-includes/abilities/class-wp-abilities-users.php

    r65726 r65864  
    439439         * @since 7.2.0
    440440         *
    441          * @return array<string, mixed> User field definitions.
     441         * @return array<string, array<string, mixed>> User field definitions.
    442442         */
    443443        private function get_user_properties(): array {
    … …  
    558558                        $paths[] = $name;
    559559
    560                         foreach ( array_keys( $schema['properties'] ?? array() ) as $sub_field ) {
     560                        foreach ( array_keys( (array) ( $schema['properties'] ?? array() ) ) as $sub_field ) {
    561561                                $paths[] = "{$name}.{$sub_field}";
    562562                        }
  • trunk/src/wp-includes/rest-api.php

    r65794 r65864  
    16761676 * @since 4.7.0
    16771677 *
    1678  * @param bool|string $maybe_bool The value being evaluated.
     1678 * @param mixed $maybe_bool The value being evaluated.
    16791679 * @return bool True if a boolean, otherwise false.
     1680 *
     1681 * @phpstan-assert-if-true bool|int|string $maybe_bool
    16801682 */
    16811683function rest_is_boolean( $maybe_bool ) {
    … …  
    17201722 * @param mixed $maybe_integer The value being evaluated.
    17211723 * @return bool True if an integer, otherwise false.
     1724 *
     1725 * @phpstan-assert-if-true int|float|string $maybe_integer
    17221726 */
    17231727function rest_is_integer( $maybe_integer ): bool {
    … …  
    33123316 * @since 5.6.0 Support the "patternProperties" keyword.
    33133317 *
    3314  * @param array $schema The schema to modify.
    3315  * @return array The modified schema.
     3318 * @param array<string, mixed> $schema The schema to modify.
     3319 * @return array<string, mixed> The modified schema.
    33163320 */
    33173321function rest_default_additional_properties_to_false( $schema ) {
  • trunk/tests/phpstan/baselines-strict/argument.type.neon

    r65861 r65864  
    3030                        path: ../../../src/wp-includes/abilities-api/class-wp-ability.php
    3131                -
    32                         message: '#^Parameter \#1 \$capability of function current_user_can expects string, mixed given\.$#'
    33                         identifier: argument.type
    34                         count: 2
    35                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    36                 -
    37                         message: '#^Parameter \#1 \$input of function array_keys expects array, mixed given\.$#'
    38                         identifier: argument.type
    39                         count: 1
    40                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    41                 -
    42                         message: '#^Parameter \#1 \$posts of function update_post_author_caches expects array\<WP_Post\>, array\<int\|WP_Post\>\|null given\.$#'
    43                         identifier: argument.type
    44                         count: 1
    45                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    46                 -
    47                         message: '#^Parameter \#1 \$posts of function update_post_parent_caches expects array\<WP_Post\>, array\<int\|WP_Post\>\|null given\.$#'
    48                         identifier: argument.type
    49                         count: 1
    50                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    51                 -
    52                         message: '#^Parameter \#1 \$required of method WP_Abilities_Content\:\:allow_password_content\(\) expects bool, mixed given\.$#'
    53                         identifier: argument.type
    54                         count: 1
    55                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    56                 -
    57                         message: '#^Parameter \#2 \$post of method WP_Abilities_Content\:\:allow_password_content\(\) expects WP_Post\|null, mixed given\.$#'
    58                         identifier: argument.type
    59                         count: 1
    60                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    61                 -
    62                         message: '#^Parameter \#1 \$input of function array_keys expects array, mixed given\.$#'
    63                         identifier: argument.type
    64                         count: 1
    65                         path: ../../../src/wp-includes/abilities/class-wp-abilities-users.php
    66                 -
    67                         message: '#^Parameter \#1 \$maybe_bool of function rest_is_boolean expects bool\|string, mixed given\.$#'
    68                         identifier: argument.type
    69                         count: 1
    70                         path: ../../../src/wp-includes/abilities/class-wp-abilities-users.php
    71                 -
    72                         message: '#^Parameter \#1 \$value of function rest_sanitize_boolean expects bool\|int\|string, mixed given\.$#'
    73                         identifier: argument.type
    74                         count: 1
    75                         path: ../../../src/wp-includes/abilities/class-wp-abilities-users.php
    76                 -
    7732                        message: '#^Parameter \#1 \$name of method WordPress\\AiClientDependencies\\Psr\\Http\\Message\\MessageInterface\:\:withHeader\(\) expects string, mixed given\.$#'
    7833                        identifier: argument.type
  • trunk/tests/phpstan/baselines-strict/cast.double.neon

    r65861 r65864  
    1 # PHPStan baseline for the `cast.double` errors in WordPress core.
    2 #
    3 # https://phpstan.org/error-identifiers/cast.double
    4 #
    5 # Each entry is scoped to a single file and carries an exact occurrence count,
    6 # so that a new instance is reported as a new error rather than being absorbed
    7 # silently. Fixing an occurrence therefore means decrementing or removing its
    8 # entry here as part of the same change.
    9 #
    10 # The goal is to empty this file and delete it.
    11 #
    12 # Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with:
    13 #
    14 #     composer phpstan:baselines -- --identifier=cast.double --config=phpstan-strict.neon.dist
    15 #
    16 # This reruns the analysis with the baselines in this directory suppressed, so
    17 # that their errors surface again, and rewrites only this file.
    18 
    19 parameters:
    20         ignoreErrors:
    21                 -
    22                         message: '#^Cannot cast mixed to float\.$#'
    23                         identifier: cast.double
    24                         count: 1
    25                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    26                 -
    27                         message: '#^Cannot cast mixed to float\.$#'
    28                         identifier: cast.double
    29                         count: 1
    30                         path: ../../../src/wp-includes/abilities/class-wp-abilities-users.php
  • trunk/tests/phpstan/baselines-strict/cast.int.neon

    r65861 r65864  
    2323                        identifier: cast.int
    2424                        count: 1
    25                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    26                 -
    27                         message: '#^Cannot cast mixed to int\.$#'
    28                         identifier: cast.int
    29                         count: 1
    3025                        path: ../../../src/wp-includes/class-wp-post.php
  • trunk/tests/phpstan/baselines-strict/cast.string.neon

    r65861 r65864  
    1 # PHPStan baseline for the `cast.string` errors in WordPress core.
    2 #
    3 # https://phpstan.org/error-identifiers/cast.string
    4 #
    5 # Each entry is scoped to a single file and carries an exact occurrence count,
    6 # so that a new instance is reported as a new error rather than being absorbed
    7 # silently. Fixing an occurrence therefore means decrementing or removing its
    8 # entry here as part of the same change.
    9 #
    10 # The goal is to empty this file and delete it.
    11 #
    12 # Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with:
    13 #
    14 #     composer phpstan:baselines -- --identifier=cast.string --config=phpstan-strict.neon.dist
    15 #
    16 # This reruns the analysis with the baselines in this directory suppressed, so
    17 # that their errors surface again, and rewrites only this file.
    18 
    19 parameters:
    20         ignoreErrors:
    21                 -
    22                         message: '#^Cannot cast mixed to string\.$#'
    23                         identifier: cast.string
    24                         count: 1
    25                         path: ../../../src/wp-includes/abilities/class-wp-abilities-settings.php
  • trunk/tests/phpstan/baselines-strict/foreach.nonIterable.neon

    r65861 r65864  
    1 # PHPStan baseline for the `foreach.nonIterable` errors in WordPress core.
    2 #
    3 # https://phpstan.org/error-identifiers/foreach.nonIterable
    4 #
    5 # Each entry is scoped to a single file and carries an exact occurrence count,
    6 # so that a new instance is reported as a new error rather than being absorbed
    7 # silently. Fixing an occurrence therefore means decrementing or removing its
    8 # entry here as part of the same change.
    9 #
    10 # The goal is to empty this file and delete it.
    11 #
    12 # Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with:
    13 #
    14 #     composer phpstan:baselines -- --identifier=foreach.nonIterable --config=phpstan-strict.neon.dist
    15 #
    16 # This reruns the analysis with the baselines in this directory suppressed, so
    17 # that their errors surface again, and rewrites only this file.
    18 
    19 parameters:
    20         ignoreErrors:
    21                 -
    22                         message: '#^Argument of an invalid type array\<int\|WP_Post\>\|null supplied for foreach, only iterables are supported\.$#'
    23                         identifier: foreach.nonIterable
    24                         count: 1
    25                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
  • trunk/tests/phpstan/baselines-strict/offsetAccess.nonOffsetAccessible.neon

    r65861 r65864  
    3434                        count: 1
    3535                        path: ../../../src/wp-includes/abilities-api/class-wp-ability.php
    36                 -
    37                         message: '#^Cannot access offset ''properties'' on mixed\.$#'
    38                         identifier: offsetAccess.nonOffsetAccessible
    39                         count: 1
    40                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    41                 -
    42                         message: '#^Cannot access offset ''the_post'' on mixed\.$#'
    43                         identifier: offsetAccess.nonOffsetAccessible
    44                         count: 1
    45                         path: ../../../src/wp-includes/abilities/class-wp-abilities-content.php
    46                 -
    47                         message: '#^Cannot access offset ''properties'' on mixed\.$#'
    48                         identifier: offsetAccess.nonOffsetAccessible
    49                         count: 1
    50                         path: ../../../src/wp-includes/abilities/class-wp-abilities-users.php
  • trunk/tests/phpstan/baselines-strict/return.type.neon

    r65861 r65864  
    2424                        count: 1
    2525                        path: ../../../src/wp-includes/abilities-api/class-wp-ability-categories-registry.php
    26                 -
    27                         message: '#^Method WP_Abilities_Settings\:\:get_exposure_args\(\) should return array\<string, mixed\> but returns array\<mixed, mixed\>\.$#'
    28                         identifier: return.type
    29                         count: 1
    30                         path: ../../../src/wp-includes/abilities/class-wp-abilities-settings.php
    31                 -
    32                         message: '#^Method WP_Abilities_Settings\:\:get_exposure_args\(\) should return array\<string, mixed\> but returns array\<mixed\>\.$#'
    33                         identifier: return.type
    34                         count: 1
    35                         path: ../../../src/wp-includes/abilities/class-wp-abilities-settings.php
    36                 -
    37                         message: '#^Method WP_Abilities_Settings\:\:get_value_schema\(\) should return array\<string, mixed\> but returns array\.$#'
    38                         identifier: return.type
    39                         count: 1
    40                         path: ../../../src/wp-includes/abilities/class-wp-abilities-settings.php
Note: See TracChangeset for help on using the changeset viewer.