Skip to content

Commit a3e1b0a

Browse files
committed
Merge branch 'main' into patch-1
2 parents f6e928e + a538162 commit a3e1b0a

30 files changed

+508
-283
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
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",
1919
"wp-cli/db-command": "^1.3 || ^2",
2020
"wp-cli/extension-command": "^1.2 || ^2",
2121
"wp-cli/media-command": "^1.1 || ^2",
2222
"wp-cli/super-admin-command": "^1 || ^2",
23-
"wp-cli/wp-cli-tests": "^4"
23+
"wp-cli/wp-cli-tests": "^5"
2424
},
2525
"config": {
2626
"process-timeout": 7200,
2727
"sort-packages": true,
2828
"allow-plugins": {
2929
"dealerdirect/phpcodesniffer-composer-installer": true,
30-
"johnpbloch/wordpress-core-installer": true
30+
"johnpbloch/wordpress-core-installer": true,
31+
"phpstan/extension-installer": true
3132
},
3233
"lock": false
3334
},
@@ -230,12 +231,15 @@
230231
"behat-rerun": "rerun-behat-tests",
231232
"lint": "run-linter-tests",
232233
"phpcs": "run-phpcs-tests",
234+
"phpstan": "run-phpstan-tests",
233235
"phpunit": "run-php-unit-tests",
234236
"phpcbf": "run-phpcbf-cleanup",
237+
"phpstan": "run-phpstan-tests",
235238
"prepare-tests": "install-package-tests",
236239
"test": [
237240
"@lint",
238241
"@phpcs",
242+
"@phpstan",
239243
"@phpunit",
240244
"@behat"
241245
]

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
@@ -470,40 +470,6 @@ Feature: Manage WordPress posts
470470
Test Post
471471
"""
472472

473-
@less-than-wp-4.4
474-
Scenario: Creating/updating posts with meta keys for WP < 4.4 has no effect so should give warning
475-
When I try `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
476-
Then the return code should be 0
477-
And STDOUT should be a number
478-
And save STDOUT as {POST_ID}
479-
And STDERR should be:
480-
"""
481-
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
482-
"""
483-
484-
When I run `wp post meta list {POST_ID} --format=count`
485-
Then STDOUT should be:
486-
"""
487-
0
488-
"""
489-
490-
When I try `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'`
491-
Then the return code should be 0
492-
And STDERR should be:
493-
"""
494-
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
495-
"""
496-
And STDOUT should be:
497-
"""
498-
Success: Updated post {POST_ID}.
499-
"""
500-
501-
When I run `wp post meta list {POST_ID} --format=count`
502-
Then STDOUT should be:
503-
"""
504-
0
505-
"""
506-
507473
Scenario: Publishing a post and setting a date fails if the edit_date flag is not passed.
508474
Given a WP install
509475

features/term-recount.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Feature: Recount terms on a taxonomy
4949

5050
When I run `wp term recount category`
5151
And I run `wp term get category {TERM_ID} --field=count`
52+
Then STDOUT should be:
5253
"""
5354
1
5455
"""

features/user.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,39 @@ Feature: Manage WordPress users
432432
publish_posts
433433
"""
434434
435+
Scenario: Show error when trying to remove capability same as role
436+
Given a WP install
437+
438+
When I run `wp user create testuser2 [email protected] --first_name=test --last_name=user --role=contributor --porcelain`
439+
Then STDOUT should be a number
440+
And save STDOUT as {USER_ID}
441+
442+
When I run `wp user list-caps {USER_ID}`
443+
Then STDOUT should contain:
444+
"""
445+
contributor
446+
"""
447+
448+
When I run `wp user get {USER_ID} --field=roles`
449+
Then STDOUT should contain:
450+
"""
451+
contributor
452+
"""
453+
454+
When I try `wp user remove-cap {USER_ID} contributor`
455+
Then the return code should be 1
456+
And STDERR should be:
457+
"""
458+
Error: Aborting because a role has the same name as 'contributor'. Use `wp user remove-cap {USER_ID} contributor --force` to proceed with the removal.
459+
"""
460+
And STDOUT should be empty
461+
462+
When I run `wp user remove-cap {USER_ID} contributor --force`
463+
Then STDOUT should be:
464+
"""
465+
Success: Removed 'contributor' cap for testuser2 ({USER_ID}).
466+
"""
467+
435468
Scenario: Show password when creating a user
436469
Given a WP install
437470

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<!-- For help understanding the `testVersion` configuration setting:
4040
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41-
<config name="testVersion" value="5.6-"/>
41+
<config name="testVersion" value="7.2-"/>
4242

4343
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
4444
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->

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

0 commit comments

Comments
 (0)