From 49fdd4629b74e109b7c0466f9bee2b229efcc226 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 5 Jun 2019 09:44:30 +0200 Subject: [PATCH] doc and codestyle tweaks --- spec/CachePluginSpec.php | 14 +++++++------- spec/StopwatchPluginSpec.php | 4 ++-- src/CachePlugin.php | 15 ++++++++------- src/PluginClient.php | 2 +- src/RedirectPlugin.php | 2 +- src/StopwatchPlugin.php | 4 ++-- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index 6824f4f..178154f 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -39,8 +39,8 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(200); $response->getBody()->willReturn($stream); - $response->getHeader('Cache-Control')->willReturn(array()); - $response->getHeader('Expires')->willReturn(array()); + $response->getHeader('Cache-Control')->willReturn([]); + $response->getHeader('Expires')->willReturn([]); $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -60,8 +60,8 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte $request->getMethod()->willReturn('GET'); $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(400); - $response->getHeader('Cache-Control')->willReturn(array()); - $response->getHeader('Expires')->willReturn(array()); + $response->getHeader('Cache-Control')->willReturn([]); + $response->getHeader('Expires')->willReturn([]); $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -97,9 +97,9 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(200); $response->getBody()->willReturn($stream); - $response->getHeader('Cache-Control')->willReturn(array('max-age=40')); - $response->getHeader('Age')->willReturn(array('15')); - $response->getHeader('Expires')->willReturn(array()); + $response->getHeader('Cache-Control')->willReturn(['max-age=40']); + $response->getHeader('Age')->willReturn(['15']); + $response->getHeader('Expires')->willReturn([]); $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); diff --git a/spec/StopwatchPluginSpec.php b/spec/StopwatchPluginSpec.php index 2cc2141..c4b74db 100644 --- a/spec/StopwatchPluginSpec.php +++ b/spec/StopwatchPluginSpec.php @@ -33,7 +33,7 @@ function it_records_event(Stopwatch $stopwatch, RequestInterface $request, Respo $request->getRequestTarget()->willReturn('/'); $stopwatch->start('GET /', 'php_http.request')->shouldBeCalled(); - $stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled(); + $stopwatch->stop('GET /')->shouldBeCalled(); $next = function (RequestInterface $request) use ($response) { return new FulfilledPromise($response->getWrappedObject()); @@ -48,7 +48,7 @@ function it_records_event_on_error(Stopwatch $stopwatch, RequestInterface $reque $request->getRequestTarget()->willReturn('/'); $stopwatch->start('GET /', 'php_http.request')->shouldBeCalled(); - $stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled(); + $stopwatch->stop('GET /')->shouldBeCalled(); $next = function (RequestInterface $request) { return new RejectedPromise(new NetworkException('', $request)); diff --git a/src/CachePlugin.php b/src/CachePlugin.php index d6f313a..ca33adf 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -12,7 +12,9 @@ use Symfony\Component\OptionsResolver\OptionsResolver; /** - * Allow for caching a response. + * Allow for caching a response with a PSR-6 compatible caching engine. + * + * It can follow the RFC-7234 caching specification or use a fixed cache lifetime. * * @author Tobias Nyholm * @@ -40,8 +42,8 @@ class CachePlugin implements Plugin * @param StreamFactory $streamFactory * @param array $config { * - * @var bool $respect_cache_headers whether to look at the cache directives or ignore them - * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. + * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them + * @var int $default_ttl If we do not respect cache headers or the headers specify cache control, use this value * } */ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) @@ -61,18 +63,17 @@ public function handleRequest(RequestInterface $request, callable $next, callabl { $method = strtoupper($request->getMethod()); - // if the request not is cachable, move to $next + // if the request is not cacheable, move to $next if ('GET' !== $method && 'HEAD' !== $method) { return $next($request); } - // If we can cache the request $key = $this->createCacheKey($request); $cacheItem = $this->pool->getItem($key); if ($cacheItem->isHit()) { - // return cached response $data = $cacheItem->get(); + /** @var ResponseInterface $response */ $response = $data['response']; $response = $response->withBody($this->streamFactory->createStream($data['body'])); @@ -82,7 +83,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { if ($this->isCacheable($response)) { $bodyStream = $response->getBody(); - $body = $bodyStream->__toString(); + $body = (string) $bodyStream; if ($bodyStream->isSeekable()) { $bodyStream->rewind(); } else { diff --git a/src/PluginClient.php b/src/PluginClient.php index 02cf22b..e91f866 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -28,7 +28,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient * @param Plugin[] $plugins * @param array $options { * - * @var int $max_restarts + * @var int $max_restarts Number of times plugins may initiate a restart of the plugin chain. Defaults to 10. * } * * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient diff --git a/src/RedirectPlugin.php b/src/RedirectPlugin.php index cfaf978..a42b34f 100644 --- a/src/RedirectPlugin.php +++ b/src/RedirectPlugin.php @@ -14,7 +14,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; /** - * Follow redirections. + * Follow redirect responses from the server. * * @author Joel Wurtz * diff --git a/src/StopwatchPlugin.php b/src/StopwatchPlugin.php index 0a9c542..0314a65 100644 --- a/src/StopwatchPlugin.php +++ b/src/StopwatchPlugin.php @@ -42,11 +42,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl $this->stopwatch->start($eventName, self::CATEGORY); return $next($request)->then(function (ResponseInterface $response) use ($eventName) { - $this->stopwatch->stop($eventName, self::CATEGORY); + $this->stopwatch->stop($eventName); return $response; }, function (Exception $exception) use ($eventName) { - $this->stopwatch->stop($eventName, self::CATEGORY); + $this->stopwatch->stop($eventName); throw $exception; });