Skip to content

Commit

Permalink
Added psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-bacart committed Oct 21, 2018
1 parent 93a3776 commit 0058c39
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.DS_STORE
.php_cs.cache
psalm.xml
composer.lock
/vendor/
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"require-dev": {
"phpstan/phpstan": "^0.10.3",
"symfony/dom-crawler": "^4.1",
"vimeo/psalm": "^2.0",
"wa72/htmlpagedom": "^1.3"
},
"suggest": {
Expand Down
19 changes: 15 additions & 4 deletions src/Client/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ public function __construct(
array $config = [],
iterable $middlewares = null
) {
$handlerStack = $config[GuzzleClientInterface::CONFIG_HANDLER] ?? HandlerStack::create();

if (null !== $middlewares) {
$handlerStack = $config[GuzzleClientInterface::CONFIG_HANDLER]
?? HandlerStack::create();

foreach ($middlewares as $middleware) {
$handlerStack->push($middleware);
}
}

$config[GuzzleClientInterface::CONFIG_HANDLER] = $handlerStack;
$config[GuzzleClientInterface::CONFIG_HANDLER] = $handlerStack;
}

parent::__construct($config);
}

/**
* {@inheritdoc}
*
* @throws GuzzleClientException
*
* @psalm-suppress InvalidCatch
*/
public function getGuzzleResponse(
string $uri,
Expand All @@ -60,6 +65,8 @@ public function getGuzzleResponse(

/**
* {@inheritdoc}
*
* @throws GuzzleClientException
*/
public function getGuzzleResponseAsString(
string $uri,
Expand All @@ -75,6 +82,8 @@ public function getGuzzleResponseAsString(

/**
* {@inheritdoc}
*
* @throws GuzzleClientException
*/
public function getGuzzleResponseAsJson(
string $uri,
Expand All @@ -98,6 +107,7 @@ public function getGuzzleResponseAsJson(
* {@inheritdoc}
*
* @throws MissingPackageException
* @throws GuzzleClientException
*/
public function getGuzzleResponseAsCrawler(
string $uri,
Expand Down Expand Up @@ -131,6 +141,7 @@ public function getGuzzleResponseAsCrawler(
* {@inheritdoc}
*
* @throws MissingPackageException
* @throws GuzzleClientException
*/
public function getGuzzleResponseAsHtmlPage(
string $uri,
Expand Down
51 changes: 29 additions & 22 deletions src/Client/GuzzleClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bacart\GuzzleClient\Client;

use Bacart\GuzzleClient\Exception\GuzzleClientException;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\DomCrawler\Crawler;
Expand Down Expand Up @@ -35,18 +34,18 @@ interface GuzzleClientInterface extends ClientInterface

public const HTTP_CONTINUE = 100;
public const HTTP_SWITCHING_PROTOCOLS = 101;
public const HTTP_PROCESSING = 102; // RFC2518
public const HTTP_EARLY_HINTS = 103; // RFC8297
public const HTTP_PROCESSING = 102; // RFC2518
public const HTTP_EARLY_HINTS = 103; // RFC8297
public const HTTP_OK = 200;
public const HTTP_CREATED = 201;
public const HTTP_ACCEPTED = 202;
public const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
public const HTTP_NO_CONTENT = 204;
public const HTTP_RESET_CONTENT = 205;
public const HTTP_PARTIAL_CONTENT = 206;
public const HTTP_MULTI_STATUS = 207; // RFC4918
public const HTTP_ALREADY_REPORTED = 208; // RFC5842
public const HTTP_IM_USED = 226; // RFC3229
public const HTTP_MULTI_STATUS = 207; // RFC4918
public const HTTP_ALREADY_REPORTED = 208; // RFC5842
public const HTTP_IM_USED = 226; // RFC3229
public const HTTP_MULTIPLE_CHOICES = 300;
public const HTTP_MOVED_PERMANENTLY = 301;
public const HTTP_FOUND = 302;
Expand All @@ -55,7 +54,7 @@ interface GuzzleClientInterface extends ClientInterface
public const HTTP_USE_PROXY = 305;
public const HTTP_RESERVED = 306;
public const HTTP_TEMPORARY_REDIRECT = 307;
public const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
public const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
public const HTTP_BAD_REQUEST = 400;
public const HTTP_UNAUTHORIZED = 401;
public const HTTP_PAYMENT_REQUIRED = 402;
Expand All @@ -74,19 +73,35 @@ interface GuzzleClientInterface extends ClientInterface
public const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
public const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
public const HTTP_EXPECTATION_FAILED = 417;
public const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
public const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
public const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
public const HTTP_LOCKED = 423; // RFC4918
public const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
public const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
public const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
public const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
public const HTTP_LOCKED = 423; // RFC4918
public const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
public const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817
public const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04
public const HTTP_UPGRADE_REQUIRED = 426; // RFC2817
public const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585
public const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585
public const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
public const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
public const HTTP_INTERNAL_SERVER_ERROR = 500;
public const HTTP_NOT_IMPLEMENTED = 501;
public const HTTP_BAD_GATEWAY = 502;
public const HTTP_SERVICE_UNAVAILABLE = 503;
public const HTTP_GATEWAY_TIMEOUT = 504;
public const HTTP_VERSION_NOT_SUPPORTED = 505;
public const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
public const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918
public const HTTP_LOOP_DETECTED = 508; // RFC5842
public const HTTP_NOT_EXTENDED = 510; // RFC2774
public const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585

/**
* @param string $uri
* @param array $options
* @param string $method
*
* @throws GuzzleClientException
*
* @return ResponseInterface
*/
public function getGuzzleResponse(
Expand All @@ -100,8 +115,6 @@ public function getGuzzleResponse(
* @param array $options
* @param string $method
*
* @throws GuzzleClientException
*
* @return string
*/
public function getGuzzleResponseAsString(
Expand All @@ -115,8 +128,6 @@ public function getGuzzleResponseAsString(
* @param array $options
* @param string $method
*
* @throws GuzzleClientException
*
* @return array|null
*/
public function getGuzzleResponseAsJson(
Expand All @@ -130,8 +141,6 @@ public function getGuzzleResponseAsJson(
* @param array $options
* @param string $method
*
* @throws GuzzleClientException
*
* @return Crawler
*/
public function getGuzzleResponseAsCrawler(
Expand All @@ -145,8 +154,6 @@ public function getGuzzleResponseAsCrawler(
* @param array $options
* @param string $method
*
* @throws GuzzleClientException
*
* @return HtmlPage
*/
public function getGuzzleResponseAsHtmlPage(
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/GuzzleMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GuzzleMessageFormatter extends MessageFormatter
protected const CREDENTIAL_REPLACEMENT = '******';

/** @var string[] */
protected $credentialsToReplace;
protected $credentialsToReplace = [];

/**
* @param string[] $credentialsToReplace
Expand Down
5 changes: 5 additions & 0 deletions src/Middleware/LogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __construct(
*/
public function __invoke(callable $handler): callable
{
// TODO: check with a NULL logger
if (null === $this->logger) {
return $handler;
}

return Middleware::log(
$this->logger,
$this->formatter,
Expand Down
24 changes: 12 additions & 12 deletions src/Middleware/RequestRetryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bacart\GuzzleClient\Middleware;

use Bacart\GuzzleClient\Client\GuzzleClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
Expand All @@ -11,7 +12,6 @@

class RequestRetryMiddleware implements GuzzleClientMiddlewareInterface
{
protected const HTTP_INTERNAL_SERVER_ERROR = 500;
protected const MAX_RETRIES = 2;

/** @var LoggerInterface|null */
Expand Down Expand Up @@ -54,22 +54,22 @@ public function decider(
Response $response = null,
RequestException $exception = null
): bool {
if (!($exception instanceof ConnectException)
|| null === $response
|| $response->getStatusCode() < static::HTTP_INTERNAL_SERVER_ERROR) {
return false;
}
$statusCode = null !== $response ? $response->getStatusCode() : 0;

$retryNeeded = $retries < $this->maxRetries;
$retryNeeded = $retries < $this->maxRetries
&& ($exception instanceof ConnectException
|| $statusCode > GuzzleClientInterface::HTTP_INTERNAL_SERVER_ERROR);

if ($retryNeeded && null !== $this->logger) {
$context = [
GuzzleClientMiddlewareInterface::STATUS => $statusCode,
GuzzleClientMiddlewareInterface::URI => (string) $request->getUri(),
GuzzleClientMiddlewareInterface::METHOD => $request->getMethod(),
];

$this->logger->warning(
sprintf('Request retry (%d)', $retries),
[
GuzzleClientMiddlewareInterface::STATUS => null === $response ? 0 : $response->getStatusCode(),
GuzzleClientMiddlewareInterface::URI => (string) $request->getUri(),
GuzzleClientMiddlewareInterface::METHOD => $request->getMethod(),
]
$context
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Middleware/ResponseCacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bacart\GuzzleClient\Middleware;

use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Response;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -56,14 +57,14 @@ public function __construct(
*/
public function __invoke(callable $handler): callable
{
return function (RequestInterface $request, array $options) use ($handler) {
return function (RequestInterface $request, array $options) use ($handler): PromiseInterface {
$cache = $options[static::CACHE] ?? true;

unset($options[static::CACHE]);

if (!$cache) {
return $handler($request, $options)->then(
function (ResponseInterface $response) {
function (ResponseInterface $response): ResponseInterface {
return $response;
}
);
Expand Down Expand Up @@ -112,7 +113,7 @@ protected function cacheSave(
array $options
) {
return $handler($request, $options)->then(
function (ResponseInterface $response) use ($request) {
function (ResponseInterface $response) use ($request): ResponseInterface {
$key = $this->getCacheKey($request);

$item = $this->cache->getItem($key)
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/UserAgentMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function __construct(array $userAgents = self::USER_AGENTS)
*/
public function __invoke(callable $handler): callable
{
return function (RequestInterface $request, array $options) use ($handler) {
return function (RequestInterface $request, array $options) use ($handler): callable {
$request = $request->withHeader(
static::USER_AGENT_HEADER,
$this->userAgents[array_rand($this->userAgents)]
Expand Down

0 comments on commit 0058c39

Please sign in to comment.