From e85a4f92cba1143cf687fd4f66028c8e2af50555 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 31 Aug 2021 17:20:42 +0200 Subject: [PATCH 1/2] InvalidArgument: add new `create()` method ... to allow for standardizing the messages thrown via this Exception. Note: the `parent::__construct()` method is not overloaded with a `private` version, so variation is still possible, just not recommended. Includes adding a perfunctory unit test for the new method. --- src/Exception/InvalidArgument.php | 30 ++++++++++++++++++++++++- tests/Exception/InvalidArgumentTest.php | 24 ++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/Exception/InvalidArgumentTest.php diff --git a/src/Exception/InvalidArgument.php b/src/Exception/InvalidArgument.php index afcfc5b82..ef336e111 100644 --- a/src/Exception/InvalidArgument.php +++ b/src/Exception/InvalidArgument.php @@ -10,4 +10,32 @@ * @package Requests * @since 2.0.0 */ -final class InvalidArgument extends InvalidArgumentException {} +final class InvalidArgument extends InvalidArgumentException { + + /** + * Create a new invalid argument exception with a standardized text. + * + * @param int $position The argument position in the function signature. 1-based. + * @param string $name The argument name in the function signature. + * @param string $expected The argument type expected as a string. + * @param string $received The actual argument type received. + * + * @return \WpOrg\Requests\Exception\InvalidArgument + */ + public static function create($position, $name, $expected, $received) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace + $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + + return new self( + sprintf( + '%s::%s(): Argument #%d (%s) must be of type %s, %s given', + $stack[1]['class'], + $stack[1]['function'], + $position, + $name, + $expected, + $received + ) + ); + } +} diff --git a/tests/Exception/InvalidArgumentTest.php b/tests/Exception/InvalidArgumentTest.php new file mode 100644 index 000000000..f2f28742c --- /dev/null +++ b/tests/Exception/InvalidArgumentTest.php @@ -0,0 +1,24 @@ +expectException(InvalidArgument::class); + $this->expectExceptionMessage('InvalidArgumentTest::testCreate(): Argument #2 ($dummy) must be of type int|null, string given'); + + throw InvalidArgument::create(2, '$dummy', 'int|null', 'string'); + } +} From 30ad3601949d6463a18fce994065b1dd4c473606 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 31 Aug 2021 17:25:11 +0200 Subject: [PATCH 2/2] Transport classes: implement use of the new `InvalidArgument::create()` method --- src/Transport/Curl.php | 11 +---------- src/Transport/Fsockopen.php | 11 +---------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 5b1dc12da..672c5b21a 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -146,16 +146,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar } elseif (is_int($data) || is_float($data)) { $data = (string) $data; } else { - throw new InvalidArgument( - sprintf( - '%s: Argument #%d (%s) must be of type %s, %s given', - __METHOD__, - 3, - '$data', - 'array|string', - gettype($data) - ) - ); + throw InvalidArgument::create(3, '$data', 'array|string', gettype($data)); } } diff --git a/src/Transport/Fsockopen.php b/src/Transport/Fsockopen.php index 3088b0bc7..df06e551b 100644 --- a/src/Transport/Fsockopen.php +++ b/src/Transport/Fsockopen.php @@ -71,16 +71,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar } elseif (is_int($data) || is_float($data)) { $data = (string) $data; } else { - throw new InvalidArgument( - sprintf( - '%s: Argument #%d (%s) must be of type %s, %s given', - __METHOD__, - 3, - '$data', - 'array|string', - gettype($data) - ) - ); + throw InvalidArgument::create(3, '$data', 'array|string', gettype($data)); } }