Skip to content

PHPStan level 6 #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
"wp-cli/extension-command": "^1.2 || ^2",
"wp-cli/media-command": "^1.1 || ^2",
"wp-cli/super-admin-command": "^1 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "dev-main"
},
"config": {
"process-timeout": 7200,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"lock": false
},
Expand Down Expand Up @@ -230,12 +231,14 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpunit": "run-php-unit-tests",
"phpcbf": "run-phpcbf-cleanup",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
34 changes: 0 additions & 34 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -451,40 +451,6 @@ Feature: Manage WordPress posts
Test Post
"""

@less-than-wp-4.4
Scenario: Creating/updating posts with meta keys for WP < 4.4 has no effect so should give warning
When I try `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
Then the return code should be 0
And STDOUT should be a number
And save STDOUT as {POST_ID}
And STDERR should be:
"""
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
"""

When I run `wp post meta list {POST_ID} --format=count`
Then STDOUT should be:
"""
0
"""

When I try `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'`
Then the return code should be 0
And STDERR should be:
"""
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
"""
And STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""

When I run `wp post meta list {POST_ID} --format=count`
Then STDOUT should be:
"""
0
"""

Scenario: Publishing a post and setting a date fails if the edit_date flag is not passed.
Given a WP install

Expand Down
20 changes: 20 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
level: 6
paths:
- src
- entity-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
- vendor/wp-cli/wp-cli-tests
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
8 changes: 6 additions & 2 deletions src/Comment_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function get( $args, $assoc_args ) {
}

if ( ! isset( $comment->url ) ) {
// @phpstan-ignore property.notFound
$comment->url = get_comment_link( $comment );
}

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

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

$count = wp_count_comments( $post_id );

// Move total_comments to the end of the object
$total = $count->total_comments;
unset( $count->total_comments );
// @phpstan-ignore assign.propertyReadOnly
$count->total_comments = $total;

foreach ( $count as $status => $count ) {
foreach ( (array) $count as $status => $count ) {
WP_CLI::line( str_pad( "$status:", 17 ) . $count );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Comment_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
/**
* Check that the comment ID exists
*
* @param int
* @param int $object_id
*/
protected function check_object_id( $object_id ) {
$fetcher = new CommentFetcher();
$comment = $fetcher->get_check( $object_id );
$comment = $fetcher->get_check( (string) $object_id );
return $comment->comment_ID;
}
}
3 changes: 2 additions & 1 deletion src/Menu_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

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

WP_CLI::line( $menu_id );
WP_CLI::line( (string) $menu_id );

Check warning on line 73 in src/Menu_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Menu_Command.php#L73

Added line #L73 was not covered by tests
} else {
WP_CLI::success( "Created menu {$menu_id}." );
}
Expand Down Expand Up @@ -166,6 +166,7 @@
$menu_locations = get_nav_menu_locations();
foreach ( $menus as &$menu ) {

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

Expand Down
8 changes: 4 additions & 4 deletions src/Menu_Item_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Menu_Item_Command extends WP_CLI_Command {
public function list_( $args, $assoc_args ) {

$items = wp_get_nav_menu_items( $args[0] );
if ( false === $items || is_wp_error( $items ) ) {
if ( false === $items ) {
WP_CLI::error( 'Invalid menu.' );
}

Expand Down Expand Up @@ -408,10 +408,10 @@ public function delete( $args, $assoc_args ) {
private function add_or_update_item( $method, $type, $args, $assoc_args ) {

$menu = $args[0];
$menu_item_db_id = Utils\get_flag_value( $args, 1, 0 );
$menu_item_db_id = $args[1] ?? 0;

$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu || is_wp_error( $menu ) ) {
if ( false === $menu ) {
WP_CLI::error( 'Invalid menu.' );
}

Expand Down Expand Up @@ -502,7 +502,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
}

if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
WP_CLI::line( $result );
WP_CLI::line( (string) $result );
} elseif ( 'add' === $method ) {
WP_CLI::success( 'Menu item added.' );
} elseif ( 'update' === $method ) {
Expand Down
10 changes: 8 additions & 2 deletions src/Menu_Location_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function list_( $args, $assoc_args ) {

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

// To be fixed in wp-cli/wp-cli.
// @phpstan-ignore property.notFound
if ( 'ids' === $formatter->format ) {
$ids = array_map(
function ( $o ) {
Expand Down Expand Up @@ -107,6 +109,8 @@ function ( $o ) {
* Success: Assigned location primary to menu primary-menu.
*
* @subcommand assign
*
* @param array{string, string} $args
*/
public function assign( $args, $assoc_args ) {

Expand Down Expand Up @@ -147,18 +151,20 @@ public function assign( $args, $assoc_args ) {
* Success: Removed location from menu.
*
* @subcommand remove
*
* @param array{string, string} $args
*/
public function remove( $args, $assoc_args ) {

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

$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu || is_wp_error( $menu ) ) {
if ( false === $menu ) {
WP_CLI::error( 'Invalid menu.' );
}

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

Expand Down
6 changes: 4 additions & 2 deletions src/Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function add( $args, $assoc_args ) {
$autoload = 'yes';
}

// @phpstan-ignore argument.type
if ( ! add_option( $key, $value, '', $autoload ) ) {
WP_CLI::error( "Could not add option '{$key}'. Does it already exist?" );
} else {
Expand Down Expand Up @@ -321,8 +322,8 @@ public function list_( $args, $assoc_args ) {
function ( $a, $b ) use ( $orderby, $order ) {
// Sort array.
return 'asc' === $order
? $a->$orderby > $b->$orderby
: $a->$orderby < $b->$orderby;
? $a->$orderby <=> $b->$orderby
: $b->$orderby <=> $a->$orderby;
Comment on lines +325 to +326
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this is not the wrong way around 🤔

}
);
} elseif ( 'option_id' === $orderby && 'desc' === $order ) { // Sort by default descending.
Expand Down Expand Up @@ -431,6 +432,7 @@ public function update( $args, $assoc_args ) {

if ( $value === $old_value && null === $autoload ) {
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
// @phpstan-ignore argument.type
} elseif ( update_option( $key, $value, $autoload ) ) {
WP_CLI::success( "Updated '{$key}' option." );
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ public function create( $args, $assoc_args ) {
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
}

if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured with our move to WP 4.9+ we can remove this compat code.

WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
}

$array_arguments = [ 'meta_input' ];
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );

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

if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
}

$array_arguments = [ 'meta_input' ];
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );

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

if ( false === $result ) {
WP_CLI::warning( 'No change made to post content.', 'Aborted' );
WP_CLI::warning( 'No change made to post content.' );
} else {
$this->update( $args, [ 'post_content' => $result ] );
}
Expand Down Expand Up @@ -644,9 +636,12 @@ public function list_( $args, $assoc_args ) {
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
}

// To be fixed in wp-cli/wp-cli.
// @phpstan-ignore property.notFound
if ( 'ids' === $formatter->format ) {
$query_args['fields'] = 'ids';
$query = new WP_Query( $query_args );
// @phpstan-ignore argument.type
echo implode( ' ', $query->posts );
} elseif ( 'count' === $formatter->format ) {
$query_args['fields'] = 'ids';
Expand All @@ -656,8 +651,13 @@ public function list_( $args, $assoc_args ) {
$query = new WP_Query( $query_args );
$posts = array_map(
function ( $post ) {
$post->url = get_permalink( $post->ID );
return $post;
/**
* @var \WP_Post $post
*/

// @phpstan-ignore property.notFound
$post->url = get_permalink( $post->ID );
return $post;
},
$query->posts
);
Expand Down
4 changes: 2 additions & 2 deletions src/Post_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class Post_Meta_Command extends CommandWithMeta {
/**
* Check that the post ID exists
*
* @param int
* @param int $object_id
*/
protected function check_object_id( $object_id ) {
$fetcher = new PostFetcher();
$post = $fetcher->get_check( $object_id );
$post = $fetcher->get_check( (string) $object_id );
return $post->ID;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Post_Type_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ public function list_( $args, $assoc_args ) {

$types = array_map(
function ( $type ) use ( $counts ) {
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
return $type;
// @phpstan-ignore property.notFound
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
return $type;
},
$types
);
Expand Down
2 changes: 1 addition & 1 deletion src/Signup_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function delete( $args, $assoc_args ) {
/**
* Deletes signup.
*
* @param stdClasss $signup
* @param object{signup_id: int|string} $signup
* @return bool True if success; otherwise false.
*/
private function delete_signup( $signup ) {
Expand Down
Loading