From 4cba94b331dda21dda095b8d5ceba30b9c43e69b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 14 Dec 2025 15:28:17 -0800 Subject: [PATCH 01/11] Implement PHPStan level 8 compliance for functions.wp-styles.php, functions.wp-scripts.php, and script-loader.php --- src/wp-includes/functions.wp-scripts.php | 46 ++++----- src/wp-includes/functions.wp-styles.php | 4 +- src/wp-includes/script-loader.php | 125 +++++++++++++---------- src/wp-includes/version.php | 8 ++ 4 files changed, 102 insertions(+), 81 deletions(-) diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index f86b456d5f69a..f1a9b2afd6b7c 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -141,7 +141,7 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { ), '4.5.0' ); - $data = trim( preg_replace( '#]*>(.*)#is', '$1', $data ) ); + $data = trim( (string) preg_replace( '#]*>(.*)#is', '$1', $data ) ); } return wp_scripts()->add_inline_script( $handle, $data, $position ); @@ -160,15 +160,15 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) { * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array. * - * @param string $handle Name of the script. Should be unique. - * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. - * If source is set to false, script is an alias of other scripts it depends on. - * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. - * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL - * as a query string for cache busting purposes. If version is set to false, a version - * number is automatically added equal to current installed WordPress version. - * If set to null, no version is added. - * @param array|bool $args { + * @param string $handle Name of the script. Should be unique. + * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. + * If source is set to false, script is an alias of other scripts it depends on. + * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL + * as a query string for cache busting purposes. If version is set to false, a version + * number is automatically added equal to current installed WordPress version. + * If set to null, no version is added. + * @param array|bool $args { * Optional. An array of additional script loading strategies. Default empty array. * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. * @@ -221,10 +221,10 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args * * @todo Documentation cleanup * - * @param string $handle Script handle the data will be attached to. - * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. - * Example: '/[a-zA-Z0-9_]+/'. - * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. + * @param string $handle Script handle the data will be attached to. + * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. + * Example: '/[a-zA-Z0-9_]+/'. + * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. * @return bool True if the script was successfully localized, false otherwise. */ function wp_localize_script( $handle, $object_name, $l10n ) { @@ -346,15 +346,15 @@ function wp_deregister_script( $handle ) { * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. * @since 6.9.0 The $fetchpriority parameter of type string was added to the $args parameter of type array. * - * @param string $handle Name of the script. Should be unique. - * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. - * Default empty. - * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. - * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL - * as a query string for cache busting purposes. If version is set to false, a version - * number is automatically added equal to current installed WordPress version. - * If set to null, no version is added. - * @param array|bool $args { + * @param string $handle Name of the script. Should be unique. + * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. + * Default empty. + * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL + * as a query string for cache busting purposes. If version is set to false, a version + * number is automatically added equal to current installed WordPress version. + * If set to null, no version is added. + * @param array|bool $args { * Optional. An array of additional script loading strategies. Default empty array. * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. * diff --git a/src/wp-includes/functions.wp-styles.php b/src/wp-includes/functions.wp-styles.php index f84b931866818..903097c313a4a 100644 --- a/src/wp-includes/functions.wp-styles.php +++ b/src/wp-includes/functions.wp-styles.php @@ -38,7 +38,7 @@ function wp_styles() { * * @global WP_Styles $wp_styles The WP_Styles object for printing styles. * - * @param string|bool|array $handles Styles to be printed. Default 'false'. + * @param string|false|string[] $handles Styles to be printed. Default 'false'. * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. */ function wp_print_styles( $handles = false ) { @@ -98,7 +98,7 @@ function wp_add_inline_style( $handle, $data ) { ), '3.7.0' ); - $data = trim( preg_replace( '#]*>(.*)#is', '$1', $data ) ); + $data = trim( (string) preg_replace( '#]*>(.*)#is', '$1', $data ) ); } return wp_styles()->add_inline_style( $handle, $data ); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 7dccff9775731..4924b86cb1f9c 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -88,21 +88,21 @@ function wp_default_packages_vendor( $scripts ) { $suffix = wp_scripts_get_suffix(); $vendor_scripts = array( - 'react', - 'react-dom' => array( 'react' ), - 'react-jsx-runtime' => array( 'react' ), - 'regenerator-runtime', - 'moment', - 'lodash', - 'wp-polyfill-fetch', - 'wp-polyfill-formdata', - 'wp-polyfill-node-contains', - 'wp-polyfill-url', - 'wp-polyfill-dom-rect', - 'wp-polyfill-element-closest', - 'wp-polyfill-object-fit', - 'wp-polyfill-inert', - 'wp-polyfill', + 'react' => array(), + 'react-dom' => array( 'react' ), + 'react-jsx-runtime' => array( 'react' ), + 'regenerator-runtime' => array(), + 'moment' => array(), + 'lodash' => array(), + 'wp-polyfill-fetch' => array(), + 'wp-polyfill-formdata' => array(), + 'wp-polyfill-node-contains' => array(), + 'wp-polyfill-url' => array(), + 'wp-polyfill-dom-rect' => array(), + 'wp-polyfill-element-closest' => array(), + 'wp-polyfill-object-fit' => array(), + 'wp-polyfill-inert' => array(), + 'wp-polyfill' => array(), ); $vendor_scripts_versions = array( @@ -124,15 +124,13 @@ function wp_default_packages_vendor( $scripts ) { ); foreach ( $vendor_scripts as $handle => $dependencies ) { - if ( is_string( $dependencies ) ) { - $handle = $dependencies; - $dependencies = array(); - } - - $path = "/wp-includes/js/dist/vendor/$handle$suffix.js"; - $version = $vendor_scripts_versions[ $handle ]; - - $scripts->add( $handle, $path, $dependencies, $version, 1 ); + $scripts->add( + $handle, + "/wp-includes/js/dist/vendor/$handle$suffix.js", + $dependencies, + $vendor_scripts_versions[ $handle ], + 1 + ); } did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' ); @@ -188,7 +186,7 @@ function wp_get_script_polyfill( $scripts, $tests ) { $src = $scripts->registered[ $handle ]->src; $ver = $scripts->registered[ $handle ]->ver; - if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) { + if ( is_string( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) { $src = $scripts->base_url . $src; } @@ -690,14 +688,14 @@ function wp_scripts_get_suffix( $type = '' ) { * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case * wp-includes/functions.php is not loaded. */ - require ABSPATH . WPINC . '/version.php'; + $versions = require ABSPATH . WPINC . '/version.php'; /* * Note: str_contains() is not used here, as this file can be included * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case * the polyfills from wp-includes/compat.php are not loaded. */ - $develop_src = false !== strpos( $wp_version, '-src' ); + $develop_src = false !== strpos( $versions['wp_version'] ?? '', '-src' ); if ( ! defined( 'SCRIPT_DEBUG' ) ) { define( 'SCRIPT_DEBUG', $develop_src ); @@ -1086,7 +1084,7 @@ function wp_default_scripts( $scripts ) { 'var mejsL10n = %s;', wp_json_encode( array( - 'language' => strtolower( strtok( determine_locale(), '_-' ) ), + 'language' => strtolower( (string) strtok( determine_locale(), '_-' ) ), 'strings' => array( 'mejs.download-file' => __( 'Download File' ), 'mejs.install-flash' => __( 'You are using a browser that does not have Flash player enabled or installed. Please turn on your Flash player plugin or download the latest version from https://get.adobe.com/flashplayer/' ), @@ -1553,7 +1551,7 @@ function wp_default_styles( $styles ) { * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case * wp-includes/functions.php is not loaded. */ - require ABSPATH . WPINC . '/version.php'; + $versions = require ABSPATH . WPINC . '/version.php'; if ( ! defined( 'SCRIPT_DEBUG' ) ) { /* @@ -1561,7 +1559,7 @@ function wp_default_styles( $styles ) { * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case * the polyfills from wp-includes/compat.php are not loaded. */ - define( 'SCRIPT_DEBUG', false !== strpos( $wp_version, '-src' ) ); + define( 'SCRIPT_DEBUG', false !== strpos( $versions['wp_version'] ?? '', '-src' ) ); } $guessurl = site_url(); @@ -1605,7 +1603,7 @@ function wp_default_styles( $styles ) { } // Register a stylesheet for the selected admin color scheme. - $styles->add( 'colors', true, array( 'wp-admin', 'buttons' ) ); + $styles->add( 'colors', false, array( 'wp-admin', 'buttons' ) ); $suffix = SCRIPT_DEBUG ? '' : '.min'; @@ -1904,7 +1902,7 @@ function wp_prototype_before_jquery( $js_array ) { unset( $js_array[ $prototype ] ); - array_splice( $js_array, $jquery, 0, 'prototype' ); + array_splice( $js_array, (int) $jquery, 0, 'prototype' ); return $js_array; } @@ -2080,7 +2078,7 @@ function wp_style_loader_src( $src, $handle ) { global $_wp_admin_css_colors; if ( wp_installing() ) { - return preg_replace( '#^wp-admin/#', './', $src ); + return (string) preg_replace( '#^wp-admin/#', './', $src ); } if ( 'colors' === $handle ) { @@ -2352,7 +2350,7 @@ function print_admin_styles() { * @global WP_Styles $wp_styles * @global bool $concatenate_scripts * - * @return array|void + * @return string[]|void */ function print_late_styles() { global $wp_styles, $concatenate_scripts; @@ -2508,8 +2506,8 @@ function wp_common_block_scripts_and_styles() { * * @since 6.1.0 * - * @param array $nodes The nodes to filter. - * @return array A filtered array of style nodes. + * @param array> $nodes The nodes to filter. + * @return array> A filtered array of style nodes. */ function wp_filter_out_block_nodes( $nodes ) { return array_filter( @@ -2869,7 +2867,7 @@ function wp_enqueue_editor_format_library_assets() { * * @since 5.7.0 * - * @param array $attributes Key-value pairs representing `