Make WordPress Core

Changeset 63351


Ignore:
Timestamp:
08/26/2026 09:44:34 PM (11 hours ago)
Author:
westonruter
Message:

Code Quality: Resolve the isset.variable PHPStan errors.

Each of the six baselined errors was an isset() on a variable that is always defined and not nullable, or never defined at all. The $_POST superglobal is always set, so the checks on it in Custom_Image_Header::step_2() and get_media_item() were unconditional; the latter becomes ! empty( $_POST ), which is what isset() plus count() already meant. PHP_VERSION_ID is constant within a request, so the guard above the $loader check in WP_oEmbed::_parse_xml() already decides it. The namespace group in WP_Block_Parser::next_token() is a non-trailing optional group under PREG_OFFSET_CAPTURE, which PHP always populates. In wp_edit_theme_plugin_file() the only assignment to $stylesheet is guarded by a ! empty() on the argument it comes from, so a truthiness test cannot diverge from isset().

The remaining error is the reverse case: $s in load_template() is undefined to static analysis because it arrives through extract(). Assigning the query vars to a local first, and annotating that local as array{ s?: scalar, ... }, resolves both that report and the inline @phpstan-ignore above it. The annotation is deliberately local rather than on WP_Query::$query_vars, where an unsealed shape makes every unnamed key "might not exist" and reports more than it resolves. scalar rather than string is what WP_Query::parse_query() actually guarantees, since it gates the value only with is_scalar().

With the last entry fixed, the baseline file and its entry in the includes of phpstan.neon.dist are removed.

Developed in https://github.com/WordPress/wordpress-develop/pull/13023.
Follow-up to r28407, r32298, r41721, r48789, r60351, r61504, r61699, r63019.

Props westonruter, apermo.
See #65817.

Location:
trunk
Files:
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpstan.neon.dist

    r63344 r63351  
    5656        - tests/phpstan/baselines/isset.offset.neon
    5757        - tests/phpstan/baselines/isset.property.neon
    58         - tests/phpstan/baselines/isset.variable.neon
    5958        - tests/phpstan/baselines/method.childParameterType.neon
    6059        - tests/phpstan/baselines/method.nonObject.neon
  • trunk/src/wp-admin/includes/class-custom-image-header.php

    r62704 r63351  
    842842                        $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
    843843                        $url           = $url[0];
    844                 } elseif ( isset( $_POST ) ) {
     844                } else {
    845845                        $data          = $this->step_2_manage_upload();
    846846                        $attachment_id = $data['attachment_id'];
  • trunk/src/wp-admin/includes/file.php

    r62894 r63351  
    398398        $content = $args['newcontent'];
    399399
    400         $plugin    = null;
    401         $theme     = null;
    402         $real_file = null;
     400        $plugin     = null;
     401        $stylesheet = null;
     402        $theme      = null;
     403        $real_file  = null;
    403404
    404405        if ( ! empty( $args['plugin'] ) ) {
     
    561562                if ( $plugin ) {
    562563                        $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
    563                 } elseif ( isset( $stylesheet ) ) {
     564                } elseif ( $stylesheet ) {
    564565                        $url = add_query_arg(
    565566                                array(
  • trunk/src/wp-admin/includes/media.php

    r63338 r63351  
    17741774        if ( isset( $_GET['post_id'] ) ) {
    17751775                $calling_post_id = absint( $_GET['post_id'] );
    1776         } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set.
     1776        } elseif ( ! empty( $_POST ) ) { // Like for async-upload where $_GET['post_id'] isn't set.
    17771777                $calling_post_id = $post->post_parent;
    17781778        }
  • trunk/src/wp-includes/class-wp-block-parser.php

    r62956 r63351  
    269269                $is_void   = isset( $matches['void'] ) && -1 !== $matches['void'][1];
    270270                $namespace = $matches['namespace'];
    271                 $namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/';
     271                $namespace = ( -1 !== $namespace[1] ) ? $namespace[0] : 'core/';
    272272                $name      = $namespace . $matches['name'][0];
    273273                $has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1];
  • trunk/src/wp-includes/class-wp-oembed.php

    r62501 r63351  
    675675                }
    676676
     677                $loader = null;
    677678                if ( PHP_VERSION_ID < 80000 ) {
    678679                        /*
     
    689690                libxml_use_internal_errors( $errors );
    690691
    691                 if ( PHP_VERSION_ID < 80000 && isset( $loader ) ) {
     692                if ( PHP_VERSION_ID < 80000 ) {
    692693                        // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.libxml_disable_entity_loaderDeprecated
    693694                        libxml_disable_entity_loader( $loader );
  • trunk/src/wp-includes/template.php

    r61699 r63351  
    783783        global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
    784784
    785         if ( is_array( $wp_query->query_vars ) ) {
     785        /** @var array{ s?: scalar, ... } $query_vars */
     786        $query_vars = $wp_query->query_vars;
     787        if ( is_array( $query_vars ) ) {
    786788                /*
    787789                 * This use of extract() cannot be removed. There are many possible ways that
     
    793795                 */
    794796                // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
    795                 extract( $wp_query->query_vars, EXTR_SKIP );
     797                extract( $query_vars, EXTR_SKIP );
    796798        }
    797799
    798800        if ( isset( $s ) ) {
    799                 $s = esc_attr( $s ); // @phpstan-ignore variable.undefined (It's extracted from query vars.)
     801                $s = esc_attr( (string) $s );
    800802        }
    801803
Note: See TracChangeset for help on using the changeset viewer.