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 all 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
24 changes: 2 additions & 22 deletions entity-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,7 @@
);
WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );
WP_CLI::add_command( 'term', 'Term_Command' );
WP_CLI::add_command(
'term meta',
'Term_Meta_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '4.4', '<' ) ) {
WP_CLI::error( 'Requires WordPress 4.4 or greater.' );
}
},
)
);
WP_CLI::add_command( 'term meta', 'Term_Meta_Command' );
WP_CLI::add_command( 'user', 'User_Command' );
WP_CLI::add_command(
'user application-password',
Expand All @@ -84,17 +74,7 @@
)
);
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
WP_CLI::add_command(
'user session',
'User_Session_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
WP_CLI::error( 'Requires WordPress 4.0 or greater.' );
}
},
)
);
WP_CLI::add_command( 'user session', 'User_Session_Command' );
WP_CLI::add_command( 'user term', 'User_Term_Command' );

if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {
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
45 changes: 24 additions & 21 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 All @@ -376,29 +379,22 @@ public function list_( $args, $assoc_args ) {
$assoc_args['count'] = true;
}

if ( ! empty( $assoc_args['comment__in'] )
&& ! empty( $assoc_args['orderby'] )
&& 'comment__in' === $assoc_args['orderby']
&& Utils\wp_version_compare( '4.4', '<' ) ) {
$comments = [];
foreach ( $assoc_args['comment__in'] as $comment_id ) {
$comment = get_comment( $comment_id );
if ( $comment ) {
$comments[] = $comment;
} else {
WP_CLI::warning( "Invalid comment {$comment_id}." );
}
}
} else {
$query = new WP_Comment_Query();
$comments = $query->query( $assoc_args );
}
$query = new WP_Comment_Query();
$comments = $query->query( $assoc_args );

if ( 'count' === $formatter->format ) {
/**
* @var int $comments
*/
echo $comments;
} else {
if ( 'ids' === $formatter->format ) {
$comments = wp_list_pluck( $comments, 'comment_ID' );
/**
* @var \WP_Comment[] $comments
*/
$items = wp_list_pluck( $comments, 'comment_ID' );

$comments = $items;
} elseif ( is_array( $comments ) ) {
$comments = array_map(
function ( $comment ) {
Expand Down Expand Up @@ -439,7 +435,7 @@ public function delete( $args, $assoc_args ) {
$args,
$assoc_args,
function ( $comment_id, $assoc_args ) {
$force = Utils\get_flag_value( $assoc_args, 'force' );
$force = (bool) Utils\get_flag_value( $assoc_args, 'force' );

$status = wp_get_comment_status( $comment_id );
$result = wp_delete_comment( $comment_id, $force );
Expand All @@ -457,6 +453,9 @@ function ( $comment_id, $assoc_args ) {
private function call( $args, $status, $success, $failure ) {
$comment_id = absint( $args );

/**
* @var callable $func
*/
$func = "wp_{$status}_comment";

if ( ! $func( $comment_id ) ) {
Expand Down Expand Up @@ -642,16 +641,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 All @@ -673,6 +673,9 @@ public function count( $args, $assoc_args ) {
public function recount( $args ) {
foreach ( $args as $id ) {
if ( wp_update_comment_count( $id ) ) {
/**
* @var \WP_Post $post
*/
$post = get_post( $id );
WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." );
} else {
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 @@ public function create( $args, $assoc_args ) {

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

WP_CLI::line( $menu_id );
WP_CLI::line( (string) $menu_id );
} else {
WP_CLI::success( "Created menu {$menu_id}." );
}
Expand Down Expand Up @@ -166,6 +166,7 @@ public function list_( $args, $assoc_args ) {
$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
16 changes: 12 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 All @@ -422,6 +422,14 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
if ( 'update' === $method ) {

$menu_item_obj = get_post( $menu_item_db_id );

if ( ! $menu_item_obj ) {
WP_CLI::error( 'Invalid menu.' );
}

/**
* @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
*/
$menu_item_obj = wp_setup_nav_menu_item( $menu_item_obj );

// Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
Expand Down Expand Up @@ -502,7 +510,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
Loading
Loading