Skip to content

Commit 8d3751a

Browse files
authored
Merge pull request #536 from wp-cli/add/phpstan
2 parents 56b1ffc + e1fcd7a commit 8d3751a

27 files changed

+451
-278
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"wp-cli/wp-cli": "^2.12"
15+
"wp-cli/wp-cli": "^2.13"
1616
},
1717
"require-dev": {
1818
"wp-cli/cache-command": "^1 || ^2",
@@ -231,6 +231,7 @@
231231
"behat-rerun": "rerun-behat-tests",
232232
"lint": "run-linter-tests",
233233
"phpcs": "run-phpcs-tests",
234+
"phpstan": "run-phpstan-tests",
234235
"phpunit": "run-php-unit-tests",
235236
"phpcbf": "run-phpcbf-cleanup",
236237
"phpstan": "run-phpstan-tests",

entity-command.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,7 @@
6060
);
6161
WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );
6262
WP_CLI::add_command( 'term', 'Term_Command' );
63-
WP_CLI::add_command(
64-
'term meta',
65-
'Term_Meta_Command',
66-
array(
67-
'before_invoke' => function () {
68-
if ( Utils\wp_version_compare( '4.4', '<' ) ) {
69-
WP_CLI::error( 'Requires WordPress 4.4 or greater.' );
70-
}
71-
},
72-
)
73-
);
63+
WP_CLI::add_command( 'term meta', 'Term_Meta_Command' );
7464
WP_CLI::add_command( 'user', 'User_Command' );
7565
WP_CLI::add_command(
7666
'user application-password',
@@ -84,17 +74,7 @@
8474
)
8575
);
8676
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
87-
WP_CLI::add_command(
88-
'user session',
89-
'User_Session_Command',
90-
array(
91-
'before_invoke' => function () {
92-
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
93-
WP_CLI::error( 'Requires WordPress 4.0 or greater.' );
94-
}
95-
},
96-
)
97-
);
77+
WP_CLI::add_command( 'user session', 'User_Session_Command' );
9878
WP_CLI::add_command( 'user term', 'User_Term_Command' );
9979

10080
if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {

features/post.feature

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -451,40 +451,6 @@ Feature: Manage WordPress posts
451451
Test Post
452452
"""
453453

454-
@less-than-wp-4.4
455-
Scenario: Creating/updating posts with meta keys for WP < 4.4 has no effect so should give warning
456-
When I try `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
457-
Then the return code should be 0
458-
And STDOUT should be a number
459-
And save STDOUT as {POST_ID}
460-
And STDERR should be:
461-
"""
462-
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
463-
"""
464-
465-
When I run `wp post meta list {POST_ID} --format=count`
466-
Then STDOUT should be:
467-
"""
468-
0
469-
"""
470-
471-
When I try `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'`
472-
Then the return code should be 0
473-
And STDERR should be:
474-
"""
475-
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
476-
"""
477-
And STDOUT should be:
478-
"""
479-
Success: Updated post {POST_ID}.
480-
"""
481-
482-
When I run `wp post meta list {POST_ID} --format=count`
483-
Then STDOUT should be:
484-
"""
485-
0
486-
"""
487-
488454
Scenario: Publishing a post and setting a date fails if the edit_date flag is not passed.
489455
Given a WP install
490456

phpstan.neon.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- entity-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
scanFiles:
9+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
treatPhpDocTypesAsCertain: false
11+
ignoreErrors:
12+
- identifier: missingType.iterableValue
13+
- identifier: missingType.property
14+
- identifier: missingType.parameter
15+
- identifier: missingType.return

src/Comment_Command.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public function get( $args, $assoc_args ) {
243243
}
244244

245245
if ( ! isset( $comment->url ) ) {
246+
// @phpstan-ignore property.notFound
246247
$comment->url = get_comment_link( $comment );
247248
}
248249

@@ -376,29 +377,27 @@ public function list_( $args, $assoc_args ) {
376377
$assoc_args['count'] = true;
377378
}
378379

379-
if ( ! empty( $assoc_args['comment__in'] )
380-
&& ! empty( $assoc_args['orderby'] )
381-
&& 'comment__in' === $assoc_args['orderby']
382-
&& Utils\wp_version_compare( '4.4', '<' ) ) {
383-
$comments = [];
384-
foreach ( $assoc_args['comment__in'] as $comment_id ) {
385-
$comment = get_comment( $comment_id );
386-
if ( $comment ) {
387-
$comments[] = $comment;
388-
} else {
389-
WP_CLI::warning( "Invalid comment {$comment_id}." );
390-
}
391-
}
392-
} else {
393-
$query = new WP_Comment_Query();
394-
$comments = $query->query( $assoc_args );
395-
}
380+
$query = new WP_Comment_Query();
381+
$comments = $query->query( $assoc_args );
396382

397383
if ( 'count' === $formatter->format ) {
384+
/**
385+
* @var int $comments
386+
*/
398387
echo $comments;
388+
return;
399389
} else {
390+
/**
391+
* @var array $comments
392+
*/
393+
400394
if ( 'ids' === $formatter->format ) {
401-
$comments = wp_list_pluck( $comments, 'comment_ID' );
395+
/**
396+
* @var \WP_Comment[] $comments
397+
*/
398+
$items = wp_list_pluck( $comments, 'comment_ID' );
399+
400+
$comments = $items;
402401
} elseif ( is_array( $comments ) ) {
403402
$comments = array_map(
404403
function ( $comment ) {
@@ -439,7 +438,7 @@ public function delete( $args, $assoc_args ) {
439438
$args,
440439
$assoc_args,
441440
function ( $comment_id, $assoc_args ) {
442-
$force = Utils\get_flag_value( $assoc_args, 'force' );
441+
$force = (bool) Utils\get_flag_value( $assoc_args, 'force' );
443442

444443
$status = wp_get_comment_status( $comment_id );
445444
$result = wp_delete_comment( $comment_id, $force );
@@ -457,6 +456,9 @@ function ( $comment_id, $assoc_args ) {
457456
private function call( $args, $status, $success, $failure ) {
458457
$comment_id = absint( $args );
459458

459+
/**
460+
* @var callable $func
461+
*/
460462
$func = "wp_{$status}_comment";
461463

462464
if ( ! $func( $comment_id ) ) {
@@ -642,16 +644,17 @@ public function unapprove( $args, $assoc_args ) {
642644
* total_comments: 19
643645
*/
644646
public function count( $args, $assoc_args ) {
645-
$post_id = Utils\get_flag_value( $args, 0, 0 );
647+
$post_id = $args[0] ?? null;
646648

647649
$count = wp_count_comments( $post_id );
648650

649651
// Move total_comments to the end of the object
650652
$total = $count->total_comments;
651653
unset( $count->total_comments );
654+
// @phpstan-ignore assign.propertyReadOnly
652655
$count->total_comments = $total;
653656

654-
foreach ( $count as $status => $count ) {
657+
foreach ( (array) $count as $status => $count ) {
655658
WP_CLI::line( str_pad( "$status:", 17 ) . $count );
656659
}
657660
}
@@ -673,6 +676,9 @@ public function count( $args, $assoc_args ) {
673676
public function recount( $args ) {
674677
foreach ( $args as $id ) {
675678
if ( wp_update_comment_count( $id ) ) {
679+
/**
680+
* @var \WP_Post $post
681+
*/
676682
$post = get_post( $id );
677683
WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." );
678684
} else {

src/Comment_Meta_Command.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_va
7676
* specified.
7777
*
7878
* @return mixed Single metadata value, or array of values.
79+
*
80+
* @phpstan-return ($single is true ? string : $meta_key is "" ? array<array<string>> : array<string>)
7981
*/
8082
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
83+
// @phpstan-ignore return.type
8184
return get_comment_meta( $object_id, $meta_key, $single );
8285
}
8386

@@ -104,11 +107,12 @@ protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
104107
/**
105108
* Check that the comment ID exists
106109
*
107-
* @param int
110+
* @param string|int $object_id
111+
* @return int|never
108112
*/
109113
protected function check_object_id( $object_id ) {
110114
$fetcher = new CommentFetcher();
111-
$comment = $fetcher->get_check( $object_id );
112-
return $comment->comment_ID;
115+
$comment = $fetcher->get_check( (string) $object_id );
116+
return (int) $comment->comment_ID;
113117
}
114118
}

src/Menu_Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function create( $args, $assoc_args ) {
7070

7171
} elseif ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
7272

73-
WP_CLI::line( $menu_id );
73+
WP_CLI::line( (string) $menu_id );
7474
} else {
7575
WP_CLI::success( "Created menu {$menu_id}." );
7676
}
@@ -166,6 +166,7 @@ public function list_( $args, $assoc_args ) {
166166
$menu_locations = get_nav_menu_locations();
167167
foreach ( $menus as &$menu ) {
168168

169+
// @phpstan-ignore property.notFound
169170
$menu->locations = [];
170171
foreach ( $menu_locations as $location => $term_id ) {
171172

src/Menu_Item_Command.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Menu_Item_Command extends WP_CLI_Command {
9292
public function list_( $args, $assoc_args ) {
9393

9494
$items = wp_get_nav_menu_items( $args[0] );
95-
if ( false === $items || is_wp_error( $items ) ) {
95+
if ( false === $items ) {
9696
WP_CLI::error( 'Invalid menu.' );
9797
}
9898

@@ -367,8 +367,10 @@ public function delete( $args, $assoc_args ) {
367367

368368
foreach ( $args as $arg ) {
369369

370-
$post = get_post( $arg );
371-
$menu_term = get_the_terms( $arg, 'nav_menu' );
370+
$post = get_post( $arg );
371+
$menu_term = get_the_terms( $arg, 'nav_menu' );
372+
373+
// @phpstan-ignore cast.int
372374
$parent_menu_id = (int) get_post_meta( $arg, '_menu_item_menu_item_parent', true );
373375
$result = wp_delete_post( $arg, true );
374376
if ( ! $result ) {
@@ -408,10 +410,10 @@ public function delete( $args, $assoc_args ) {
408410
private function add_or_update_item( $method, $type, $args, $assoc_args ) {
409411

410412
$menu = $args[0];
411-
$menu_item_db_id = Utils\get_flag_value( $args, 1, 0 );
413+
$menu_item_db_id = $args[1] ?? 0;
412414

413415
$menu = wp_get_nav_menu_object( $menu );
414-
if ( ! $menu || is_wp_error( $menu ) ) {
416+
if ( false === $menu ) {
415417
WP_CLI::error( 'Invalid menu.' );
416418
}
417419

@@ -422,6 +424,14 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
422424
if ( 'update' === $method ) {
423425

424426
$menu_item_obj = get_post( $menu_item_db_id );
427+
428+
if ( ! $menu_item_obj ) {
429+
WP_CLI::error( 'Invalid menu.' );
430+
}
431+
432+
/**
433+
* @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, post_status: string, menu_order: int} $menu_item_obj
434+
*/
425435
$menu_item_obj = wp_setup_nav_menu_item( $menu_item_obj );
426436

427437
// Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
@@ -502,7 +512,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
502512
}
503513

504514
if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
505-
WP_CLI::line( $result );
515+
WP_CLI::line( (string) $result );
506516
} elseif ( 'add' === $method ) {
507517
WP_CLI::success( 'Menu item added.' );
508518
} elseif ( 'update' === $method ) {

src/Menu_Location_Command.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ function ( $o ) {
107107
* Success: Assigned location primary to menu primary-menu.
108108
*
109109
* @subcommand assign
110+
*
111+
* @param array{string, string} $args
110112
*/
111113
public function assign( $args, $assoc_args ) {
112114

@@ -147,18 +149,20 @@ public function assign( $args, $assoc_args ) {
147149
* Success: Removed location from menu.
148150
*
149151
* @subcommand remove
152+
*
153+
* @param array{string, string} $args
150154
*/
151155
public function remove( $args, $assoc_args ) {
152156

153157
list( $menu, $location ) = $args;
154158

155159
$menu = wp_get_nav_menu_object( $menu );
156-
if ( ! $menu || is_wp_error( $menu ) ) {
160+
if ( false === $menu ) {
157161
WP_CLI::error( 'Invalid menu.' );
158162
}
159163

160164
$locations = get_nav_menu_locations();
161-
if ( Utils\get_flag_value( $locations, $location ) !== $menu->term_id ) {
165+
if ( ( $locations[ $location ] ?? null ) !== $menu->term_id ) {
162166
WP_CLI::error( "Menu isn't assigned to location." );
163167
}
164168

0 commit comments

Comments
 (0)