wp-cli / wp-cli

⚙️ WP-CLI framework

Home Page:https://wp-cli.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP deprecation warnings triggered by calls to `WP_CLI::error()` and `WP_CLI::halt()`

christianwach opened this issue · comments

Bug Report

Describe the current, buggy behavior

I'm seeing a couple of deprecation warnings triggered by calls to WP_CLI::error() and WP_CLI::halt():

PHP Deprecated:  Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated in phar:///path/to/wp-cli/wp-cli/php/class-wp-cli.php on line 919

And:

PHP Deprecated:  Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated in phar:///path/to/wp-cli/wp-cli/php/class-wp-cli.php on line 938

The first parameter of the class constructor for Exception is defined as being of type string yet null is being passed on both of the lines in class-wp-cli.php and PHP versions greater than 8.1.x are now grumbling about it in my logs.

Describe how other contributors can replicate this bug

This only happens when WP_CLI::error() and WP_CLI::halt() are called in a separate command via WP_CLI::runcommand():

  /**
   * Trigger deprecation notices.
   *
   * @param array $args The WP-CLI positional arguments.
   * @param array $assoc_args The WP-CLI associative arguments.
   */
  public function one($args, $assoc_args) {

    // Trigger deprecation notice in `WP_CLI::halt()`
    $command = 'foo bar two --format=json';
    $options = ['launch' => false, 'return' => true, 'parse' => 'json', 'exit_error' => false, 'command_args' => ['--quiet']];
    $result = WP_CLI::runcommand($command, $options);

    // Trigger deprecation notice in `WP_CLI::error()`
    $command = 'foo bar three --format=json';
    $options = ['launch' => false, 'return' => true, 'parse' => 'json', 'exit_error' => false, 'command_args' => ['--quiet']];
    $result = WP_CLI::runcommand($command, $options);

  }

  /**
   * Trigger deprecation notice in `WP_CLI::halt()`.
   *
   * @param array $args The WP-CLI positional arguments.
   * @param array $assoc_args The WP-CLI associative arguments.
   */
  public function two($args, $assoc_args) {
    WP_CLI::halt(0);
  }

  /**
   * Trigger deprecation notice in `WP_CLI::error()`.
   *
   * @param array $args The WP-CLI positional arguments.
   * @param array $assoc_args The WP-CLI associative arguments.
   */
  public function three($args, $assoc_args) {
    WP_CLI::error('Foo Bar');
  }

Let us know what environment you are running this on

OS:	Darwin 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64
Shell:	/bin/zsh
PHP binary:	/Applications/MAMP/bin/php/php8.1.13/bin/php
PHP version:	8.1.13
php.ini used:	/Applications/MAMP/bin/php/php8.1.13/conf/php.ini
MySQL binary:	/usr/local/bin/mysql
MySQL version:	mysql  Ver 14.14 Distrib 5.7.39, for osx11.0 (x86_64) using  EditLine wrapper
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/foo/bar/httpdocs
WP-CLI packages dir:	/Users/foo/.wp-cli/packages/
WP-CLI cache dir:	/Users/foo/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version:	2.9.0

Provide a possible solution

Passing an empty string avoids the warnings:

throw new ExitException( '', $return_code );

PR to follow shortly.

Closed via #5897