Skip to content

Conversation

@menno-ll
Copy link

Because the return type of the WP_CLI::error function is currently defined as null, PHPStan assumes code execution never stops when calling WP_CLI::error(). And code following phpstan assumes will still be executed.

For example:

$post = get_post( 1 );
if ( ! $post instanceof \WP_Post ) {
    WP_CLI::error( 'Post does not exist' );
}

// Here PHPStan thinks $post can still be something else than a \WP_Post, and will show appropriate error that the `post_type` property does not always exist on $post.
$post_type = $post->post_type;
WP_CLI::success( sprintf( 'The post type is %s', $post_type ) );

To solve this, i've looked at the original WP_CLI repo code for WP_CLI::error(), and put the @phpstan-return docblock line from there in the subs as well.

@phpstan-return ($exit is true|positive-int ? never : void)

@menno-ll menno-ll changed the title fix error phpstan return type for WP_CLI::error function fix phpstan not detecting WP_CLI::error() function will stop code execution Nov 14, 2025
@szepeviktor
Copy link
Member

Thank you for the PR.

The thing is: stubs are auto-generated.
How to include your change in the next release?

@menno-ll
Copy link
Author

menno-ll commented Nov 14, 2025

Whoops, i have not seen that, just looked at the code in the repo, modified it myself, and tested it.
Basically which was asked in https://github.com/php-stubs/wp-cli-stubs/blob/master/wp-cli-stubs.php however it's not as easy apparently as presented there...

What it seems like is that the commit / PR that added the line in WP_CLI has been done about 6 months ago, and a new release has not been published by WP_CLI since.

So I think that

  1. WP_CLI needs to tag a new version, it's about time.
  2. The generator needs to then run again on this repo with the new WP_CLI version
  3. New tag for php-stubs/wp-cli-stubs needs to be created

So let's hope they release a new version soon, so a new version of the subs can be generated :).

@szepeviktor
Copy link
Member

Please follow wp-cli/wp-cli#6026

@menno-ll
Copy link
Author

Will do, thank you, and have a good day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants