From 60228ecf5ca66f9a59f2965271d3cf0322fa08f3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Mar 2026 11:15:47 +0100 Subject: [PATCH 1/2] PHPStan level 9 --- phpstan.neon.dist | 13 ++++++++++++ src/Sidebar_Command.php | 3 +++ src/Widget_Command.php | 44 ++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..3de37c76 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,13 @@ +parameters: + level: 9 + paths: + - src + - widget-command.php + scanDirectories: + - vendor/wp-cli/wp-cli/php + scanFiles: + - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + treatPhpDocTypesAsCertain: false + ignoreErrors: + - identifier: missingType.parameter + - identifier: missingType.return diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index aa8439d4..e5946507 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -18,6 +18,9 @@ */ class Sidebar_Command extends WP_CLI_Command { + /** + * @var string[] + */ private $fields = [ 'name', 'id', diff --git a/src/Widget_Command.php b/src/Widget_Command.php index 4eed6ebd..2226b733 100644 --- a/src/Widget_Command.php +++ b/src/Widget_Command.php @@ -34,6 +34,9 @@ */ class Widget_Command extends WP_CLI_Command { + /** + * @var string[] + */ private $fields = [ 'name', 'id', @@ -156,7 +159,7 @@ public function add( $args, $assoc_args ) { } unset( $option_keys['_multiwidget'] ); $option_keys = array_keys( $option_keys ); - $last_key = array_pop( $option_keys ); + $last_key = (int) array_pop( $option_keys ); $option_index = $last_key + 1; $widget_options[ $option_index ] = $this->sanitize_widget_options( $name, $assoc_args, array() ); $this->update_widget_options( $name, $widget_options ); @@ -624,7 +627,7 @@ private function validate_sidebar_widget( $widget_id ) { * Gets the widgets (and their associated data) for a given sidebar * * @param string $sidebar_id - * @return array + * @return list}&\stdClass> */ private function get_sidebar_widgets( $sidebar_id ) { @@ -640,24 +643,33 @@ private function get_sidebar_widgets( $sidebar_id ) { $prepared_widget = new stdClass(); $parts = explode( '-', $widget_id ); - $option_index = array_pop( $parts ); + $option_index = (string) array_pop( $parts ); $widget_name = implode( '-', $parts ); $prepared_widget->name = $widget_name; $prepared_widget->id = $widget_id; $prepared_widget->position = $key + 1; $widget_options = get_option( 'widget_' . $widget_name ); - $prepared_widget->options = $widget_options[ $option_index ]; + /** + * @var array $widget_options + */ + $prepared_widget->options = $widget_options[ $option_index ]; $prepared_widgets[] = $prepared_widget; } + /** + * @phpstan-var list}&\stdClass> $prepared_widgets + */ + return $prepared_widgets; } /** * Re-implementation of wp_get_sidebars_widgets() - * because the original has a nasty global component + * because the original has a nasty global component. + * + * @return array> */ private function wp_get_sidebars_widgets() { $sidebars_widgets = get_option( 'sidebars_widgets', array() ); @@ -666,6 +678,10 @@ private function wp_get_sidebars_widgets() { unset( $sidebars_widgets['array_version'] ); } + /** + * @var array> $sidebars_widgets + */ + return $sidebars_widgets; } @@ -673,7 +689,7 @@ private function wp_get_sidebars_widgets() { * Gets the widget's name, option index, sidebar, and sidebar index from its ID * * @param string $widget_id - * @return array + * @return array{0: string, 1: string, 2: string, 3: int} */ private function get_widget_data( $widget_id ) { @@ -694,24 +710,28 @@ private function get_widget_data( $widget_id ) { } } - return array( $name, $option_index, $sidebar_id, $sidebar_index ); + return array( $name, $option_index, (string) $sidebar_id, (int) $sidebar_index ); } /** * Gets the options for a given widget * * @param string $name - * @return array + * @return array */ private function get_widget_options( $name ) { - return get_option( 'widget_' . $name, array() ); + $options = get_option( 'widget_' . $name, array() ); + /** + * @var array $options + */ + return $options; } /** * Updates the options for a given widget * * @param string $name - * @param mixed + * @param mixed $value */ private function update_widget_options( $name, $value ) { update_option( 'widget_' . $name, $value ); @@ -785,7 +805,7 @@ private function get_widget_obj( $id_base ) { * @param string $id_base Name of the widget * @param mixed $dirty_options * @param mixed $old_options - * @return mixed + * @return array */ private function sanitize_widget_options( $id_base, $dirty_options, $old_options ) { @@ -797,6 +817,6 @@ private function sanitize_widget_options( $id_base, $dirty_options, $old_options // No easy way to determine expected array keys for $dirty_options // because Widget API dependent on the form fields // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Whitelisting due to above reason. - return @$widget->update( $dirty_options, $old_options ); + return @$widget->update( $dirty_options, $old_options ); // @phpstan-ignore argument.type, argument.type } } From 13e82f602ece7cc9d48434ddd22a8bc942774d78 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Mar 2026 11:29:03 +0100 Subject: [PATCH 2/2] Set default --- src/Widget_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Widget_Command.php b/src/Widget_Command.php index 2226b733..c92b9c43 100644 --- a/src/Widget_Command.php +++ b/src/Widget_Command.php @@ -649,7 +649,7 @@ private function get_sidebar_widgets( $sidebar_id ) { $prepared_widget->name = $widget_name; $prepared_widget->id = $widget_id; $prepared_widget->position = $key + 1; - $widget_options = get_option( 'widget_' . $widget_name ); + $widget_options = get_option( 'widget_' . $widget_name, [] ); /** * @var array $widget_options */