wp-cli / entity-command

Manage WordPress comments, menus, options, posts, sites, terms, and users.
MIT License
100 stars 90 forks source link

PHP warning in `wp comment recount` command with invalid post ID #464

Closed ernilambar closed 8 months ago

ernilambar commented 8 months ago

Bug Report

Describe the current, buggy behavior

In comment recount command, if Post ID does not exist, PHP warnings are generated.

wp comment recount 111

PHP Warning:  Attempt to read property "ID" on null in /Users/nilambarsharma/.../extension-command/vendor/wp-cli/entity-command/src/Comment_Command.php on line 680
Warning: Attempt to read property "ID" on null in /Users/nilambarsharma/.../extension-command/vendor/wp-cli/entity-command/src/Comment_Command.php on line 680
Warning: Post  doesn't exist.

Describe what you would expect as the correct outcome

Proper message should be displayed without PHP warnings.

Let us know what environment you are running this on

OS:     Darwin 22.6.0 Darwin Kernel Version 22.6.0: Tue Nov  7 21:48:06 PST 2023; root:xnu-8796.141.3.702.9~2/RELEASE_X86_64 x86_64
Shell:  /bin/zsh
PHP binary:     /usr/local/Cellar/php@8.2/8.2.15/bin/php
PHP version:    8.2.15
php.ini used:   /usr/local/etc/php/8.2/php.ini
MySQL binary:   /usr/local/bin/mysql
MySQL version:  mysql  Ver 8.3.0 for macos13.6 on x86_64 (Homebrew)
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/nilambarsharma/Sites/staging
WP-CLI packages dir:    /Users/nilambarsharma/.wp-cli/packages/
WP-CLI cache dir:       /Users/nilambarsharma/.wp-cli/cache
WP-CLI global config:   /Users/nilambarsharma/.wp-cli/config.yml
WP-CLI project config:  /Users/nilambarsharma/Sites/staging/wp-cli.local.yml
WP-CLI version: 2.10.0
swissspidy commented 8 months ago

Looks like it's just a matter of moving the function calls around here;

https://github.com/wp-cli/entity-command/blob/9ffed77cae04ef3a92991eac90d04b3f1a9fbc87/src/Comment_Command.php#L674-L682

wp_update_comment_count() should be inside the if.

Wanna submit a PR? :)