From 20bc9efc68fe637460685d6238f7079208492a63 Mon Sep 17 00:00:00 2001 From: andriusbaliutis Date: Thu, 1 Aug 2024 16:54:29 +0200 Subject: [PATCH] Upgrade library for PHP 8 compatibility --- composer.json | 2 +- src/BasicHttpClient.php | 63 +----- .../HttpRequestAuthenticationException.php | 2 + src/Exception/HttpRequestException.php | 2 + src/Exception/HttpRequestMessageException.php | 2 + src/Exception/HttpResponseException.php | 2 + src/HttpClientInterface.php | 50 ++--- src/Request/AbstractRequest.php | 189 +++--------------- .../AuthenticationInterface.php | 9 +- .../Authentication/BasicAuthentication.php | 58 ++---- .../ClientCertificateAuthentication.php | 87 ++------ .../Base/CurlConfiguratorInterface.php | 8 +- src/Request/Message/Body/Body.php | 38 +--- src/Request/Message/Body/BodyInterface.php | 2 + src/Request/Message/Cookie/Cookie.php | 38 +--- .../Message/Cookie/CookieInterface.php | 22 +- src/Request/Message/Header/Header.php | 33 +-- .../Message/Header/HeaderInterface.php | 32 +-- src/Request/Message/Message.php | 156 +++------------ src/Request/Message/MessageInterface.php | 114 ++--------- src/Request/Request.php | 5 +- src/Request/RequestInterface.php | 103 ++-------- src/Request/Transport/HttpTransport.php | 115 +++-------- src/Request/Transport/HttpsTransport.php | 28 +-- src/Request/Transport/TransportInterface.php | 56 +----- src/Response/AbstractResponse.php | 110 ++-------- src/Response/Header/Header.php | 33 +-- src/Response/Response.php | 2 + src/Response/ResponseInterface.php | 42 +--- src/Response/Statistics/Statistics.php | 53 ++--- src/Util/HeaderNameUtil.php | 2 + 31 files changed, 280 insertions(+), 1178 deletions(-) diff --git a/composer.json b/composer.json index b426581..5c2cf62 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } }, "require": { - "php": "^7.1 || ^8.3", + "php": "^8.3", "ext-curl": "*", "ext-mbstring": "*", "chroma-x/url-util": "~2.0", diff --git a/src/BasicHttpClient.php b/src/BasicHttpClient.php index 31eca67..607af63 100644 --- a/src/BasicHttpClient.php +++ b/src/BasicHttpClient.php @@ -1,5 +1,7 @@ setUrl(new Url($endpoint)); } - /** - * @return RequestInterface - */ public function getRequest(): RequestInterface { return $this->request; } - /** - * @param mixed[] $queryParameters - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function get(array $queryParameters = array()): ResponseInterface + public function get(array $queryParameters = []): ResponseInterface { $this->request ->setMethod(RequestInterface::REQUEST_METHOD_GET) @@ -69,13 +54,7 @@ public function get(array $queryParameters = array()): ResponseInterface return $this->request->getResponse(); } - /** - * @param mixed[] $queryParameters - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function head(array $queryParameters = array()): ResponseInterface + public function head(array $queryParameters = []): ResponseInterface { $this->request ->setMethod(RequestInterface::REQUEST_METHOD_HEAD) @@ -85,13 +64,7 @@ public function head(array $queryParameters = array()): ResponseInterface return $this->request->getResponse(); } - /** - * @param array $postData - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function post(array $postData = array()): ResponseInterface + public function post(array $postData = []): ResponseInterface { $body = new Body(); $body->setBodyTextFromArray($postData); @@ -104,13 +77,7 @@ public function post(array $postData = array()): ResponseInterface return $this->request->getResponse(); } - /** - * @param array $putData - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function put(array $putData = array()): ResponseInterface + public function put(array $putData = []): ResponseInterface { $body = new Body(); $body->setBodyTextFromArray($putData); @@ -123,13 +90,7 @@ public function put(array $putData = array()): ResponseInterface return $this->request->getResponse(); } - /** - * @param array $patchData - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function patch(array $patchData = array()): ResponseInterface + public function patch(array $patchData = []): ResponseInterface { $body = new Body(); $body->setBodyTextFromArray($patchData); @@ -142,13 +103,7 @@ public function patch(array $patchData = array()): ResponseInterface return $this->request->getResponse(); } - /** - * @param mixed[] $queryParameters - * @return ResponseInterface - * @throws NetworkException - * @throws ConnectionTimeoutException - */ - public function delete(array $queryParameters = array()): ResponseInterface + public function delete(array $queryParameters = []): ResponseInterface { $this->request ->setMethod(RequestInterface::REQUEST_METHOD_DELETE) diff --git a/src/Exception/HttpRequestAuthenticationException.php b/src/Exception/HttpRequestAuthenticationException.php index 0100424..9af7fca 100644 --- a/src/Exception/HttpRequestAuthenticationException.php +++ b/src/Exception/HttpRequestAuthenticationException.php @@ -1,5 +1,7 @@ transport = new HttpTransport(); } - /** - * @return string - */ public function getUserAgent(): string { return $this->userAgent; } - /** - * @param string $userAgent - * @return $this - */ - public function setUserAgent(string $userAgent) + public function setUserAgent(string $userAgent): self { $this->userAgent = $userAgent; return $this; } - /** - * @return string - */ public function getMethod(): string { return $this->method; } - /** - * @param string $method - * @return $this - */ - public function setMethod(string $method) + public function setMethod(string $method): self { $this->method = $method; return $this; } - /** - * @return UrlInterface - */ public function getUrl(): ?UrlInterface { return $this->url; } - /** - * @param UrlInterface $url - * @return $this - */ - public function setUrl(UrlInterface $url) + public function setUrl(UrlInterface $url): self { $this->url = $url; return $this; } - /** - * @return TransportInterface - */ public function getTransport(): ?TransportInterface { return $this->transport; } - /** - * @param TransportInterface $transport - * @return $this - */ - public function setTransport(TransportInterface $transport) + public function setTransport(TransportInterface $transport): self { $this->transport = $transport; return $this; } - /** - * @return MessageInterface - */ public function getMessage(): ?MessageInterface { return $this->message; } - /** - * @param MessageInterface $message - * @return $this - */ - public function setMessage(MessageInterface $message) + public function setMessage(MessageInterface $message): self { $this->message = $message; return $this; @@ -186,19 +125,14 @@ public function getAuthentications(): array /** * @param AuthenticationInterface[] $authentications - * @return $this */ - public function setAuthentications(array $authentications) + public function setAuthentications(array $authentications): self { $this->authentications = $authentications; return $this; } - /** - * @param AuthenticationInterface $authentication - * @return $this - */ - public function addAuthentication(AuthenticationInterface $authentication) + public function addAuthentication(AuthenticationInterface $authentication): self { if (!$this->hasAuthentication($authentication)) { $this->authentications[] = $authentication; @@ -206,11 +140,7 @@ public function addAuthentication(AuthenticationInterface $authentication) return $this; } - /** - * @param AuthenticationInterface $authentication - * @return $this - */ - public function removeAuthentication(AuthenticationInterface $authentication) + public function removeAuthentication(AuthenticationInterface $authentication): self { $authenticationCount = count($this->authentications); for ($i = 0; $i < $authenticationCount; $i++) { @@ -223,46 +153,28 @@ public function removeAuthentication(AuthenticationInterface $authentication) return $this; } - /** - * @param AuthenticationInterface $authentication - * @return bool - */ public function hasAuthentication(AuthenticationInterface $authentication): bool { - foreach ($this->authentications as $existingAuth) { - if ($authentication === $existingAuth) { - return true; - } + if (in_array($authentication, $this->authentications, true)) { + return true; } return false; } - /** - * @return bool - */ public function hasAuthentications(): bool { return count($this->authentications) > 0; } - /** - * @return int - */ public function countAuthentications(): int { return count($this->authentications); } - /** - * @param resource $curl - * @return $this - * @throws HttpRequestException - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } $url = $this->getUrl(); if ($url === null) { @@ -285,13 +197,7 @@ public function configureCurl($curl) return $this; } - /** - * @return $this - * @throws ConnectionTimeoutException - * @throws NetworkException - * @throws \Exception - */ - public function perform() + public function perform(): self { // Reset former result $this->response = null; @@ -333,67 +239,42 @@ public function perform() throw new CurlException('The request failed with message: ' . $curlErrorMessage); } - /** - * @return ResponseInterface - */ abstract protected function buildResponse(): ResponseInterface; - /** - * @return ResponseInterface - */ public function getResponse(): ?ResponseInterface { return $this->response; } - /** - * @return string - */ public function getEffectiveStatus(): ?string { return $this->effectiveStatus; } - /** - * @return string - */ public function getEffectiveEndpoint(): ?string { return $this->effectiveEndpoint; } - /** - * @return string - */ public function getEffectiveRawHeader(): ?string { return $this->effectiveRawHeader; } /** - * @return Header[] + * @return ?Header[] */ public function getEffectiveHeaders(): ?array { return $this->effectiveHeaders; } - /** - * @return string - */ protected function calculateEndpoint(): ?string { $url = $this->getUrl(); - if ($url === null) { - return null; - } - return $url->buildUrl(); + return $url?->buildUrl(); } - /** - * @return void - * @throws \Exception - */ protected function prePerform(): void { $url = $this->getUrl(); @@ -408,11 +289,7 @@ protected function prePerform(): void } } - /** - * @param resource $curl - * @return $this - */ - private function setEffectiveProperties($curl) + private function setEffectiveProperties(\CurlHandle $curl): self { $this->effectiveEndpoint = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); $this->effectiveRawHeader = curl_getinfo($curl, CURLINFO_HEADER_OUT); @@ -420,17 +297,17 @@ private function setEffectiveProperties($curl) $requestHeaders = preg_split( '/\r\n/', $this->effectiveRawHeader, - null, + 0, PREG_SPLIT_NO_EMPTY ); foreach ($requestHeaders as $requestHeader) { - if (strpos($requestHeader, ':') !== false) { + if (str_contains($requestHeader, ':')) { $headerName = mb_substr($requestHeader, 0, strpos($requestHeader, ':')); $headerValue = mb_substr($requestHeader, strpos($requestHeader, ':') + 1); $headerValues = explode(',', $headerValue); $this->effectiveHeaders[] = new Header($headerName, $headerValues); } - if (strpos($requestHeader, ':') === false) { + if (!str_contains($requestHeader, ':')) { $this->effectiveStatus = $requestHeader; } } diff --git a/src/Request/Authentication/AuthenticationInterface.php b/src/Request/Authentication/AuthenticationInterface.php index 49a0759..56b27bf 100644 --- a/src/Request/Authentication/AuthenticationInterface.php +++ b/src/Request/Authentication/AuthenticationInterface.php @@ -1,5 +1,7 @@ username = $username; $this->password = $password; } - /** - * @return string - */ - public function getUsername():string + public function getUsername(): string { return $this->username; } - /** - * @param string $username - * @return $this - */ - public function setUsername(string $username) + public function setUsername(string $username): self { $this->username = $username; return $this; } - /** - * @return string - */ - public function getPassword():string + public function getPassword(): string { return $this->password; } - /** - * @param string $password - * @return $this - */ - public function setPassword(string $password) + public function setPassword(string $password): self { $this->password = $password; return $this; } - /** - * @param RequestInterface $request - * @return $this - */ - public function validate(RequestInterface $request) + public function validate(RequestInterface $request): self { return $this; } - /** - * @param resource $curl - * @return $this - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); diff --git a/src/Request/Authentication/ClientCertificateAuthentication.php b/src/Request/Authentication/ClientCertificateAuthentication.php index 4239630..0312305 100644 --- a/src/Request/Authentication/ClientCertificateAuthentication.php +++ b/src/Request/Authentication/ClientCertificateAuthentication.php @@ -1,5 +1,7 @@ setCaCertPath($caCertPath); @@ -47,21 +31,12 @@ public function __construct(string $caCertPath, string $clientCertPath, string $ $this->setClientCertPassword($clientCertPassword); } - /** - * @return string - */ - public function getCaCertPath():string + public function getCaCertPath(): string { return $this->caCertPath; } - /** - * @param string $caCertPath - * @return $this - * @throws FileNotFoundException - * @throws FileReadableException - */ - public function setCaCertPath(string $caCertPath) + public function setCaCertPath(string $caCertPath): self { if (!is_file($caCertPath)) { throw new FileNotFoundException('CA certificate file not found.'); @@ -73,21 +48,12 @@ public function setCaCertPath(string $caCertPath) return $this; } - /** - * @return string - */ - public function getClientCertPath():string + public function getClientCertPath(): string { return $this->clientCertPath; } - /** - * @param string $clientCertPath - * @return $this - * @throws FileNotFoundException - * @throws FileReadableException - */ - public function setClientCertPath(string $clientCertPath) + public function setClientCertPath(string $clientCertPath): self { if (!is_file($clientCertPath)) { throw new FileNotFoundException('Client certificate file not found.'); @@ -99,30 +65,18 @@ public function setClientCertPath(string $clientCertPath) return $this; } - /** - * @return string - */ - public function getClientCertPassword():string + public function getClientCertPassword(): string { return $this->clientCertPassword; } - /** - * @param string $clientCertPassword - * @return $this - */ - public function setClientCertPassword(string $clientCertPassword) + public function setClientCertPassword(string $clientCertPassword): self { $this->clientCertPassword = $clientCertPassword; return $this; } - /** - * @param RequestInterface $request - * @return $this - * @throws HttpRequestAuthenticationException - */ - public function validate(RequestInterface $request) + public function validate(RequestInterface $request): self { if (!$request->getTransport() instanceof HttpsTransport) { throw new HttpRequestAuthenticationException( @@ -132,15 +86,10 @@ public function validate(RequestInterface $request) return $this; } - /** - * @param resource $curl - * @return mixed - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } curl_setopt($curl, CURLOPT_CAINFO, $this->caCertPath); curl_setopt($curl, CURLOPT_SSLCERT, $this->clientCertPath); diff --git a/src/Request/Base/CurlConfiguratorInterface.php b/src/Request/Base/CurlConfiguratorInterface.php index dc168d2..e831f38 100644 --- a/src/Request/Base/CurlConfiguratorInterface.php +++ b/src/Request/Base/CurlConfiguratorInterface.php @@ -1,5 +1,7 @@ bodyText = $bodyText; } - /** - * @return string - */ public function getBodyText(): ?string { return $this->bodyText; } - /** - * @param string $bodyText - * @return $this - */ - public function setBodyText(string $bodyText) + public function setBodyText(string $bodyText): self { $this->bodyText = $bodyText; return $this; } - /** - * @param array $bodyData - * @return $this - */ - public function setBodyTextFromArray(array $bodyData) + public function setBodyTextFromArray(array $bodyData): self { $this->bodyText = http_build_query($bodyData); return $this; } - /** - * @param resource $curl - * @return $this - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } curl_setopt($curl, CURLOPT_POSTFIELDS, $this->bodyText); return $this; diff --git a/src/Request/Message/Body/BodyInterface.php b/src/Request/Message/Body/BodyInterface.php index 40c0a84..cc70e32 100644 --- a/src/Request/Message/Body/BodyInterface.php +++ b/src/Request/Message/Body/BodyInterface.php @@ -1,5 +1,7 @@ name = $name; $this->value = $value; } - /** - * @return string - */ public function getName(): string { return $this->name; } - /** - * @param string $name - * @return $this - */ - public function setName(string $name) + + public function setName(string $name): self { $this->name = $name; return $this; } - /** - * @return string - */ + public function getValue(): string { return $this->value; } - /** - * @param string $value - * @return $this - */ - public function setValue(string $value) + public function setValue(string $value): self { $this->value = $value; return $this; diff --git a/src/Request/Message/Cookie/CookieInterface.php b/src/Request/Message/Cookie/CookieInterface.php index d0dd768..7793500 100644 --- a/src/Request/Message/Cookie/CookieInterface.php +++ b/src/Request/Message/Cookie/CookieInterface.php @@ -1,5 +1,7 @@ setName($name); $this->setValues($values); } - /** - * @return string - */ public function getName(): string { return $this->name; } - /** - * @return string - */ public function getNormalizedName(): string { $headerNameUtil = new HeaderNameUtil(); return $headerNameUtil->normalizeHeaderName($this->name); } - /** - * @param string $name - * @return $this - */ - public function setName(string $name) + public function setName(string $name): self { $this->name = trim($name); return $this; @@ -69,9 +52,6 @@ public function getValues(): array return $this->values; } - /** - * @return string - */ public function getValuesAsString(): string { return implode(', ', $this->values); @@ -79,9 +59,8 @@ public function getValuesAsString(): string /** * @param string[] $values - * @return $this */ - public function setValues(array $values) + public function setValues(array $values): self { foreach ($values as $value) { if (!is_string($value)) { diff --git a/src/Request/Message/Header/HeaderInterface.php b/src/Request/Message/Header/HeaderInterface.php index 3b3bd4a..4304864 100644 --- a/src/Request/Message/Header/HeaderInterface.php +++ b/src/Request/Message/Header/HeaderInterface.php @@ -1,5 +1,7 @@ findHeadersByName($name); } - /** - * @param string $name - * @return HeaderInterface - */ public function getHeaderByName(string $name): ?HeaderInterface { if (!$this->hasHeaderWithName($name)) { @@ -60,10 +55,7 @@ public function getHeaderByName(string $name): ?HeaderInterface return $matchingHeaders[0]; } - /** - * @return $this - */ - public function clearHeaders() + public function clearHeaders(): self { $this->headers = array(); return $this; @@ -71,9 +63,8 @@ public function clearHeaders() /** * @param HeaderInterface[] $headers - * @return $this */ - public function setHeaders(array $headers) + public function setHeaders(array $headers): self { foreach ($headers as $header) { if (!$header instanceof HeaderInterface) { @@ -85,42 +76,26 @@ public function setHeaders(array $headers) return $this; } - /** - * @param HeaderInterface $header - * @return $this - */ - public function addHeader(HeaderInterface $header) + public function addHeader(HeaderInterface $header): self { $this->headers[] = $header; return $this; } - /** - * @param HeaderInterface $header - * @return $this - */ - public function setHeader(HeaderInterface $header) + public function setHeader(HeaderInterface $header): self { $this->removeHeadersByName($header->getName()); $this->headers[] = $header; return $this; } - /** - * @param string $name - * @return $this - */ - public function removeHeadersByName(string $name) + public function removeHeadersByName(string $name): self { $this->headers = $this->findHeadersExcludedByName($name); return $this; } - /** - * @param HeaderInterface $header - * @return $this - */ - public function removeHeader(HeaderInterface $header) + public function removeHeader(HeaderInterface $header): self { if (!$this->hasHeader($header)) { return $this; @@ -130,52 +105,31 @@ public function removeHeader(HeaderInterface $header) return $this; } - /** - * @param $name - * @return bool - */ public function hasHeaderWithName(string $name): bool { return count($this->findHeadersByName($name)) > 0; } - /** - * @param HeaderInterface $header - * @return bool - */ public function hasHeader(HeaderInterface $header): bool { return !is_null($this->findHeaderIndex($header)); } - /** - * @return bool - */ public function hasHeaders(): bool { return count($this->headers) > 0; } - /** - * @return int - */ public function getHeaderCount(): int { - return (int)count($this->headers); + return count($this->headers); } - /** - * @return CookieInterface[] - */ public function getCookies(): array { return $this->cookies; } - /** - * @param $name - * @return CookieInterface - */ public function getCookieByName(string $name): ?CookieInterface { foreach ($this->cookies as $cookie) { @@ -186,10 +140,7 @@ public function getCookieByName(string $name): ?CookieInterface return null; } - /** - * @return $this - */ - public function clearCookies() + public function clearCookies(): self { $this->cookies = array(); return $this; @@ -197,9 +148,8 @@ public function clearCookies() /** * @param CookieInterface[] $cookies - * @return $this */ - public function setCookies(array $cookies) + public function setCookies(array $cookies): self { foreach ($cookies as $cookie) { if (!$cookie instanceof CookieInterface) { @@ -211,21 +161,13 @@ public function setCookies(array $cookies) return $this; } - /** - * @param CookieInterface $cookie - * @return $this - */ - public function addCookie(CookieInterface $cookie) + public function addCookie(CookieInterface $cookie): self { $this->cookies[] = $cookie; return $this; } - /** - * @param string $name - * @return $this - */ - public function removeCookieByName(string $name) + public function removeCookieByName(string $name): self { $cookieCount = count($this->cookies); for ($i = 0; $i < $cookieCount; $i++) { @@ -238,11 +180,7 @@ public function removeCookieByName(string $name) return $this; } - /** - * @param CookieInterface $cookie - * @return $this - */ - public function removeCookie(CookieInterface $cookie) + public function removeCookie(CookieInterface $cookie): self { $cookieCount = count($this->cookies); for ($i = 0; $i < $cookieCount; $i++) { @@ -255,10 +193,6 @@ public function removeCookie(CookieInterface $cookie) return $this; } - /** - * @param $name - * @return bool - */ public function hasCookieWithName(string $name): bool { foreach ($this->cookies as $cookie) { @@ -269,80 +203,50 @@ public function hasCookieWithName(string $name): bool return false; } - /** - * @param CookieInterface $cookie - * @return bool - */ public function hasCookie(CookieInterface $cookie): bool { - foreach ($this->cookies as $existingCookie) { - if ($existingCookie === $cookie) { - return true; - } + if (in_array($cookie, $this->cookies, true)) { + return true; } return false; } - /** - * @return bool - */ public function hasCookies(): bool { return count($this->cookies) > 0; } - /** - * @return int - */ public function getCookieCount(): int { - return (int)count($this->cookies); + return count($this->cookies); } - /** - * @return BodyInterface - */ public function getBody(): ?BodyInterface { return $this->body; } - /** - * @param BodyInterface $body - * @return $this - */ - public function setBody(BodyInterface $body) + public function setBody(BodyInterface $body): self { $this->body = $body; return $this; } - /** - * @return bool - */ public function hasBody(): bool { return !is_null($this->body); } - /** - * @return $this - */ - public function removeBody() + public function removeBody(): self { $this->body = null; return $this; } - /** - * @param resource $curl - * @return $this - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } // Add request headers if ($this->hasHeaders()) { @@ -363,14 +267,11 @@ public function configureCurl($curl) } // Setup body $body = $this->getBody(); - if ($body !== null) { - $body->configureCurl($curl); - } + $body?->configureCurl($curl); return $this; } /** - * @param string $name * @return HeaderInterface[] */ private function findHeadersByName(string $name): array @@ -386,10 +287,6 @@ private function findHeadersByName(string $name): array return $matchingHeaders; } - /** - * @param HeaderInterface $header - * @return int - */ private function findHeaderIndex(HeaderInterface $header): ?int { $headerCount = count($this->headers); @@ -402,7 +299,6 @@ private function findHeaderIndex(HeaderInterface $header): ?int } /** - * @param string $name * @return HeaderInterface[] */ private function findHeadersExcludedByName(string $name): array diff --git a/src/Request/Message/MessageInterface.php b/src/Request/Message/MessageInterface.php index 367b0f7..1b60ee3 100644 --- a/src/Request/Message/MessageInterface.php +++ b/src/Request/Message/MessageInterface.php @@ -1,5 +1,7 @@ httpVersion; } - /** - * @param int $httpVersion - * @return $this - */ - public function setHttpVersion(int $httpVersion) + public function setHttpVersion(int $httpVersion): self { $this->httpVersion = $httpVersion; return $this; } - /** - * @return int - */ public function getTimeout(): int { return $this->timeout; } - /** - * @param int $timeout - * @return $this - */ - public function setTimeout(int $timeout) + public function setTimeout(int $timeout): self { $this->timeout = $timeout; return $this; } - /** - * @return bool - */ public function getReuseConnection(): bool { return $this->reuseConnection; } - /** - * @param bool $reuseConnection - * @return $this - */ - public function setReuseConnection(bool $reuseConnection) + public function setReuseConnection(bool $reuseConnection): self { $this->reuseConnection = $reuseConnection; return $this; } - /** - * @return bool - */ public function getAllowCaching(): bool { return $this->allowCaching; } - /** - * @param bool $allowCaching - * @return $this - */ - public function setAllowCaching(bool $allowCaching) + public function setAllowCaching(bool $allowCaching): self { $this->allowCaching = $allowCaching; return $this; } - /** - * @return bool - */ public function getFollowRedirects(): bool { return $this->followRedirects; } - /** - * @param bool $followRedirects - * @return $this - */ - public function setFollowRedirects(bool $followRedirects) + public function setFollowRedirects(bool $followRedirects): self { $this->followRedirects = $followRedirects; return $this; } - /** - * @return int - */ public function getMaxRedirects(): int { return $this->maxRedirects; } - /** - * @param int $maxRedirects - * @return $this - */ - public function setMaxRedirects(int $maxRedirects) + public function setMaxRedirects(int $maxRedirects): self { $this->maxRedirects = $maxRedirects; return $this; } - /** - * @param resource $curl - * @return $this - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } // HTTP version curl_setopt($curl, CURLOPT_HTTP_VERSION, $this->getHttpVersion()); diff --git a/src/Request/Transport/HttpsTransport.php b/src/Request/Transport/HttpsTransport.php index 60a7f18..ceed1d6 100644 --- a/src/Request/Transport/HttpsTransport.php +++ b/src/Request/Transport/HttpsTransport.php @@ -1,5 +1,7 @@ verifyHost; } - /** - * @param bool $verifyHost - */ - public function setVerifyHost(bool $verifyHost) + public function setVerifyHost(bool $verifyHost): self { $this->verifyHost = $verifyHost; return $this; } - /** - * @return bool - */ public function getVerifyPeer(): bool { return $this->verifyPeer; } - /** - * @param bool $verifyPeer - * @return $this - */ - public function setVerifyPeer(bool $verifyPeer) + public function setVerifyPeer(bool $verifyPeer): self { $this->verifyPeer = $verifyPeer; return $this; } - /** - * @param resource $curl - * @return $this - */ - public function configureCurl($curl) + public function configureCurl(\CurlHandle|false $curl): self { parent::configureCurl($curl); // Verify host diff --git a/src/Request/Transport/TransportInterface.php b/src/Request/Transport/TransportInterface.php index b1402ab..8eec03a 100644 --- a/src/Request/Transport/TransportInterface.php +++ b/src/Request/Transport/TransportInterface.php @@ -1,5 +1,7 @@ request = $request; } - /** - * @param resource $curl - * @param string $responseBody - * @return $this - */ - public function populateFromCurlResult($curl, string $responseBody) + public function populateFromCurlResult(\CurlHandle|false $curl, string $responseBody): self { - if (!is_resource($curl)) { - $argumentType = (is_object($curl)) ? get_class($curl) : gettype($curl); - throw new \TypeError('curl argument invalid. Expected a valid resource. Got ' . $argumentType); + if ($curl === false) { + throw new \TypeError('cURL is not a valid CurlHandle class.'); } $this->statusCode = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE); $this->setStatistics($curl); @@ -72,51 +48,34 @@ public function populateFromCurlResult($curl, string $responseBody) return $this; } - /** - * @return RequestInterface - */ public function getRequest(): RequestInterface { return $this->request; } - /** - * @return int - */ public function getStatusCode(): ?int { return $this->statusCode; } - /** - * @return string - */ public function getStatusText(): ?string { return $this->statusText; } /** - * @return Header[] + * @return ?Header[] */ public function getHeaders(): ?array { return $this->headers; } - /** - * @param string $name - * @return bool - */ public function hasHeader(string $name): bool { return !is_null($this->getHeader($name)); } - /** - * @param string $name - * @return Header - */ public function getHeader(string $name): ?Header { $headers = $this->getHeaders(); @@ -130,38 +89,24 @@ public function getHeader(string $name): ?Header return null; } - /** - * @return mixed - */ - public function getBody() + public function getBody(): mixed { return $this->body; } - /** - * @return Statistics - */ public function getStatistics(): ?Statistics { return $this->statistics; } - /** - * @param resource $curl - * @return $this - */ - protected function setStatistics($curl) + protected function setStatistics(\CurlHandle $curl): self { $this->statistics = new Statistics(); $this->statistics->populateFromCurlResult($curl); return $this; } - /** - * @param string $responseBody - * @return $this - */ - protected function setResponseData(string $responseBody) + protected function setResponseData(string $responseBody): self { // Parse response $responseHeaders = array(); @@ -185,7 +130,7 @@ protected function setResponseData(string $responseBody) || !$responseStatusCode >= 400 ) ); - $responseHeaders = preg_split('/\r\n/', $responseHeader, null, PREG_SPLIT_NO_EMPTY); + $responseHeaders = preg_split('/\r\n/', $responseHeader, 0, PREG_SPLIT_NO_EMPTY); } $this->setResponseHeader($responseHeaders); if (!is_null($responseStatusCode)) { @@ -198,31 +143,19 @@ protected function setResponseData(string $responseBody) return $this; } - /** - * @param int $statusCode - * @return $this - */ - protected function setStatusCode(int $statusCode) + protected function setStatusCode(int $statusCode): self { $this->statusCode = $statusCode; return $this; } - /** - * @param string $statusText - * @return $this - */ - protected function setStatusText(string $statusText) + protected function setStatusText(string $statusText): self { $this->statusText = $statusText; return $this; } - /** - * @param mixed $body - * @return $this - */ - protected function setBody($body) + protected function setBody(mixed $body): self { $this->body = $body; return $this; @@ -230,12 +163,11 @@ protected function setBody($body) /** * @param string[] $responseHeaders - * @return $this */ - protected function setResponseHeader(array $responseHeaders) + protected function setResponseHeader(array $responseHeaders): self { foreach ($responseHeaders as $responseHeader) { - if (strpos($responseHeader, ':') !== false) { + if (str_contains($responseHeader, ':')) { $headerName = mb_substr($responseHeader, 0, strpos($responseHeader, ':')); $headerValue = mb_substr($responseHeader, strpos($responseHeader, ':') + 1); $headerValues = explode(',', $headerValue); diff --git a/src/Response/Header/Header.php b/src/Response/Header/Header.php index 27fc1e7..e27171f 100644 --- a/src/Response/Header/Header.php +++ b/src/Response/Header/Header.php @@ -1,5 +1,7 @@ setName($name); $this->setValues($values); } - /** - * @return string - */ public function getName(): string { return $this->name; } - /** - * @return string - */ public function getNormalizedName(): string { $headerNameUtil = new HeaderNameUtil(); return $headerNameUtil->normalizeHeaderName($this->name); } - /** - * @param string $name - * @return $this - */ - private function setName(string $name) + private function setName(string $name): self { $this->name = trim($name); return $this; @@ -69,9 +52,6 @@ public function getValues(): array return $this->values; } - /** - * @return string - */ public function getValuesAsString(): string { return implode(', ', $this->values); @@ -79,9 +59,8 @@ public function getValuesAsString(): string /** * @param string[] $values - * @return $this */ - private function setValues(array $values) + private function setValues(array $values): self { foreach ($values as $value) { if (!is_string($value)) { diff --git a/src/Response/Response.php b/src/Response/Response.php index c869af7..a869be4 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -1,5 +1,7 @@ totalTime = curl_getinfo($curl, CURLINFO_TOTAL_TIME); $this->hostLookupTime = curl_getinfo($curl, CURLINFO_NAMELOOKUP_TIME); diff --git a/src/Util/HeaderNameUtil.php b/src/Util/HeaderNameUtil.php index ed0edd0..fcbd47c 100644 --- a/src/Util/HeaderNameUtil.php +++ b/src/Util/HeaderNameUtil.php @@ -1,5 +1,7 @@