Skip to content

Commit

Permalink
Allow library to work on PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cs278 committed Aug 29, 2024
1 parent a8c722e commit 8582a1a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
},
"require": {
"php": ">= 8.2",
"php": ">= 8.1",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-factory-implementation": "*",
Expand All @@ -19,8 +19,8 @@
"bamarni/composer-bin-plugin": "^1.8",
"nyholm/psr7": "^1.8",
"php-http/discovery": "^1.17",
"symfony/http-client": "^7.1",
"symfony/var-dumper": "^7.1"
"symfony/http-client": "^6.4 || ^7.1",
"symfony/var-dumper": "^6.4 || ^7.1"
},
"suggest": {
"php-http/discovery": "For automatic discovery and configuration of HTTP client."
Expand Down
18 changes: 17 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ public function __construct(
private readonly string $secretKey,
) {}

/**
* @param (HttpClientInterface&RequestFactoryInterface&StreamFactoryInterface)|null $httpClient
*/
public static function create(
#[\SensitiveParameter()]
string $secretKey,
(HttpClientInterface&RequestFactoryInterface&StreamFactoryInterface)|null $httpClient = null,
object|null $httpClient = null,
): self {
// @todo This can be removed when minimum requirement is raised to PHP 8.2.
if ($httpClient !== null && !($httpClient instanceof HttpClientInterface && $httpClient instanceof RequestFactoryInterface && $httpClient instanceof StreamFactoryInterface)) {
throw new \TypeError(sprintf(
'%s::%s(): Argument #2 ($httpClient) must be of type (%s&%s&%s)|null, %s given',
self::class,
__METHOD__,
HttpClientInterface::class,
RequestFactoryInterface::class,
StreamFactoryInterface::class,
\get_debug_type($httpClient),
));
}

if ($httpClient === null) {
if (!\class_exists(Psr18Client::class)) {
throw new \LogicException('Pass in a suitable object or install package `php-http/discovery` to have one automatically created');
Expand Down
18 changes: 18 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

#[CoversClass(Client::class)]
#[UsesClass(Result::class)]
#[UsesClass(Exception\NotSupportedResponseException::class)]
final class ClientTest extends TestCase
{
public function testCreateWithInvalidObject(): void
{
$httpClient = new class implements HttpClientInterface {
public function sendRequest(RequestInterface $request): ResponseInterface
{
throw new \Exception();
}
};

$this->expectException(\TypeError::class);
$this->expectExceptionMessage('WiderPlan\\Hcaptcha\\Client::create(): Argument #2 ($httpClient) must be of type (Psr\\Http\\Client\\ClientInterface&Psr\\Http\\Message\\RequestFactoryInterface&Psr\\Http\\Message\\StreamFactoryInterface)|null, Psr\Http\Client\ClientInterface@anonymous given');

// @phpstan-ignore-next-line argument.type
Client::create('', $httpClient);
}

public function testWithoutSiteKeyAndRemoteIp(): void
{
$client = $this->createClient(function (string $method, string $url, string $body) {
Expand Down

0 comments on commit 8582a1a

Please sign in to comment.