Skip to content

Commit d8c60d0

Browse files
committed
PHPStan level 6
1 parent b61308b commit d8c60d0

24 files changed

+139
-134
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
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": "dev-main"
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,14 @@
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",
235237
"prepare-tests": "install-package-tests",
236238
"test": [
237239
"@lint",
238240
"@phpcs",
241+
"@phpstan",
239242
"@phpunit",
240243
"@behat"
241244
]

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
- entity-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
- vendor/wp-cli/wp-cli-tests
9+
scanFiles:
10+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
11+
treatPhpDocTypesAsCertain: false
12+
dynamicConstantNames:
13+
- WP_DEBUG
14+
- WP_DEBUG_LOG
15+
- WP_DEBUG_DISPLAY
16+
ignoreErrors:
17+
- identifier: missingType.iterableValue
18+
- identifier: missingType.property
19+
- identifier: missingType.parameter
20+
- identifier: missingType.return

src/Comment_Command.php

Lines changed: 6 additions & 2 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

@@ -366,6 +367,8 @@ public function get( $args, $assoc_args ) {
366367
public function list_( $args, $assoc_args ) {
367368
$formatter = $this->get_formatter( $assoc_args );
368369

370+
// To be fixed in wp-cli/wp-cli.
371+
// @phpstan-ignore property.notFound
369372
if ( 'ids' === $formatter->format ) {
370373
$assoc_args['fields'] = 'comment_ID';
371374
}
@@ -642,16 +645,17 @@ public function unapprove( $args, $assoc_args ) {
642645
* total_comments: 19
643646
*/
644647
public function count( $args, $assoc_args ) {
645-
$post_id = Utils\get_flag_value( $args, 0, 0 );
648+
$post_id = $args[0] ?? null;
646649

647650
$count = wp_count_comments( $post_id );
648651

649652
// Move total_comments to the end of the object
650653
$total = $count->total_comments;
651654
unset( $count->total_comments );
655+
// @phpstan-ignore assign.propertyReadOnly
652656
$count->total_comments = $total;
653657

654-
foreach ( $count as $status => $count ) {
658+
foreach ( (array) $count as $status => $count ) {
655659
WP_CLI::line( str_pad( "$status:", 17 ) . $count );
656660
}
657661
}

src/Comment_Meta_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
104104
/**
105105
* Check that the comment ID exists
106106
*
107-
* @param int
107+
* @param int $object_id
108108
*/
109109
protected function check_object_id( $object_id ) {
110110
$fetcher = new CommentFetcher();
111-
$comment = $fetcher->get_check( $object_id );
111+
$comment = $fetcher->get_check( (string) $object_id );
112112
return $comment->comment_ID;
113113
}
114114
}

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: 4 additions & 4 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

@@ -408,10 +408,10 @@ public function delete( $args, $assoc_args ) {
408408
private function add_or_update_item( $method, $type, $args, $assoc_args ) {
409409

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

413413
$menu = wp_get_nav_menu_object( $menu );
414-
if ( ! $menu || is_wp_error( $menu ) ) {
414+
if ( false === $menu ) {
415415
WP_CLI::error( 'Invalid menu.' );
416416
}
417417

@@ -502,7 +502,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
502502
}
503503

504504
if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
505-
WP_CLI::line( $result );
505+
WP_CLI::line( (string) $result );
506506
} elseif ( 'add' === $method ) {
507507
WP_CLI::success( 'Menu item added.' );
508508
} elseif ( 'update' === $method ) {

src/Menu_Location_Command.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function list_( $args, $assoc_args ) {
7777

7878
$formatter = new Formatter( $assoc_args, [ 'location', 'description' ] );
7979

80+
// To be fixed in wp-cli/wp-cli.
81+
// @phpstan-ignore property.notFound
8082
if ( 'ids' === $formatter->format ) {
8183
$ids = array_map(
8284
function ( $o ) {
@@ -107,6 +109,8 @@ function ( $o ) {
107109
* Success: Assigned location primary to menu primary-menu.
108110
*
109111
* @subcommand assign
112+
*
113+
* @param array{string, string} $args
110114
*/
111115
public function assign( $args, $assoc_args ) {
112116

@@ -147,18 +151,20 @@ public function assign( $args, $assoc_args ) {
147151
* Success: Removed location from menu.
148152
*
149153
* @subcommand remove
154+
*
155+
* @param array{string, string} $args
150156
*/
151157
public function remove( $args, $assoc_args ) {
152158

153159
list( $menu, $location ) = $args;
154160

155161
$menu = wp_get_nav_menu_object( $menu );
156-
if ( ! $menu || is_wp_error( $menu ) ) {
162+
if ( false === $menu ) {
157163
WP_CLI::error( 'Invalid menu.' );
158164
}
159165

160166
$locations = get_nav_menu_locations();
161-
if ( Utils\get_flag_value( $locations, $location ) !== $menu->term_id ) {
167+
if ( ( $locations[ $location ] ?? null ) !== $menu->term_id ) {
162168
WP_CLI::error( "Menu isn't assigned to location." );
163169
}
164170

src/Option_Command.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function add( $args, $assoc_args ) {
133133
$autoload = 'yes';
134134
}
135135

136+
// @phpstan-ignore argument.type
136137
if ( ! add_option( $key, $value, '', $autoload ) ) {
137138
WP_CLI::error( "Could not add option '{$key}'. Does it already exist?" );
138139
} else {
@@ -321,8 +322,8 @@ public function list_( $args, $assoc_args ) {
321322
function ( $a, $b ) use ( $orderby, $order ) {
322323
// Sort array.
323324
return 'asc' === $order
324-
? $a->$orderby > $b->$orderby
325-
: $a->$orderby < $b->$orderby;
325+
? $a[ $orderby ] <=> $b[ $orderby ]
326+
: $b[ $orderby ] <=> $a[ $orderby ];
326327
}
327328
);
328329
} elseif ( 'option_id' === $orderby && 'desc' === $order ) { // Sort by default descending.
@@ -431,6 +432,7 @@ public function update( $args, $assoc_args ) {
431432

432433
if ( $value === $old_value && null === $autoload ) {
433434
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
435+
// @phpstan-ignore argument.type
434436
} elseif ( update_option( $key, $value, $autoload ) ) {
435437
WP_CLI::success( "Updated '{$key}' option." );
436438
} else {

src/Post_Command.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ public function create( $args, $assoc_args ) {
182182
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
183183
}
184184

185-
if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
186-
WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
187-
}
188-
189185
$array_arguments = [ 'meta_input' ];
190186
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
191187

@@ -351,10 +347,6 @@ public function update( $args, $assoc_args ) {
351347
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
352348
}
353349

354-
if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
355-
WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
356-
}
357-
358350
$array_arguments = [ 'meta_input' ];
359351
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
360352

@@ -387,7 +379,7 @@ public function edit( $args, $assoc_args ) {
387379
$result = $this->_edit( $post->post_content, "WP-CLI post {$post->ID}" );
388380

389381
if ( false === $result ) {
390-
WP_CLI::warning( 'No change made to post content.', 'Aborted' );
382+
WP_CLI::warning( 'No change made to post content.' );
391383
} else {
392384
$this->update( $args, [ 'post_content' => $result ] );
393385
}
@@ -644,9 +636,12 @@ public function list_( $args, $assoc_args ) {
644636
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
645637
}
646638

639+
// To be fixed in wp-cli/wp-cli.
640+
// @phpstan-ignore property.notFound
647641
if ( 'ids' === $formatter->format ) {
648642
$query_args['fields'] = 'ids';
649643
$query = new WP_Query( $query_args );
644+
// @phpstan-ignore argument.type
650645
echo implode( ' ', $query->posts );
651646
} elseif ( 'count' === $formatter->format ) {
652647
$query_args['fields'] = 'ids';
@@ -656,8 +651,13 @@ public function list_( $args, $assoc_args ) {
656651
$query = new WP_Query( $query_args );
657652
$posts = array_map(
658653
function ( $post ) {
659-
$post->url = get_permalink( $post->ID );
660-
return $post;
654+
/**
655+
* @var \WP_Post $post
656+
*/
657+
658+
// @phpstan-ignore property.notFound
659+
$post->url = get_permalink( $post->ID );
660+
return $post;
661661
},
662662
$query->posts
663663
);

src/Post_Meta_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class Post_Meta_Command extends CommandWithMeta {
3030
/**
3131
* Check that the post ID exists
3232
*
33-
* @param int
33+
* @param int $object_id
3434
*/
3535
protected function check_object_id( $object_id ) {
3636
$fetcher = new PostFetcher();
37-
$post = $fetcher->get_check( $object_id );
37+
$post = $fetcher->get_check( (string) $object_id );
3838
return $post->ID;
3939
}
4040

src/Post_Type_Command.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ public function list_( $args, $assoc_args ) {
147147

148148
$types = array_map(
149149
function ( $type ) use ( $counts ) {
150-
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
151-
return $type;
150+
// @phpstan-ignore property.notFound
151+
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
152+
return $type;
152153
},
153154
$types
154155
);

src/Signup_Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function delete( $args, $assoc_args ) {
308308
/**
309309
* Deletes signup.
310310
*
311-
* @param stdClasss $signup
311+
* @param object{signup_id: int|string} $signup
312312
* @return bool True if success; otherwise false.
313313
*/
314314
private function delete_signup( $signup ) {

0 commit comments

Comments
 (0)