Skip to content

type declarations for php 7.1 #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions spec/Plugin/ErrorPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down
18 changes: 9 additions & 9 deletions spec/Plugin/RedirectPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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 () {});
Expand All @@ -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 () {});
Expand All @@ -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);
Expand All @@ -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 () {});
Expand All @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down
4 changes: 2 additions & 2 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/BatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface HttpClientPool extends HttpAsyncClient, HttpClient
*
* @param ClientInterface|HttpAsyncClient|HttpClientPoolItem $client
*/
public function addHttpClient($client);
public function addHttpClient($client): void;
}
2 changes: 1 addition & 1 deletion src/HttpClientPool/HttpClientPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/HttpClientPool/HttpClientPoolItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,31 @@ public function getSendingRequestCount(): int
/**
* Increment the request count.
*/
private function incrementRequestCount()
private function incrementRequestCount(): void
{
++$this->sendingRequestCount;
}

/**
* Decrement the request count.
*/
private function decrementRequestCount()
private function decrementRequestCount(): void
{
--$this->sendingRequestCount;
}

/**
* Enable the current client.
*/
private function enable()
private function enable(): void
{
$this->disabledAt = null;
}

/**
* Disable the current client.
*/
private function disable()
private function disable(): void
{
$this->disabledAt = new \DateTime('now');
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientRouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/HttpMethodsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/HttpMethodsClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Plugin/AddHostPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/CookiePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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
{
Expand Down
45 changes: 16 additions & 29 deletions src/Plugin/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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']);
Expand Down