Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidArgument: add new create() method #542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Exception/InvalidArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
}
11 changes: 1 addition & 10 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/Transport/Fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Exception/InvalidArgumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace WpOrg\Requests\Tests\Exception;

use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Tests\TestCase;

/**
* @covers \WpOrg\Requests\Exception\InvalidArgument
*/
final class InvalidArgumentTest extends TestCase {

/**
* Test that the text of the exception is as expected.
*
* @return void
*/
public function testCreate() {
$this->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');
}
}