diff --git a/src/wp-includes/category.php b/src/wp-includes/category.php index 79f05c6cfca60..fbbc169dc6235 100644 --- a/src/wp-includes/category.php +++ b/src/wp-includes/category.php @@ -291,6 +291,8 @@ function cat_is_ancestor_of( $cat1, $cat2 ) { * @param object|array $category Category data. * @param string $context Optional. Default 'display'. * @return object|array Same type as $category with sanitized data for safe use. + * + * @phpstan-return ($category is WP_Term ? WP_Term : ($category is array ? array : object)) */ function sanitize_category( $category, $context = 'display' ) { return sanitize_term( $category, 'category', $context ); diff --git a/src/wp-includes/class-wp-widget-factory.php b/src/wp-includes/class-wp-widget-factory.php index 1f6ed3a58919e..f7009788549e4 100644 --- a/src/wp-includes/class-wp-widget-factory.php +++ b/src/wp-includes/class-wp-widget-factory.php @@ -69,11 +69,14 @@ public function WP_Widget_Factory() { * @since 7.1.1 The key for an instance is prefixed so that it is never cast to an integer. * * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + * + * @phpstan-param class-string|WP_Widget $widget */ public function register( $widget ) { if ( $widget instanceof WP_Widget ) { $this->widgets[ self::INSTANCE_KEY_PREFIX . spl_object_id( $widget ) ] = $widget; } else { + // @phpstan-ignore arguments.count (Widget classes declare their own constructor, which takes no arguments.) $this->widgets[ $widget ] = new $widget(); } } @@ -87,6 +90,8 @@ public function register( $widget ) { * @since 7.1.1 The key for an instance is prefixed so that it is never cast to an integer. * * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + * + * @phpstan-param class-string|WP_Widget $widget */ public function unregister( $widget ) { if ( $widget instanceof WP_Widget ) { diff --git a/src/wp-includes/class-wp-widget.php b/src/wp-includes/class-wp-widget.php index f1d432c0d3fb5..f7cd8f2b84711 100644 --- a/src/wp-includes/class-wp-widget.php +++ b/src/wp-includes/class-wp-widget.php @@ -16,6 +16,8 @@ * * @since 2.8.0 * @since 4.4.0 Moved to its own file from wp-includes/widgets.php + * + * @phpstan-template T of array = array */ #[AllowDynamicProperties] class WP_Widget { @@ -109,6 +111,8 @@ class WP_Widget { * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance The settings for the particular instance of the widget. + * + * @phpstan-param T $instance */ public function widget( $args, $instance ) { die( 'function WP_Widget::widget() must be overridden in a subclass.' ); @@ -127,6 +131,10 @@ public function widget( $args, $instance ) { * WP_Widget::form(). * @param array $old_instance Old settings for this instance. * @return array Settings to save or bool false to cancel saving. + * + * @phpstan-param array $new_instance + * @phpstan-param T|array{} $old_instance + * @phpstan-return array|false */ public function update( $new_instance, $old_instance ) { return $new_instance; @@ -140,6 +148,8 @@ public function update( $new_instance, $old_instance ) { * @param array $instance The settings for the particular instance of the widget. * @return string|void Default return is 'noform'. A subclass which echoes its own * form returns nothing. + * + * @phpstan-param T|array{} $instance */ public function form( $instance ) { echo '

' . __( 'There are no options for this widget.' ) . '

'; diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index ecae45083a6ca..8cb0413150ed9 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -2942,6 +2942,8 @@ function stripslashes_from_strings_only( $value ) { * * @param mixed $value The array or string to be encoded. * @return mixed The encoded value. + * + * @phpstan-return ($value is array ? array : ($value is object ? object : string)) */ function urlencode_deep( $value ) { return map_deep( $value, 'urlencode' ); @@ -2954,6 +2956,8 @@ function urlencode_deep( $value ) { * * @param mixed $value The array or string to be encoded. * @return mixed The encoded value. + * + * @phpstan-return ($value is array ? array : ($value is object ? object : string)) */ function rawurlencode_deep( $value ) { return map_deep( $value, 'rawurlencode' ); @@ -2966,6 +2970,8 @@ function rawurlencode_deep( $value ) { * * @param mixed $value The array or string to be decoded. * @return mixed The decoded value. + * + * @phpstan-return ($value is array ? array : ($value is object ? object : string)) */ function urldecode_deep( $value ) { return map_deep( $value, 'urldecode' ); diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index 31fce41b65954..db6a99077bd3c 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -116,6 +116,8 @@ * @global WP_Widget_Factory $wp_widget_factory * * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + * + * @phpstan-param class-string|WP_Widget $widget */ function register_widget( $widget ) { global $wp_widget_factory; @@ -138,6 +140,8 @@ function register_widget( $widget ) { * @global WP_Widget_Factory $wp_widget_factory * * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + * + * @phpstan-param class-string|WP_Widget $widget */ function unregister_widget( $widget ) { global $wp_widget_factory; diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index aece929f18751..54689f00d7d28 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -749,7 +749,7 @@ parameters: count: 1 path: ../../../src/wp-includes/class-wp-user.php - - message: '#^Parameter \#1 \$number of method WP_Widget\:\:_set\(\) expects int, string given\.$#' + message: '#^Parameter \#1 \$number of method WP_Widget\\>\:\:_set\(\) expects int, string given\.$#' identifier: argument.type count: 1 path: ../../../src/wp-includes/class-wp-widget.php diff --git a/tests/phpstan/baselines/isset.property.neon b/tests/phpstan/baselines/isset.property.neon index 5278005df2869..1ae9e8ebbf188 100644 --- a/tests/phpstan/baselines/isset.property.neon +++ b/tests/phpstan/baselines/isset.property.neon @@ -129,7 +129,7 @@ parameters: count: 1 path: ../../../src/wp-includes/class-wp-user.php - - message: '#^Property WP_Widget\:\:\$alt_option_name \(string\) in isset\(\) is not nullable\.$#' + message: '#^Property WP_Widget\\>\:\:\$alt_option_name \(string\) in isset\(\) is not nullable\.$#' identifier: isset.property count: 1 path: ../../../src/wp-includes/class-wp-widget.php