From a678aae79fe7edcae2c30e85b71567eee5f29a19 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sat, 29 Aug 2026 08:07:13 +0000 Subject: [PATCH] Fix PHPStan errors with WordPress 7.1 stubs php-stubs/wordpress-stubs v7.1.0 tightened the types of get_sites(), get_terms(), wp_insert_term(), wp_delete_term(), WP_Query::$posts and WP_Comment_Query::query(), and it now covers the Icons API. --- phpstan.neon.dist | 20 ++------------------ src/Comment_Command.php | 4 ++-- src/Post_Command.php | 4 ++-- src/Site_Command.php | 10 ++++++++++ src/Term_Command.php | 17 ++++++----------- 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index c1aa55f5b..d7b4608d1 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -49,13 +49,13 @@ parameters: # `wp icon` and `wp icon collection` abort on WordPress < 7.1 via `before_invoke`. - identifier: WPCompat.methodNotAvailable - message: '#^WP_Icon_Collections_Registry::get_instance\(\) is only available since WordPress version 7\.1\.0\.$#' + message: '#^WP_Icon_Collections_Registry::(get_all_registered|get_instance|get_registered|is_registered)\(\) is only available since WordPress version 7\.1\.0\.$#' paths: - src/Icon_Collection_Command.php - src/Icon_Command.php - identifier: WPCompat.methodNotAvailable - message: '#^WP_Icons_Registry::get_instance\(\) is only available since WordPress version 7\.0\.0\.$#' + message: '#^WP_Icons_Registry::(get_instance|get_registered_icon|get_registered_icons|is_registered)\(\) is only available since WordPress version 7\.0\.0\.$#' paths: - src/Icon_Collection_Command.php - src/Icon_Command.php @@ -129,19 +129,3 @@ parameters: - identifier: WPCompat.parameterNotAvailable.wploadalloptions.forcecache path: src/Option_Command.php - - # The Icons API introduced in WordPress 7.0 and 7.1 is not covered by the WordPress - # stubs yet, so PHPStan does not know these symbols at all. `reportUnmatched` keeps - # the entries from turning into errors themselves once the stubs catch up. - - - identifier: class.notFound - message: '#^Call to static method get_instance\(\) on an unknown class WP_(Icons_Registry|Icon_Collections_Registry)\.$#' - reportUnmatched: false - paths: - - src/Icon_Collection_Command.php - - src/Icon_Command.php - - - identifier: function.notFound - message: '#^Function wp_get_icon not found\.$#' - reportUnmatched: false - path: src/Icon_Command.php diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 384af1fe9..2bb32d2b8 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -525,13 +525,13 @@ public function list_( $args, $assoc_args ) { if ( 'count' === $formatter->format ) { /** - * @var int $comments + * @var int<0, max> $comments */ echo $comments; return; } else { /** - * @var array $comments + * @var array|\WP_Comment> $comments */ if ( 'ids' === $formatter->format ) { diff --git a/src/Post_Command.php b/src/Post_Command.php index 5f068bc54..41f3e1216 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -943,7 +943,7 @@ public function list_( $args, $assoc_args ) { } elseif ( 'count' === $formatter->format ) { $query_args['fields'] = 'ids'; $query = new WP_Query( $query_args ); - $formatter->display_items( $query->posts ); + $formatter->display_items( $query->posts ?? [] ); } else { $query = new WP_Query( $query_args ); $posts = array_map( @@ -956,7 +956,7 @@ function ( $post ) { $post->url = get_permalink( $post->ID ); return $post; }, - $query->posts + $query->posts ?? [] ); $formatter->display_items( $posts ); } diff --git a/src/Site_Command.php b/src/Site_Command.php index c8b959d4e..7c69b4646 100644 --- a/src/Site_Command.php +++ b/src/Site_Command.php @@ -1366,6 +1366,16 @@ function ( $site ) { * @return \Generator */ private static function get_sites_iterator( $query_args ) { + // 'count' and 'fields' are pinned to the WP_Site_Query defaults, so that + // get_sites() always answers with the WP_Site objects this yields. + $query_args = array_merge( + $query_args, + [ + 'count' => false, + 'fields' => '', + ] + ); + if ( isset( $query_args['number'] ) ) { // The arguments are whatever the user passed, so they cannot be narrowed // to the shape get_sites() documents. WP_Site_Query validates them itself. diff --git a/src/Term_Command.php b/src/Term_Command.php index 7559df1b2..7958cbda2 100644 --- a/src/Term_Command.php +++ b/src/Term_Command.php @@ -150,6 +150,7 @@ public function list_( $args, $assoc_args ) { $assoc_args, [ 'taxonomy' => $args, + 'fields' => 'all', ] ) ); @@ -158,10 +159,6 @@ public function list_( $args, $assoc_args ) { if ( is_wp_error( $terms ) ) { WP_CLI::error( $terms ); } - - /** - * @var \WP_Term[] $terms - */ } $terms = array_map( @@ -603,7 +600,7 @@ public function generate( $args, $assoc_args ) { WP_CLI::warning( $term ); } else { $created[] = $term['term_id']; - $previous_term_id = $term['term_id']; + $previous_term_id = absint( $term['term_id'] ); if ( 'ids' === $format ) { echo $term['term_id']; if ( $index < $max_id + $count ) { @@ -827,9 +824,7 @@ public function migrate( $args, $assoc_args ) { WP_CLI::error( "Taxonomy term '{$term_reference}' for taxonomy '{$original_taxonomy}' doesn't exist." ); } - $tax = get_taxonomy( $original_taxonomy ); - - if ( ! $tax ) { + if ( ! taxonomy_exists( $original_taxonomy ) ) { WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." ); } @@ -856,7 +851,7 @@ public function migrate( $args, $assoc_args ) { /** * @var string[] $post_ids */ - $post_ids = get_objects_in_term( $term->term_id, $tax->name ); + $post_ids = get_objects_in_term( $term->term_id, $original_taxonomy ); $post_count = 0; foreach ( $post_ids as $post_id ) { @@ -881,7 +876,7 @@ public function migrate( $args, $assoc_args ) { WP_CLI::log( "Term '{$term->slug}' migrated." ); - $del = wp_delete_term( $term->term_id, $tax->name ); + $del = wp_delete_term( $term->term_id, $original_taxonomy ); if ( is_wp_error( $del ) ) { WP_CLI::error( "Failed to delete the term '{$term->slug}'. Reason: " . $del->get_error_message() ); @@ -889,7 +884,7 @@ public function migrate( $args, $assoc_args ) { WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." ); $post_plural = Utils\pluralize( 'post', $post_count ); - WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." ); + WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$original_taxonomy}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." ); } private function maybe_make_child() {