diff --git a/src/Fetch/Enum/Status.php b/src/Fetch/Enum/Status.php index 1656307..31b4f0a 100644 --- a/src/Fetch/Enum/Status.php +++ b/src/Fetch/Enum/Status.php @@ -1,5 +1,7 @@ 'GET', + 'method' => 'GET', 'headers' => [], 'timeout' => self::DEFAULT_TIMEOUT, ]; @@ -44,13 +44,12 @@ class ClientHandler implements ClientHandlerInterface /** * ClientHandler constructor. * - * @param ClientInterface|null $syncClient The synchronous HTTP client. - * @param array $options The options for the request. - * @param int|null $timeout Timeout for the request. - * @param int|null $retries Number of retries for the request. - * @param int|null $retryDelay Delay between retries. - * @param bool $isAsync Whether the request is asynchronous. - * + * @param ClientInterface|null $syncClient The synchronous HTTP client. + * @param array $options The options for the request. + * @param int|null $timeout Timeout for the request. + * @param int|null $retries Number of retries for the request. + * @param int|null $retryDelay Delay between retries. + * @param bool $isAsync Whether the request is asynchronous. * @return void */ public function __construct( @@ -60,21 +59,14 @@ public function __construct( protected ?int $retries = null, protected ?int $retryDelay = null, protected bool $isAsync = false - ) { - } + ) {} /** * Apply options and execute the request. - * - * @param string $method - * @param string $uri - * @param array $options - * - * @return mixed */ public static function handle(string $method, string $uri, array $options = []): mixed { - $handler = new static(); + $handler = new static; $handler->applyOptions($options); return $handler->finalizeRequest($method, $uri); @@ -82,10 +74,6 @@ public static function handle(string $method, string $uri, array $options = []): /** * Apply the options to the handler. - * - * @param array $options - * - * @return void */ protected function applyOptions(array $options): void { @@ -107,11 +95,6 @@ protected function applyOptions(array $options): void /** * Finalize the request and send it. - * - * @param string $method - * @param string $uri - * - * @return mixed */ protected function finalizeRequest(string $method, string $uri): mixed { @@ -125,8 +108,6 @@ protected function finalizeRequest(string $method, string $uri): mixed /** * Merge class properties and options into the final options array. - * - * @return void */ protected function mergeOptionsAndProperties(): void { @@ -137,8 +118,6 @@ protected function mergeOptionsAndProperties(): void /** * Send a synchronous HTTP request. - * - * @return ResponseInterface */ protected function sendSync(): ResponseInterface { @@ -155,8 +134,6 @@ protected function sendSync(): ResponseInterface /** * Send an asynchronous HTTP request. - * - * @return AsyncHelperInterface */ protected function sendAsync(): AsyncHelperInterface { @@ -167,10 +144,6 @@ protected function sendAsync(): AsyncHelperInterface /** * Implement retry logic for the request with exponential backoff. - * - * @param callable $request - * - * @return ResponseInterface */ protected function retryRequest(callable $request): ResponseInterface { @@ -193,10 +166,6 @@ protected function retryRequest(callable $request): ResponseInterface /** * Determine if an error is retryable. - * - * @param RequestException $e - * - * @return bool */ protected function isRetryableError(RequestException $e): bool { @@ -205,8 +174,6 @@ protected function isRetryableError(RequestException $e): bool /** * Get the full URI for the request. - * - * @return string */ protected function getFullUri(): string { @@ -234,8 +201,6 @@ protected function getFullUri(): string /** * Reset the handler state. - * - * @return self */ public function reset(): self { @@ -250,13 +215,11 @@ public function reset(): self /** * Get the synchronous HTTP client. - * - * @return ClientInterface */ public function getSyncClient(): ClientInterface { if (! $this->syncClient) { - $this->syncClient = new SyncClient(); + $this->syncClient = new SyncClient; } return $this->syncClient; @@ -264,10 +227,6 @@ public function getSyncClient(): ClientInterface /** * Set the synchronous HTTP client. - * - * @param ClientInterface $syncClient - * - * @return self */ public function setSyncClient(ClientInterface $syncClient): self { @@ -278,8 +237,6 @@ public function setSyncClient(ClientInterface $syncClient): self /** * Get the default options for the request. - * - * @return array */ public static function getDefaultOptions(): array { @@ -288,10 +245,6 @@ public static function getDefaultOptions(): array /** * Set the base URI for the request. - * - * @param string $baseUri - * - * @return self */ public function baseUri(string $baseUri): self { @@ -302,10 +255,6 @@ public function baseUri(string $baseUri): self /** * Set the token for the request. - * - * @param string $token - * - * @return self */ public function withToken(string $token): self { @@ -316,11 +265,6 @@ public function withToken(string $token): self /** * Set the basic auth for the request. - * - * @param string $username - * @param string $password - * - * @return self */ public function withAuth(string $username, string $password): self { @@ -331,10 +275,6 @@ public function withAuth(string $username, string $password): self /** * Set the headers for the request. - * - * @param array $headers - * - * @return self */ public function withHeaders(array $headers): self { @@ -348,10 +288,6 @@ public function withHeaders(array $headers): self /** * Set the body for the request. - * - * @param array $body - * - * @return self */ public function withBody(array $body): self { @@ -362,10 +298,6 @@ public function withBody(array $body): self /** * Set the query parameters for the request. - * - * @param array $queryParams - * - * @return self */ public function withQueryParameters(array $queryParams): self { @@ -376,10 +308,6 @@ public function withQueryParameters(array $queryParams): self /** * Set the timeout for the request. - * - * @param int $seconds - * - * @return self */ public function timeout(int $seconds): self { @@ -390,11 +318,6 @@ public function timeout(int $seconds): self /** * Set the retry logic for the request. - * - * @param int $retries - * @param int $delay - * - * @return self */ public function retry(int $retries, int $delay = 100): self { @@ -406,8 +329,6 @@ public function retry(int $retries, int $delay = 100): self /** * Set the request to be asynchronous. - * - * @return self */ public function async(): self { @@ -418,10 +339,6 @@ public function async(): self /** * Set the proxy for the request. - * - * @param string|array $proxy - * - * @return self */ public function withProxy(string|array $proxy): self { @@ -432,10 +349,6 @@ public function withProxy(string|array $proxy): self /** * Set the cookies for the request. - * - * @param bool|\GuzzleHttp\Cookie\CookieJarInterface $cookies - * - * @return self */ public function withCookies(bool|CookieJarInterface $cookies): self { @@ -446,10 +359,6 @@ public function withCookies(bool|CookieJarInterface $cookies): self /** * Set whether to follow redirects. - * - * @param bool|array $redirects - * - * @return self */ public function withRedirects(bool|array $redirects = true): self { @@ -460,10 +369,6 @@ public function withRedirects(bool|array $redirects = true): self /** * Set the certificate for the request. - * - * @param string|array $cert - * - * @return self */ public function withCert(string|array $cert): self { @@ -474,10 +379,6 @@ public function withCert(string|array $cert): self /** * Set the SSL key for the request. - * - * @param string|array $sslKey - * - * @return self */ public function withSslKey(string|array $sslKey): self { @@ -488,10 +389,6 @@ public function withSslKey(string|array $sslKey): self /** * Set the stream option for the request. - * - * @param bool $stream - * - * @return self */ public function withStream(bool $stream): self { @@ -502,10 +399,6 @@ public function withStream(bool $stream): self /** * Finalize and send a GET request. - * - * @param string $uri - * - * @return mixed */ public function get(string $uri): mixed { @@ -514,11 +407,6 @@ public function get(string $uri): mixed /** * Finalize and send a POST request. - * - * @param string $uri - * @param mixed $body - * - * @return mixed */ public function post(string $uri, mixed $body = null): mixed { @@ -531,11 +419,6 @@ public function post(string $uri, mixed $body = null): mixed /** * Finalize and send a PUT request. - * - * @param string $uri - * @param mixed $body - * - * @return mixed */ public function put(string $uri, mixed $body = null): mixed { @@ -548,10 +431,6 @@ public function put(string $uri, mixed $body = null): mixed /** * Finalize and send a DELETE request. - * - * @param string $uri - * - * @return mixed */ public function delete(string $uri): mixed { @@ -560,10 +439,6 @@ public function delete(string $uri): mixed /** * Finalize and send an OPTIONS request. - * - * @param string $uri - * - * @return mixed */ public function options(string $uri): mixed { diff --git a/src/Fetch/Http/Response.php b/src/Fetch/Http/Response.php index ab4c93c..1161aff 100644 --- a/src/Fetch/Http/Response.php +++ b/src/Fetch/Http/Response.php @@ -1,29 +1,23 @@ andReturn(new Response(200, [], json_encode(['success' => true]))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->get('https://example.com'); @@ -42,7 +44,7 @@ ->andReturn(new Response(200, [], json_encode(['async' => 'result']))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); async(fn () => $clientHandler->get('https://example.com')) @@ -65,7 +67,7 @@ ->andReturn(new Response(200, [], 'Headers checked')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withHeaders(['Authorization' => 'Bearer token']) @@ -87,7 +89,7 @@ ->andReturn(new Response(200, [], 'Query params checked')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withQueryParameters(['foo' => 'bar', 'baz' => 'qux']) @@ -109,7 +111,7 @@ ->andThrow(new RequestException('Timeout', new Request('GET', 'https://example.com'))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); expect(fn () => $clientHandler->timeout(1)->get('https://example.com')) @@ -131,7 +133,7 @@ ->andReturn(new Response(200, [], 'Success after retry')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->retry(2, 100)->get('https://example.com'); @@ -152,7 +154,7 @@ ->andReturn(new Response(201, [], 'Created')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withBody(['name' => 'John']) @@ -177,7 +179,7 @@ ->andReturn(new Response(200, [], 'Success after retry')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); async(fn () => $clientHandler->retry(2, 100)->get('https://example.com')) diff --git a/tests/Pest.php b/tests/Pest.php index fd279ad..4e6310e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ andReturn(new Response(200, [], json_encode(['success' => true]))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->get('https://example.com'); @@ -40,7 +42,7 @@ ->andReturn(new Response(200, [], json_encode(['success' => true]))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $response = $clientHandler->setSyncClient($mockClient) ->baseUri('http://localhost') ->get('/'); @@ -60,7 +62,7 @@ ->andReturn(new Response(200, [], json_encode(['async' => 'result']))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); // Directly invoke the async method and interact with the AsyncHelper instance @@ -82,7 +84,7 @@ ->andReturn(new Response(201, [], json_encode(['success' => true]))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $response = $clientHandler->setSyncClient($mockClient) ->baseUri('http://localhost') ->withHeaders(['Content-Type' => 'application/json']) @@ -107,7 +109,7 @@ ->andReturn(new Response(200, [], 'Headers checked')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withHeaders(['Authorization' => 'Bearer token']) @@ -129,7 +131,7 @@ ->andReturn(new Response(200, [], 'Query params checked')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withQueryParameters(['foo' => 'bar', 'baz' => 'qux']) @@ -151,7 +153,7 @@ ->andThrow(new RequestException('Timeout', new Request('GET', 'https://example.com'))); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); try { @@ -176,7 +178,7 @@ ->andReturn(new Response(200, [], 'Success after retry')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->retry(2)->get('https://example.com'); // Retry once on failure @@ -197,7 +199,7 @@ ->andReturn(new Response(201, [], 'Created')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); $response = $clientHandler->withBody(['name' => 'John']) @@ -222,7 +224,7 @@ ->andReturn(new Response(200, [], 'Success after retry')); }); - $clientHandler = new ClientHandler(); + $clientHandler = new ClientHandler; $clientHandler->setSyncClient($mockClient); async(fn () => $clientHandler->retry(2)->get('https://example.com')) diff --git a/tests/Unit/FetchTest.php b/tests/Unit/FetchTest.php index 60ded49..0221c7a 100644 --- a/tests/Unit/FetchTest.php +++ b/tests/Unit/FetchTest.php @@ -1,10 +1,12 @@ ['Authorization' => 'Bearer token'], - 'client' => $mockClient + 'client' => $mockClient, ]); expect($response->text())->toBe('Headers checked'); @@ -103,8 +105,8 @@ }); $response = fetch('http://localhost', [ - 'query' => ['foo' => 'bar', 'baz' => 'qux'], - 'client' => $mockClient + 'query' => ['foo' => 'bar', 'baz' => 'qux'], + 'client' => $mockClient, ]); expect($response->text())->toBe('Query params checked'); @@ -165,8 +167,8 @@ $response = fetch('http://localhost/users', [ 'method' => 'POST', - 'body' => json_encode(['name' => 'John']), - 'client' => $mockClient + 'body' => json_encode(['name' => 'John']), + 'client' => $mockClient, ]); expect($response->getStatusCode())->toBe(201); diff --git a/tests/Unit/ResponseTest.php b/tests/Unit/ResponseTest.php index ed5cc62..f125712 100644 --- a/tests/Unit/ResponseTest.php +++ b/tests/Unit/ResponseTest.php @@ -1,5 +1,7 @@