Skip to content

Commit

Permalink
Added return types to many anonymous functions (#2449)
Browse files Browse the repository at this point in the history
* Added return types to many anonymous functions

* Fix the failign ci

* Fix failing ci

* Update HandlerStack.php
  • Loading branch information
gmponos authored and Nyholm committed Dec 11, 2019
1 parent 3d9079b commit 92d3e04
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .php_cs
Expand Up @@ -13,7 +13,7 @@ $config = PhpCsFixer\Config::create()
'no_empty_phpdoc' => true,
'no_extra_blank_lines' => true,
'ordered_imports' => true,
'no_superfluous_phpdoc_tags' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'no_blank_lines_after_phpdoc' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_trim' => true,
Expand Down
42 changes: 1 addition & 41 deletions phpstan-baseline.neon
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: src/Client.php

-
message: "#^Method GuzzleHttp\\\\Client\\:\\:getConfig\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/Client.php

-
message: "#^Method GuzzleHttp\\\\Client\\:\\:buildUri\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -100,11 +95,6 @@ parameters:
count: 1
path: src/ClientInterface.php

-
message: "#^Method GuzzleHttp\\\\ClientInterface\\:\\:getConfig\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/ClientInterface.php

-
message: "#^Method GuzzleHttp\\\\Cookie\\\\CookieJar\\:\\:__construct\\(\\) has parameter \\$cookieArray with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -906,27 +896,7 @@ parameters:
path: src/RetryMiddleware.php

-
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$request has no typehint specified\\.$#"
count: 1
path: src/TransferStats.php

-
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$response has no typehint specified\\.$#"
count: 1
path: src/TransferStats.php

-
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$transferTime has no typehint specified\\.$#"
count: 1
path: src/TransferStats.php

-
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$handlerStats has no typehint specified\\.$#"
count: 1
path: src/TransferStats.php

-
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$handlerErrorData has no typehint specified\\.$#"
message: "#^Property GuzzleHttp\\\\TransferStats\\:\\:\\$handlerStats type has no value type specified in iterable type array\\.$#"
count: 1
path: src/TransferStats.php

Expand All @@ -935,11 +905,6 @@ parameters:
count: 1
path: src/TransferStats.php

-
message: "#^Method GuzzleHttp\\\\TransferStats\\:\\:getHandlerErrorData\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/TransferStats.php

-
message: "#^Method GuzzleHttp\\\\TransferStats\\:\\:getHandlerStats\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -980,11 +945,6 @@ parameters:
count: 1
path: src/functions.php

-
message: "#^Function GuzzleHttp\\\\is_host_in_noproxy\\(\\) has parameter \\$noProxyArray with no value type specified in iterable type array\\.$#"
count: 1
path: src/functions.php

-
message: "#^Cannot access offset 0 on array\\<int, string\\>\\|false\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions src/Client.php
Expand Up @@ -202,6 +202,8 @@ public function request(string $method, $uri = '', array $options = []): Respons
* the concrete client.
*
* @param string|null $option The config option to retrieve.
*
* @return mixed
*/
public function getConfig(?string $option = null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ClientInterface.php
Expand Up @@ -69,6 +69,8 @@ public function requestAsync(string $method, $uri, array $options = []): Promise
* the concrete client.
*
* @param string|null $option The config option to retrieve.
*
* @return mixed
*/
public function getConfig(?string $option = null);
}
8 changes: 4 additions & 4 deletions src/Cookie/CookieJar.php
Expand Up @@ -100,7 +100,7 @@ public function getCookieByName(string $name): ?SetCookie
*/
public function toArray(): array
{
return array_map(function (SetCookie $cookie) {
return array_map(function (SetCookie $cookie): array {
return $cookie->toArray();
}, $this->getIterator()->getArrayCopy());
}
Expand All @@ -116,14 +116,14 @@ public function clear(?string $domain = null, ?string $path = null, ?string $nam
} elseif (!$path) {
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) use ($domain) {
function (SetCookie $cookie) use ($domain): bool {
return !$cookie->matchesDomain($domain);
}
);
} elseif (!$name) {
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) use ($path, $domain) {
function (SetCookie $cookie) use ($path, $domain): bool {
return !($cookie->matchesPath($path) &&
$cookie->matchesDomain($domain));
}
Expand All @@ -147,7 +147,7 @@ public function clearSessionCookies(): void
{
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) {
function (SetCookie $cookie): bool {
return !$cookie->getDiscard() && $cookie->getExpires();
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CurlFactory.php
Expand Up @@ -394,7 +394,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
$sink = new LazyOpenStream($sink, 'w+');
}
$easy->sink = $sink;
$conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink) {
$conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink): int {
return $sink->write($write);
};
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/HandlerStack.php
Expand Up @@ -31,13 +31,11 @@ class HandlerStack
* The returned handler stack can be passed to a client in the "handler"
* option.
*
* @param callable $handler HTTP handler function to use with the stack. If no
* handler is provided, the best handler for your
* system will be utilized.
*
* @return HandlerStack
* @param callable|null $handler HTTP handler function to use with the stack. If no
* handler is provided, the best handler for your
* system will be utilized.
*/
public static function create(callable $handler = null): self
public static function create(?callable $handler = null): self
{
$stack = new self($handler ?: choose_handler());
$stack->push(Middleware::httpErrors(), 'http_errors');
Expand Down
42 changes: 21 additions & 21 deletions src/Middleware.php
Expand Up @@ -3,8 +3,8 @@

use GuzzleHttp\Cookie\CookieJarInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -23,7 +23,7 @@ final class Middleware
*/
public static function cookies(): callable
{
return function (callable $handler) {
return function (callable $handler): callable {
return function ($request, array $options) use ($handler) {
if (empty($options['cookies'])) {
return $handler($request, $options);
Expand All @@ -34,7 +34,7 @@ public static function cookies(): callable
$request = $cookieJar->withCookieHeader($request);
return $handler($request, $options)
->then(
function ($response) use ($cookieJar, $request) {
function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface {
$cookieJar->extractCookies($request, $response);
return $response;
}
Expand All @@ -51,7 +51,7 @@ function ($response) use ($cookieJar, $request) {
*/
public static function httpErrors(): callable
{
return function (callable $handler) {
return function (callable $handler): callable {
return function ($request, array $options) use ($handler) {
if (empty($options['http_errors'])) {
return $handler($request, $options);
Expand All @@ -78,14 +78,14 @@ function (ResponseInterface $response) use ($request) {
*
* @throws \InvalidArgumentException if container is not an array or ArrayAccess.
*/
public static function history(&$container)
public static function history(&$container): callable
{
if (!is_array($container) && !$container instanceof \ArrayAccess) {
throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
}

return function (callable $handler) use (&$container) {
return function ($request, array $options) use ($handler, &$container) {
return function (callable $handler) use (&$container): callable {
return function (RequestInterface $request, array $options) use ($handler, &$container) {
return $handler($request, $options)->then(
function ($value) use ($request, &$container, $options) {
$container[] = [
Expand Down Expand Up @@ -125,8 +125,8 @@ function ($reason) use ($request, &$container, $options) {
*/
public static function tap(callable $before = null, callable $after = null): callable
{
return function (callable $handler) use ($before, $after) {
return function ($request, array $options) use ($handler, $before, $after) {
return function (callable $handler) use ($before, $after): callable {
return function (RequestInterface $request, array $options) use ($handler, $before, $after) {
if ($before) {
$before($request, $options);
}
Expand All @@ -146,7 +146,7 @@ public static function tap(callable $before = null, callable $after = null): cal
*/
public static function redirect(): callable
{
return function (callable $handler) {
return function (callable $handler): RedirectMiddleware {
return new RedirectMiddleware($handler);
};
}
Expand All @@ -168,7 +168,7 @@ public static function redirect(): callable
*/
public static function retry(callable $decider, callable $delay = null): callable
{
return function (callable $handler) use ($decider, $delay) {
return function (callable $handler) use ($decider, $delay): RetryMiddleware {
return new RetryMiddleware($decider, $handler, $delay);
};
}
Expand All @@ -185,15 +185,15 @@ public static function retry(callable $decider, callable $delay = null): callabl
*/
public static function log(LoggerInterface $logger, MessageFormatter $formatter, string $logLevel = 'info' /* \Psr\Log\LogLevel::INFO */): callable
{
return function (callable $handler) use ($logger, $formatter, $logLevel) {
return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) {
return function (callable $handler) use ($logger, $formatter, $logLevel): callable {
return function (RequestInterface $request, array $options) use ($handler, $logger, $formatter, $logLevel) {
return $handler($request, $options)->then(
function ($response) use ($logger, $request, $formatter, $logLevel) {
function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface {
$message = $formatter->format($request, $response);
$logger->log($logLevel, $message);
return $response;
},
function ($reason) use ($logger, $request, $formatter) {
function ($reason) use ($logger, $request, $formatter): PromiseInterface {
$response = $reason instanceof RequestException
? $reason->getResponse()
: null;
Expand All @@ -212,7 +212,7 @@ function ($reason) use ($logger, $request, $formatter) {
*/
public static function prepareBody(): callable
{
return function (callable $handler) {
return function (callable $handler): PrepareBodyMiddleware {
return new PrepareBodyMiddleware($handler);
};
}
Expand All @@ -226,8 +226,8 @@ public static function prepareBody(): callable
*/
public static function mapRequest(callable $fn): callable
{
return function (callable $handler) use ($fn) {
return function ($request, array $options) use ($handler, $fn) {
return function (callable $handler) use ($fn): callable {
return function (RequestInterface $request, array $options) use ($handler, $fn) {
return $handler($fn($request), $options);
};
};
Expand All @@ -242,8 +242,8 @@ public static function mapRequest(callable $fn): callable
*/
public static function mapResponse(callable $fn): callable
{
return function (callable $handler) use ($fn) {
return function ($request, array $options) use ($handler, $fn) {
return function (callable $handler) use ($fn): callable {
return function (RequestInterface $request, array $options) use ($handler, $fn) {
return $handler($request, $options)->then($fn);
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/Pool.php
Expand Up @@ -89,7 +89,7 @@ public function promise(): PromiseInterface
* @param ClientInterface $client Client used to send the requests
* @param array|\Iterator $requests Requests to send concurrently.
* @param array $options Passes through the options available in
* {@see GuzzleHttp\Pool::__construct}
* {@see \GuzzleHttp\Pool::__construct}
*
* @return array Returns an array containing the response or an exception
* in the same order that the requests were sent.
Expand Down
2 changes: 1 addition & 1 deletion src/RedirectMiddleware.php
Expand Up @@ -13,7 +13,7 @@
* Request redirect middleware.
*
* Apply this middleware like other middleware using
* {@see GuzzleHttp\Middleware::redirect()}.
* {@see \GuzzleHttp\Middleware::redirect()}.
*/
class RedirectMiddleware
{
Expand Down
4 changes: 1 addition & 3 deletions src/RetryMiddleware.php
Expand Up @@ -2,8 +2,6 @@
namespace GuzzleHttp;

use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -47,7 +45,7 @@ public function __construct(
*
* @return int milliseconds.
*/
public static function exponentialDelay(int $retries)
public static function exponentialDelay(int $retries): int
{
return (int) pow(2, $retries - 1) * 1000;
}
Expand Down
21 changes: 21 additions & 0 deletions src/TransferStats.php
Expand Up @@ -11,10 +11,29 @@
*/
final class TransferStats
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var ResponseInterface|null
*/
private $response;

/**
* @var float|null
*/
private $transferTime;

/**
* @var array
*/
private $handlerStats;

/**
* @var mixed|null
*/
private $handlerErrorData;

/**
Expand Down Expand Up @@ -65,6 +84,8 @@ public function hasResponse(): bool
* This might be an exception, a integer representing an error code, or
* anything else. Relying on this value assumes that you know what handler
* you are using.
*
* @return mixed
*/
public function getHandlerErrorData()
{
Expand Down

0 comments on commit 92d3e04

Please sign in to comment.