Skip to content

Commit 77d6472

Browse files
authored
Fix PHPStan errors with WordPress 7.1 stubs (#650)
1 parent 557cc00 commit 77d6472

5 files changed

Lines changed: 22 additions & 33 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ parameters:
4949
# `wp icon` and `wp icon collection` abort on WordPress < 7.1 via `before_invoke`.
5050
-
5151
identifier: WPCompat.methodNotAvailable
52-
message: '#^WP_Icon_Collections_Registry::get_instance\(\) is only available since WordPress version 7\.1\.0\.$#'
52+
message: '#^WP_Icon_Collections_Registry::(get_all_registered|get_instance|get_registered|is_registered)\(\) is only available since WordPress version 7\.1\.0\.$#'
5353
paths:
5454
- src/Icon_Collection_Command.php
5555
- src/Icon_Command.php
5656
-
5757
identifier: WPCompat.methodNotAvailable
58-
message: '#^WP_Icons_Registry::get_instance\(\) is only available since WordPress version 7\.0\.0\.$#'
58+
message: '#^WP_Icons_Registry::(get_instance|get_registered_icon|get_registered_icons|is_registered)\(\) is only available since WordPress version 7\.0\.0\.$#'
5959
paths:
6060
- src/Icon_Collection_Command.php
6161
- src/Icon_Command.php
@@ -129,19 +129,3 @@ parameters:
129129
-
130130
identifier: WPCompat.parameterNotAvailable.wploadalloptions.forcecache
131131
path: src/Option_Command.php
132-
133-
# The Icons API introduced in WordPress 7.0 and 7.1 is not covered by the WordPress
134-
# stubs yet, so PHPStan does not know these symbols at all. `reportUnmatched` keeps
135-
# the entries from turning into errors themselves once the stubs catch up.
136-
-
137-
identifier: class.notFound
138-
message: '#^Call to static method get_instance\(\) on an unknown class WP_(Icons_Registry|Icon_Collections_Registry)\.$#'
139-
reportUnmatched: false
140-
paths:
141-
- src/Icon_Collection_Command.php
142-
- src/Icon_Command.php
143-
-
144-
identifier: function.notFound
145-
message: '#^Function wp_get_icon not found\.$#'
146-
reportUnmatched: false
147-
path: src/Icon_Command.php

‎src/Comment_Command.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,13 @@ public function list_( $args, $assoc_args ) {
525525

526526
if ( 'count' === $formatter->format ) {
527527
/**
528-
* @var int $comments
528+
* @var int<0, max> $comments
529529
*/
530530
echo $comments;
531531
return;
532532
} else {
533533
/**
534-
* @var array $comments
534+
* @var array<int<0, max>|\WP_Comment> $comments
535535
*/
536536

537537
if ( 'ids' === $formatter->format ) {

‎src/Post_Command.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ public function list_( $args, $assoc_args ) {
943943
} elseif ( 'count' === $formatter->format ) {
944944
$query_args['fields'] = 'ids';
945945
$query = new WP_Query( $query_args );
946-
$formatter->display_items( $query->posts );
946+
$formatter->display_items( $query->posts ?? [] );
947947
} else {
948948
$query = new WP_Query( $query_args );
949949
$posts = array_map(
@@ -956,7 +956,7 @@ function ( $post ) {
956956
$post->url = get_permalink( $post->ID );
957957
return $post;
958958
},
959-
$query->posts
959+
$query->posts ?? []
960960
);
961961
$formatter->display_items( $posts );
962962
}

‎src/Site_Command.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,16 @@ function ( $site ) {
13661366
* @return \Generator<int, \WP_Site>
13671367
*/
13681368
private static function get_sites_iterator( $query_args ) {
1369+
// 'count' and 'fields' are pinned to the WP_Site_Query defaults, so that
1370+
// get_sites() always answers with the WP_Site objects this yields.
1371+
$query_args = array_merge(
1372+
$query_args,
1373+
[
1374+
'count' => false,
1375+
'fields' => '',
1376+
]
1377+
);
1378+
13691379
if ( isset( $query_args['number'] ) ) {
13701380
// The arguments are whatever the user passed, so they cannot be narrowed
13711381
// to the shape get_sites() documents. WP_Site_Query validates them itself.

‎src/Term_Command.php‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function list_( $args, $assoc_args ) {
150150
$assoc_args,
151151
[
152152
'taxonomy' => $args,
153+
'fields' => 'all',
153154
]
154155
)
155156
);
@@ -158,10 +159,6 @@ public function list_( $args, $assoc_args ) {
158159
if ( is_wp_error( $terms ) ) {
159160
WP_CLI::error( $terms );
160161
}
161-
162-
/**
163-
* @var \WP_Term[] $terms
164-
*/
165162
}
166163

167164
$terms = array_map(
@@ -603,7 +600,7 @@ public function generate( $args, $assoc_args ) {
603600
WP_CLI::warning( $term );
604601
} else {
605602
$created[] = $term['term_id'];
606-
$previous_term_id = $term['term_id'];
603+
$previous_term_id = absint( $term['term_id'] );
607604
if ( 'ids' === $format ) {
608605
echo $term['term_id'];
609606
if ( $index < $max_id + $count ) {
@@ -827,9 +824,7 @@ public function migrate( $args, $assoc_args ) {
827824
WP_CLI::error( "Taxonomy term '{$term_reference}' for taxonomy '{$original_taxonomy}' doesn't exist." );
828825
}
829826

830-
$tax = get_taxonomy( $original_taxonomy );
831-
832-
if ( ! $tax ) {
827+
if ( ! taxonomy_exists( $original_taxonomy ) ) {
833828
WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." );
834829
}
835830

@@ -856,7 +851,7 @@ public function migrate( $args, $assoc_args ) {
856851
/**
857852
* @var string[] $post_ids
858853
*/
859-
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
854+
$post_ids = get_objects_in_term( $term->term_id, $original_taxonomy );
860855
$post_count = 0;
861856

862857
foreach ( $post_ids as $post_id ) {
@@ -881,15 +876,15 @@ public function migrate( $args, $assoc_args ) {
881876

882877
WP_CLI::log( "Term '{$term->slug}' migrated." );
883878

884-
$del = wp_delete_term( $term->term_id, $tax->name );
879+
$del = wp_delete_term( $term->term_id, $original_taxonomy );
885880

886881
if ( is_wp_error( $del ) ) {
887882
WP_CLI::error( "Failed to delete the term '{$term->slug}'. Reason: " . $del->get_error_message() );
888883
}
889884

890885
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
891886
$post_plural = Utils\pluralize( 'post', $post_count );
892-
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
887+
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$original_taxonomy}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
893888
}
894889

895890
private function maybe_make_child() {

0 commit comments

Comments
 (0)