From a142d5a56a3f986159129ad3774c4846c9457290 Mon Sep 17 00:00:00 2001 From: "Vlad. Samoletov" Date: Thu, 4 Jun 2020 16:19:28 +0300 Subject: [PATCH 1/3] Fixes: #5356 --- php/class-wp-cli.php | 7 +++++++ tests/test-wp-cli.php | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index bb909e93ee..80d6776ce6 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -1010,6 +1010,13 @@ public static function error_to_string( $errors ) { if ( interface_exists( 'Throwable' ) && ( $errors instanceof \Throwable ) || ( $errors instanceof \Exception ) ) { return get_class( $errors ) . ': ' . $errors->getMessage(); } + + throw new InvalidArgumentException( + sprintf( + "Unsupported argument type passed to WP_CLI::error_to_string(): '%s'", + gettype( $errors ) + ) + ); } /** diff --git a/tests/test-wp-cli.php b/tests/test-wp-cli.php index a2a68f83bd..e0d67ec7fd 100644 --- a/tests/test-wp-cli.php +++ b/tests/test-wp-cli.php @@ -5,4 +5,9 @@ class WP_CLI_Test extends PHPUnit_Framework_TestCase { public function testGetPHPBinary() { $this->assertSame( WP_CLI\Utils\get_php_binary(), WP_CLI::get_php_binary() ); } + + public function testErrorToString() { + $this->setExpectedException( 'InvalidArgumentException', "Unsupported argument type passed to WP_CLI::error_to_string(): 'boolean'" ); + WP_CLI::error_to_string( (bool) true ); + } } From de5e261adcde2a65d0b349fcfcf27701d50c21cd Mon Sep 17 00:00:00 2001 From: "Vlad. S" Date: Thu, 4 Jun 2020 16:28:27 +0300 Subject: [PATCH 2/3] Redundant casting removed Co-authored-by: Pascal Birchler --- tests/test-wp-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-wp-cli.php b/tests/test-wp-cli.php index e0d67ec7fd..9ad4f1e26c 100644 --- a/tests/test-wp-cli.php +++ b/tests/test-wp-cli.php @@ -8,6 +8,6 @@ public function testGetPHPBinary() { public function testErrorToString() { $this->setExpectedException( 'InvalidArgumentException', "Unsupported argument type passed to WP_CLI::error_to_string(): 'boolean'" ); - WP_CLI::error_to_string( (bool) true ); + WP_CLI::error_to_string( true ); } } From 68d96c9cb5e199f18f1d32a3f8248185e4eed0d7 Mon Sep 17 00:00:00 2001 From: "Vlad. Samoletov" Date: Thu, 4 Jun 2020 16:48:41 +0300 Subject: [PATCH 3/3] PHPDoc and code update --- php/class-wp-cli.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index 80d6776ce6..701a5ed9e5 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -979,6 +979,8 @@ public static function print_value( $value, $assoc_args = array() ) { * Convert a WP_Error or Exception into a string * * @param mixed $errors + * @throws \InvalidArgumentException + * * @return string */ public static function error_to_string( $errors ) { @@ -1011,7 +1013,7 @@ public static function error_to_string( $errors ) { return get_class( $errors ) . ': ' . $errors->getMessage(); } - throw new InvalidArgumentException( + throw new \InvalidArgumentException( sprintf( "Unsupported argument type passed to WP_CLI::error_to_string(): '%s'", gettype( $errors )