diff --git a/.github/workflows/laravel-pint.yml b/.github/workflows/laravel-pint.yml new file mode 100644 index 0000000..8abef71 --- /dev/null +++ b/.github/workflows/laravel-pint.yml @@ -0,0 +1,34 @@ +name: Check & fix styling + +on: [push] + +jobs: + lint: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: [8.3] + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: json, dom, curl, libxml, mbstring + coverage: none + + - name: Install Pint + run: composer global require laravel/pint + + - name: Run Pint + run: pint + + - name: Commit linted files + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "Fix coding style" + diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml deleted file mode 100644 index 96e9a2d..0000000 --- a/.github/workflows/php-cs-fixer.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Check & fix styling - -on: [push] - -jobs: - php-cs-fixer: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref }} - - - name: Run PHP CS Fixer - uses: docker://oskarstark/php-cs-fixer-ga - with: - args: --config=.php-cs-fixer.dist.php --allow-risky=yes - - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v5 - with: - commit_message: Fix styling diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index c144d21..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,67 +0,0 @@ -setRiskyAllowed(true) - ->setRules([ - '@PSR12' => true, - 'array_syntax' => ['syntax' => 'short'], - 'blank_line_before_statement' => true, - 'cast_spaces' => true, - 'combine_consecutive_unsets' => true, - 'concat_space' => ['spacing' => 'one'], - 'general_phpdoc_annotation_remove' => true, - 'linebreak_after_opening_tag' => true, - 'list_syntax' => ['syntax' => 'short'], - 'method_argument_space' => ['keep_multiple_spaces_after_comma' => false], - 'native_constant_invocation' => true, - 'new_with_parentheses' => true, - 'global_namespace_import' => true, - 'no_blank_lines_after_class_opening' => true, - 'no_blank_lines_after_phpdoc' => true, - 'no_break_comment' => false, - 'no_spaces_around_offset' => true, - 'no_trailing_comma_in_singleline' => true, - 'no_unused_imports' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'no_whitespace_before_comma_in_array' => true, - 'no_whitespace_in_blank_line' => true, - 'normalize_index_brace' => true, - 'not_operator_with_successor_space' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'phpdoc_add_missing_param_annotation' => true, - 'phpdoc_align' => true, - 'phpdoc_indent' => true, - 'phpdoc_separation' => true, - 'phpdoc_to_comment' => false, - 'phpdoc_trim' => true, - 'phpdoc_no_empty_return' => false, - 'protected_to_private' => false, - 'single_quote' => true, - 'single_line_throw' => false, - 'no_superfluous_phpdoc_tags' => false, - 'nullable_type_declaration' => true, - 'ternary_to_null_coalescing' => true, - 'trim_array_spaces' => true, - 'yoda_style' => false, - 'php_unit_method_casing' => true, - 'fully_qualified_strict_types' => [ - 'import_symbols' => true, - 'leading_backslash_in_global_namespace' => true, - 'phpdoc_tags' => [], - ], - // Rule to solve error "Expected 1 space after class keyword" - 'class_definition' => [ - 'space_before_parenthesis' => true, - ], - ]) - ->setFinder( - Finder::create() - ->exclude('vendor') - ->in(__DIR__) - ); diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..e0c889f --- /dev/null +++ b/pint.json @@ -0,0 +1,45 @@ +{ + "preset": "laravel", + "rules": { + "no_unused_imports": true, + "array_indentation": true, + "declare_strict_types": true, + "array_syntax": { + "syntax": "short" + }, + "blank_line_before_statement": { + "statements": [ + "break", + "declare", + "return", + "throw", + "try", + "while", + "for", + "if", + "continue", + "foreach" + ] + }, + "braces": { + "allow_single_line_anonymous_class_with_empty_body": false, + "allow_single_line_closure": false, + "position_after_anonymous_constructs": "same", + "position_after_control_structures": "same", + "position_after_functions_and_oop_constructs": "next" + }, + "cast_spaces": false, + "concat_space": { + "spacing": "one" + }, + "binary_operator_spaces": { + "operators": { + "=>": "align_single_space_minimal" + } + }, + "ordered_imports": { + "sort_algorithm": "length" + }, + "nullable_type_declaration_for_default_null_value": false + } +} 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 @@