From 907748a3c108ad0802a2fcfbdb40d30238d61210 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 22 Feb 2024 18:27:12 +0300 Subject: [PATCH] Enforce readonly modifier on class properties (#61) --- CHANGELOG.md | 2 + contracts/Authenticator/Authenticator.php | 4 +- contracts/Authorizer/Authorizer.php | 4 +- contracts/Client/Client.php | 40 +++++++++++--- contracts/HttpClient/HttpClient.php | 6 ++- contracts/Response/Response.php | 8 ++- src/Authenticator/CookieAuthenticator.php | 9 ++-- src/Authorizer/CookieAuthorizer.php | 7 +-- src/Authorizer/TokenAuthorizer.php | 7 +-- src/Client/YouTrackClient.php | 64 +++++++++++++++-------- src/HttpClient/GuzzleHttpClient.php | 19 ++++--- src/Response/YouTrackResponse.php | 12 +++-- 12 files changed, 127 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fc2660..1f9f0c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to `cybercog/youtrack-rest-php` will be documented in this f - ([#58]) Bumped minimum Guzzle version to v7 - ([#59]) Raised minimum PHP version to v8.1 - ([#60]) Enforced class properties strict types +- ([#61]) Enforced readonly modifier on class properties ## [7.0.0] - 2022-10-02 @@ -155,6 +156,7 @@ All notable changes to `cybercog/youtrack-rest-php` will be documented in this f [3.0.0]: https://github.com/cybercog/youtrack-rest-php/compare/2.0.1...3.0.0 [2.0.1]: https://github.com/cybercog/youtrack-rest-php/compare/1.0.0...2.0.1 +[#61]: https://github.com/cybercog/youtrack-rest-php/pull/61 [#60]: https://github.com/cybercog/youtrack-rest-php/pull/60 [#59]: https://github.com/cybercog/youtrack-rest-php/pull/59 [#58]: https://github.com/cybercog/youtrack-rest-php/pull/58 diff --git a/contracts/Authenticator/Authenticator.php b/contracts/Authenticator/Authenticator.php index b8cd702..85cea4b 100644 --- a/contracts/Authenticator/Authenticator.php +++ b/contracts/Authenticator/Authenticator.php @@ -22,7 +22,9 @@ interface Authenticator * * @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException */ - public function authenticate(ClientInterface $client): void; + public function authenticate( + ClientInterface $client, + ): void; /** * Retrieve authentication token. diff --git a/contracts/Authorizer/Authorizer.php b/contracts/Authorizer/Authorizer.php index 5fb8207..aa074f8 100644 --- a/contracts/Authorizer/Authorizer.php +++ b/contracts/Authorizer/Authorizer.php @@ -20,5 +20,7 @@ interface Authorizer /** * Append authorization headers to REST client. */ - public function appendHeadersTo(ClientInterface $client): void; + public function appendHeadersTo( + ClientInterface $client, + ): void; } diff --git a/contracts/Client/Client.php b/contracts/Client/Client.php index e735533..1a82784 100644 --- a/contracts/Client/Client.php +++ b/contracts/Client/Client.php @@ -35,7 +35,12 @@ interface Client * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function request(string $method, string $uri, array $params = [], array $options = []): ResponseInterface; + public function request( + string $method, + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface; /** * Create and send an GET HTTP request. @@ -49,7 +54,11 @@ public function request(string $method, string $uri, array $params = [], array $ * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function get(string $uri, array $params = [], array $options = []): ResponseInterface; + public function get( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface; /** * Create and send an POST HTTP request. @@ -63,7 +72,11 @@ public function get(string $uri, array $params = [], array $options = []): Respo * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function post(string $uri, array $params = [], array $options = []): ResponseInterface; + public function post( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface; /** * Create and send an PUT HTTP request. @@ -77,7 +90,11 @@ public function post(string $uri, array $params = [], array $options = []): Resp * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function put(string $uri, array $params = [], array $options = []): ResponseInterface; + public function put( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface; /** * Create and send an DELETE HTTP request. @@ -91,12 +108,19 @@ public function put(string $uri, array $params = [], array $options = []): Respo * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function delete(string $uri, array $params = [], array $options = []): ResponseInterface; + public function delete( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface; /** * Write header value. */ - public function withHeader(string $key, string $value): void; + public function withHeader( + string $key, + string $value, + ): void; /** * Write header values. @@ -104,5 +128,7 @@ public function withHeader(string $key, string $value): void; * @param array $headers * @return void */ - public function withHeaders(array $headers): void; + public function withHeaders( + array $headers, + ): void; } diff --git a/contracts/HttpClient/HttpClient.php b/contracts/HttpClient/HttpClient.php index 27161d3..1d0ed15 100644 --- a/contracts/HttpClient/HttpClient.php +++ b/contracts/HttpClient/HttpClient.php @@ -27,5 +27,9 @@ interface HttpClient * * @throws \Cog\Contracts\YouTrack\Rest\HttpClient\Exceptions\HttpClientException */ - public function request(string $method, string $uri, array $options = []): PsrResponseInterface; + public function request( + string $method, + string $uri, + array $options = [], + ): PsrResponseInterface; } diff --git a/contracts/Response/Response.php b/contracts/Response/Response.php index f330ea0..1edfc2b 100644 --- a/contracts/Response/Response.php +++ b/contracts/Response/Response.php @@ -33,7 +33,9 @@ public function statusCode(): int; /** * Retrieves a comma-separated string of the values for a single header. */ - public function header(string $header): string; + public function header( + string $header, + ): string; /** * Transform response cookie headers to string. @@ -60,7 +62,9 @@ public function toArray(): array; /** * Assert the status code of the response. */ - public function isStatusCode(int $code): bool; + public function isStatusCode( + int $code, + ): bool; /** * Determine if response has successful status code. diff --git a/src/Authenticator/CookieAuthenticator.php b/src/Authenticator/CookieAuthenticator.php index 3dd5d82..1dba6e4 100644 --- a/src/Authenticator/CookieAuthenticator.php +++ b/src/Authenticator/CookieAuthenticator.php @@ -27,8 +27,8 @@ class CookieAuthenticator implements private string $cookie = ''; public function __construct( - private string $username, - private string $password, + private readonly string $username, + private readonly string $password, ) { } @@ -37,8 +37,9 @@ public function __construct( * * @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException */ - public function authenticate(ClientInterface $client): void - { + public function authenticate( + ClientInterface $client, + ): void { if ($this->cookie !== '' || $this->isAuthenticating) { return; } diff --git a/src/Authorizer/CookieAuthorizer.php b/src/Authorizer/CookieAuthorizer.php index 0deb55e..aaab3ca 100644 --- a/src/Authorizer/CookieAuthorizer.php +++ b/src/Authorizer/CookieAuthorizer.php @@ -21,7 +21,7 @@ class CookieAuthorizer implements AuthorizerInterface { public function __construct( - private AuthenticatorInterface $authenticator, + private readonly AuthenticatorInterface $authenticator, ) { } @@ -30,8 +30,9 @@ public function __construct( * * @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException */ - public function appendHeadersTo(ClientInterface $client): void - { + public function appendHeadersTo( + ClientInterface $client, + ): void { $this->authenticator->authenticate($client); $client->withHeader('Cookie', $this->authenticator->token()); diff --git a/src/Authorizer/TokenAuthorizer.php b/src/Authorizer/TokenAuthorizer.php index d79daef..7417300 100644 --- a/src/Authorizer/TokenAuthorizer.php +++ b/src/Authorizer/TokenAuthorizer.php @@ -23,15 +23,16 @@ class TokenAuthorizer implements AuthorizerInterface { public function __construct( - private string $token, + private readonly string $token, ) { } /** * Append authorization headers to REST client. */ - public function appendHeadersTo(ClientInterface $client): void - { + public function appendHeadersTo( + ClientInterface $client, + ): void { $client->withHeader('Authorization', "Bearer {$this->token}"); } } diff --git a/src/Client/YouTrackClient.php b/src/Client/YouTrackClient.php index 41d0e5f..7c8a4d9 100644 --- a/src/Client/YouTrackClient.php +++ b/src/Client/YouTrackClient.php @@ -39,13 +39,13 @@ class YouTrackClient implements /** * Request headers. * - * @var array + * @var array */ private array $headers = []; public function __construct( - private HttpClientInterface $httpClient, - private AuthorizerInterface $authorizer, + private readonly HttpClientInterface $httpClient, + private readonly AuthorizerInterface $authorizer, string | null $endpointPathPrefix = null, ) { $this->endpointPathPrefix = $endpointPathPrefix !== null @@ -66,8 +66,12 @@ public function __construct( * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function request(string $method, string $uri, array $params = [], array $options = []): ResponseInterface - { + public function request( + string $method, + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface { try { $response = $this->httpClient->request( $method, @@ -100,8 +104,11 @@ public function request(string $method, string $uri, array $params = [], array $ * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function get(string $uri, array $params = [], array $options = []): ResponseInterface - { + public function get( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface { return $this->request('GET', $uri, $params, $options); } @@ -117,8 +124,11 @@ public function get(string $uri, array $params = [], array $options = []): Respo * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function post(string $uri, array $params = [], array $options = []): ResponseInterface - { + public function post( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface { return $this->request('POST', $uri, $params, $options); } @@ -134,8 +144,11 @@ public function post(string $uri, array $params = [], array $options = []): Resp * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function put(string $uri, array $params = [], array $options = []): ResponseInterface - { + public function put( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface { return $this->request('PUT', $uri, $params, $options); } @@ -151,16 +164,21 @@ public function put(string $uri, array $params = [], array $options = []): Respo * @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken * @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException */ - public function delete(string $uri, array $params = [], array $options = []): ResponseInterface - { + public function delete( + string $uri, + array $params = [], + array $options = [], + ): ResponseInterface { return $this->request('DELETE', $uri, $params, $options); } /** * Write header value. */ - public function withHeader(string $key, string $value): void - { + public function withHeader( + string $key, + string $value, + ): void { $this->headers[$key] = $value; } @@ -170,16 +188,18 @@ public function withHeader(string $key, string $value): void * @param array $headers * @return void */ - public function withHeaders(array $headers): void - { + public function withHeaders( + array $headers, + ): void { $this->headers = array_merge_recursive($this->headers, $headers); } /** * Build endpoint URI. */ - protected function buildUri(string $uri): string - { + protected function buildUri( + string $uri, + ): string { return $this->endpointPathPrefix . '/' . ltrim($uri, '/'); } @@ -190,8 +210,10 @@ protected function buildUri(string $uri): string * @param array $options * @return array */ - protected function buildOptions(array $params = [], array $options = []): array - { + protected function buildOptions( + array $params = [], + array $options = [], + ): array { $defaultOptions = [ 'form_params' => $params, 'headers' => $this->buildHeaders(), diff --git a/src/HttpClient/GuzzleHttpClient.php b/src/HttpClient/GuzzleHttpClient.php index c3c9d69..c9a6b80 100644 --- a/src/HttpClient/GuzzleHttpClient.php +++ b/src/HttpClient/GuzzleHttpClient.php @@ -26,7 +26,7 @@ class GuzzleHttpClient implements HttpClientInterface { public function __construct( - private ClientInterface $httpClient, + private readonly ClientInterface $httpClient, ) { } @@ -40,8 +40,11 @@ public function __construct( * * @throws \Cog\Contracts\YouTrack\Rest\HttpClient\Exceptions\HttpClientException */ - public function request(string $method, string $uri, array $options = []): ResponseInterface - { + public function request( + string $method, + string $uri, + array $options = [], + ): ResponseInterface { try { return $this->httpClient->request($method, $uri, $this->buildOptions($options)); } catch (BadResponseException | RequestException $e) { @@ -61,8 +64,9 @@ public function request(string $method, string $uri, array $options = []): Respo * @param array $options * @return array */ - private function buildOptions(array $options): array - { + private function buildOptions( + array $options, + ): array { return $this->appendUserAgent($options); } @@ -72,8 +76,9 @@ private function buildOptions(array $options): array * @param array $options * @return array */ - private function appendUserAgent(array $options): array - { + private function appendUserAgent( + array $options, + ): array { $defaultAgent = Utils::defaultUserAgent(); if (extension_loaded('curl') && function_exists('curl_version')) { $curlVersion = \curl_version(); diff --git a/src/Response/YouTrackResponse.php b/src/Response/YouTrackResponse.php index 90b8a4e..d0595b0 100644 --- a/src/Response/YouTrackResponse.php +++ b/src/Response/YouTrackResponse.php @@ -20,7 +20,7 @@ class YouTrackResponse implements ResponseInterface { public function __construct( - private PsrResponseInterface $response, + private readonly PsrResponseInterface $response, ) { } @@ -46,8 +46,9 @@ public function statusCode(): int /** * Retrieves a comma-separated string of the values for a single header. */ - public function header(string $header): string - { + public function header( + string $header, + ): string { return $this->response->getHeaderLine($header); } @@ -88,8 +89,9 @@ public function toArray(): array /** * Assert the status code of the response. */ - public function isStatusCode(int $code): bool - { + public function isStatusCode( + int $code, + ): bool { return $this->response->getStatusCode() === $code; }