diff --git a/spec/Plugin/ErrorPluginSpec.php b/spec/Plugin/ErrorPluginSpec.php index 67e5c7e..205192a 100644 --- a/spec/Plugin/ErrorPluginSpec.php +++ b/spec/Plugin/ErrorPluginSpec.php @@ -27,7 +27,7 @@ public function it_is_a_plugin() public function it_throw_client_error_exception_on_4xx_error(RequestInterface $request, ResponseInterface $response) { - $response->getStatusCode()->willReturn('400'); + $response->getStatusCode()->willReturn(400); $response->getReasonPhrase()->willReturn('Bad request'); $next = function (RequestInterface $receivedRequest) use ($request, $response) { @@ -45,7 +45,7 @@ public function it_does_not_throw_client_error_exception_on_4xx_error_if_only_se { $this->beConstructedWith(['only_server_exception' => true]); - $response->getStatusCode()->willReturn('400'); + $response->getStatusCode()->willReturn(400); $response->getReasonPhrase()->willReturn('Bad request'); $next = function (RequestInterface $receivedRequest) use ($request, $response) { @@ -59,7 +59,7 @@ public function it_does_not_throw_client_error_exception_on_4xx_error_if_only_se public function it_throw_server_error_exception_on_5xx_error(RequestInterface $request, ResponseInterface $response) { - $response->getStatusCode()->willReturn('500'); + $response->getStatusCode()->willReturn(500); $response->getReasonPhrase()->willReturn('Server error'); $next = function (RequestInterface $receivedRequest) use ($request, $response) { @@ -75,7 +75,7 @@ public function it_throw_server_error_exception_on_5xx_error(RequestInterface $r public function it_returns_response(RequestInterface $request, ResponseInterface $response) { - $response->getStatusCode()->willReturn('200'); + $response->getStatusCode()->willReturn(200); $next = function (RequestInterface $receivedRequest) use ($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { diff --git a/spec/Plugin/RedirectPluginSpec.php b/spec/Plugin/RedirectPluginSpec.php index de9b30a..3342f60 100644 --- a/spec/Plugin/RedirectPluginSpec.php +++ b/spec/Plugin/RedirectPluginSpec.php @@ -37,7 +37,7 @@ public function it_redirects_on_302( ResponseInterface $finalResponse, Promise $promise ) { - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $responseRedirect->getHeaderLine('Location')->willReturn('/redirect'); @@ -156,7 +156,7 @@ public function it_replace_full_url( $request->getUri()->willReturn($uri); $uri->__toString()->willReturn('/original'); - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $responseRedirect->getHeaderLine('Location')->willReturn('https://server.com:8000/redirect?query#fragment'); @@ -203,7 +203,7 @@ public function it_throws_http_exception_on_no_location(RequestInterface $reques $request->getUri()->willReturn($uri); $uri->__toString()->willReturn('/original'); - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); @@ -223,7 +223,7 @@ public function it_throws_http_exception_on_invalid_location(RequestInterface $r $uri->__toString()->willReturn('/original'); $responseRedirect->getHeaderLine('Location')->willReturn('scheme:///invalid'); - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $promise = $this->handleRequest($request, $next, function () {}); @@ -240,7 +240,7 @@ public function it_throw_multi_redirect_exception_on_300(RequestInterface $reque }; $this->beConstructedWith(['preserve_header' => true, 'use_default_for_multiple' => false]); - $responseRedirect->getStatusCode()->willReturn('300'); + $responseRedirect->getStatusCode()->willReturn(300); $promise = $this->handleRequest($request, $next, function () {}); $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); @@ -255,7 +255,7 @@ public function it_throw_multi_redirect_exception_on_300_if_no_location(RequestI } }; - $responseRedirect->getStatusCode()->willReturn('300'); + $responseRedirect->getStatusCode()->willReturn(300); $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); @@ -275,7 +275,7 @@ public function it_switch_method_for_302( $request->getUri()->willReturn($uri); $uri->__toString()->willReturn('/original'); - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $responseRedirect->getHeaderLine('Location')->willReturn('/redirect'); @@ -324,7 +324,7 @@ public function it_clears_headers( $request->getUri()->willReturn($uri); $uri->__toString()->willReturn('/original'); - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $responseRedirect->getHeaderLine('Location')->willReturn('/redirect'); @@ -468,7 +468,7 @@ public function it_redirects_http_to_https( ResponseInterface $finalResponse, Promise $promise ) { - $responseRedirect->getStatusCode()->willReturn('302'); + $responseRedirect->getStatusCode()->willReturn(302); $responseRedirect->hasHeader('Location')->willReturn(true); $responseRedirect->getHeaderLine('Location')->willReturn('https://my-site.com/original'); diff --git a/src/Deferred.php b/src/Deferred.php index 7c3f9f5..4c50721 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -79,7 +79,7 @@ public function getState(): string /** * Resolve this deferred with a Response. */ - public function resolve(ResponseInterface $response) + public function resolve(ResponseInterface $response): void { if (self::PENDING !== $this->state) { return; @@ -96,7 +96,7 @@ public function resolve(ResponseInterface $response) /** * Reject this deferred with an Exception. */ - public function reject(Exception $exception) + public function reject(Exception $exception): void { if (self::PENDING !== $this->state) { return; diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php index 5167842..46d1e47 100644 --- a/src/Exception/BatchException.php +++ b/src/Exception/BatchException.php @@ -28,10 +28,8 @@ public function __construct(BatchResult $result) /** * Returns the BatchResult that contains all responses and exceptions. - * - * @return BatchResult */ - public function getResult() + public function getResult(): BatchResult { return $this->result; } diff --git a/src/HttpClientPool.php b/src/HttpClientPool.php index 9e979eb..24ab421 100644 --- a/src/HttpClientPool.php +++ b/src/HttpClientPool.php @@ -20,5 +20,5 @@ interface HttpClientPool extends HttpAsyncClient, HttpClient * * @param ClientInterface|HttpAsyncClient|HttpClientPoolItem $client */ - public function addHttpClient($client); + public function addHttpClient($client): void; } diff --git a/src/HttpClientPool/HttpClientPool.php b/src/HttpClientPool/HttpClientPool.php index 0c417c0..ffa21d2 100644 --- a/src/HttpClientPool/HttpClientPool.php +++ b/src/HttpClientPool/HttpClientPool.php @@ -27,7 +27,7 @@ abstract class HttpClientPool implements HttpClientPoolInterface * * @param ClientInterface|HttpAsyncClient|HttpClientPoolItem $client */ - public function addHttpClient($client) + public function addHttpClient($client): void { if (!$client instanceof HttpClientPoolItem) { $client = new HttpClientPoolItem($client); diff --git a/src/HttpClientPool/HttpClientPoolItem.php b/src/HttpClientPool/HttpClientPoolItem.php index 47c9eb2..31ff569 100644 --- a/src/HttpClientPool/HttpClientPoolItem.php +++ b/src/HttpClientPool/HttpClientPoolItem.php @@ -144,7 +144,7 @@ public function getSendingRequestCount(): int /** * Increment the request count. */ - private function incrementRequestCount() + private function incrementRequestCount(): void { ++$this->sendingRequestCount; } @@ -152,7 +152,7 @@ private function incrementRequestCount() /** * Decrement the request count. */ - private function decrementRequestCount() + private function decrementRequestCount(): void { --$this->sendingRequestCount; } @@ -160,7 +160,7 @@ private function decrementRequestCount() /** * Enable the current client. */ - private function enable() + private function enable(): void { $this->disabledAt = null; } @@ -168,7 +168,7 @@ private function enable() /** * Disable the current client. */ - private function disable() + private function disable(): void { $this->disabledAt = new \DateTime('now'); } diff --git a/src/HttpClientRouter.php b/src/HttpClientRouter.php index 37f8c3c..ac97de0 100644 --- a/src/HttpClientRouter.php +++ b/src/HttpClientRouter.php @@ -45,7 +45,7 @@ public function sendAsyncRequest(RequestInterface $request) * * @param HttpClient|HttpAsyncClient $client */ - public function addClient($client, RequestMatcher $requestMatcher) + public function addClient($client, RequestMatcher $requestMatcher): void { $this->clients[] = [ 'matcher' => $requestMatcher, diff --git a/src/HttpClientRouterInterface.php b/src/HttpClientRouterInterface.php index 4526e7f..ae012cf 100644 --- a/src/HttpClientRouterInterface.php +++ b/src/HttpClientRouterInterface.php @@ -23,5 +23,5 @@ interface HttpClientRouterInterface extends HttpClient, HttpAsyncClient * * @param ClientInterface|HttpAsyncClient $client */ - public function addClient($client, RequestMatcher $requestMatcher); + public function addClient($client, RequestMatcher $requestMatcher): void; } diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index 9c53c71..9db9363 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -71,7 +71,7 @@ public function options($uri, array $headers = [], $body = null): ResponseInterf return $this->send('OPTIONS', $uri, $headers, $body); } - public function send($method, $uri, array $headers = [], $body = null): ResponseInterface + public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface { return $this->sendRequest($this->requestFactory->createRequest( $method, diff --git a/src/HttpMethodsClientInterface.php b/src/HttpMethodsClientInterface.php index 20fbf03..bc0829a 100644 --- a/src/HttpMethodsClientInterface.php +++ b/src/HttpMethodsClientInterface.php @@ -112,5 +112,5 @@ public function options($uri, array $headers = [], $body = null): ResponseInterf * * @throws Exception */ - public function send($method, $uri, array $headers = [], $body = null): ResponseInterface; + public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface; } diff --git a/src/Plugin/AddHostPlugin.php b/src/Plugin/AddHostPlugin.php index db89d34..33ede17 100644 --- a/src/Plugin/AddHostPlugin.php +++ b/src/Plugin/AddHostPlugin.php @@ -66,7 +66,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl return $next($request); } - private function configureOptions(OptionsResolver $resolver) + private function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'replace' => false, diff --git a/src/Plugin/CookiePlugin.php b/src/Plugin/CookiePlugin.php index 8399210..2ec5265 100644 --- a/src/Plugin/CookiePlugin.php +++ b/src/Plugin/CookiePlugin.php @@ -91,11 +91,9 @@ public function handleRequest(RequestInterface $request, callable $next, callabl /** * Creates a cookie from a string. * - * @return Cookie|null - * * @throws TransferException */ - private function createCookie(RequestInterface $request, string $setCookieHeader) + private function createCookie(RequestInterface $request, string $setCookieHeader): ?Cookie { $parts = array_map('trim', explode(';', $setCookieHeader)); @@ -168,6 +166,8 @@ private function createCookie(RequestInterface $request, string $setCookieHeader * Separates key/value pair from cookie. * * @param string $part A single cookie value in format key=value + * + * @return string[] */ private function createValueKey(string $part): array { diff --git a/src/Plugin/RedirectPlugin.php b/src/Plugin/RedirectPlugin.php index e562553..061f35b 100644 --- a/src/Plugin/RedirectPlugin.php +++ b/src/Plugin/RedirectPlugin.php @@ -9,7 +9,6 @@ use Http\Client\Common\Plugin; use Http\Client\Exception\HttpException; use Http\Promise\Promise; -use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -98,7 +97,7 @@ final class RedirectPlugin implements Plugin private $useDefaultForMultiple; /** - * @var array + * @var string[][] Chain identifier => list of URLs for this chain */ private $circularDetection = []; @@ -180,63 +179,51 @@ public function handleRequest(RequestInterface $request, callable $next, callabl }); } - /** - * Builds the redirect request. - * - * @param RequestInterface $request Original request - * @param UriInterface $uri New uri - * @param int $statusCode Status code from the redirect response - * - * @return MessageInterface|RequestInterface - */ - private function buildRedirectRequest(RequestInterface $request, UriInterface $uri, $statusCode) + private function buildRedirectRequest(RequestInterface $originalRequest, UriInterface $targetUri, int $statusCode): RequestInterface { - $request = $request->withUri($uri); + $originalRequest = $originalRequest->withUri($targetUri); - if (false !== $this->redirectCodes[$statusCode]['switch'] && !in_array($request->getMethod(), $this->redirectCodes[$statusCode]['switch']['unless'])) { - $request = $request->withMethod($this->redirectCodes[$statusCode]['switch']['to']); + if (false !== $this->redirectCodes[$statusCode]['switch'] && !in_array($originalRequest->getMethod(), $this->redirectCodes[$statusCode]['switch']['unless'])) { + $originalRequest = $originalRequest->withMethod($this->redirectCodes[$statusCode]['switch']['to']); } if (is_array($this->preserveHeader)) { - $headers = array_keys($request->getHeaders()); + $headers = array_keys($originalRequest->getHeaders()); foreach ($headers as $name) { if (!in_array($name, $this->preserveHeader)) { - $request = $request->withoutHeader($name); + $originalRequest = $originalRequest->withoutHeader($name); } } } - return $request; + return $originalRequest; } /** * Creates a new Uri from the old request and the location header. * - * @param ResponseInterface $response The redirect response - * @param RequestInterface $request The original request - * * @throws HttpException If location header is not usable (missing or incorrect) * @throws MultipleRedirectionException If a 300 status code is received and default location cannot be resolved (doesn't use the location header or not present) */ - private function createUri(ResponseInterface $response, RequestInterface $request): UriInterface + private function createUri(ResponseInterface $redirectResponse, RequestInterface $originalRequest): UriInterface { - if ($this->redirectCodes[$response->getStatusCode()]['multiple'] && (!$this->useDefaultForMultiple || !$response->hasHeader('Location'))) { - throw new MultipleRedirectionException('Cannot choose a redirection', $request, $response); + if ($this->redirectCodes[$redirectResponse->getStatusCode()]['multiple'] && (!$this->useDefaultForMultiple || !$redirectResponse->hasHeader('Location'))) { + throw new MultipleRedirectionException('Cannot choose a redirection', $originalRequest, $redirectResponse); } - if (!$response->hasHeader('Location')) { - throw new HttpException('Redirect status code, but no location header present in the response', $request, $response); + if (!$redirectResponse->hasHeader('Location')) { + throw new HttpException('Redirect status code, but no location header present in the response', $originalRequest, $redirectResponse); } - $location = $response->getHeaderLine('Location'); + $location = $redirectResponse->getHeaderLine('Location'); $parsedLocation = parse_url($location); if (false === $parsedLocation) { - throw new HttpException(sprintf('Location %s could not be parsed', $location), $request, $response); + throw new HttpException(sprintf('Location %s could not be parsed', $location), $originalRequest, $redirectResponse); } - $uri = $request->getUri(); + $uri = $originalRequest->getUri(); if (array_key_exists('scheme', $parsedLocation)) { $uri = $uri->withScheme($parsedLocation['scheme']);