99use Http \Client \Common \Plugin \Cache \Listener \CacheListener ;
1010use Http \Message \StreamFactory ;
1111use Http \Promise \FulfilledPromise ;
12+ use Http \Promise \Promise ;
1213use Psr \Cache \CacheItemInterface ;
1314use Psr \Cache \CacheItemPoolInterface ;
1415use Psr \Http \Message \RequestInterface ;
@@ -39,20 +40,20 @@ final class CachePlugin implements Plugin
3940 private $ streamFactory ;
4041
4142 /**
42- * @var array
43+ * @var mixed[]
4344 */
4445 private $ config ;
4546
4647 /**
4748 * Cache directives indicating if a response can not be cached.
4849 *
49- * @var array
50+ * @var string[]
5051 */
5152 private $ noCacheFlags = ['no-cache ' , 'private ' , 'no-store ' ];
5253
5354 /**
5455 * @param StreamFactory|StreamFactoryInterface $streamFactory
55- * @param array $config {
56+ * @param mixed[] $config {
5657 *
5758 * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them
5859 * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this
@@ -61,9 +62,9 @@ final class CachePlugin implements Plugin
6162 * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304
6263 * we have to store the cache for a longer time than the server originally says it is valid for.
6364 * We store a cache item for $cache_lifetime + max age of the response.
64- * @var array $methods list of request methods which can be cached
65- * @var array $blacklisted_paths list of regex for URLs explicitly not to be cached
66- * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
65+ * @var string[] $methods list of request methods which can be cached
66+ * @var string[] $blacklisted_paths list of regex for URLs explicitly not to be cached
67+ * @var string[] $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
6768 * @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator
6869 * @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check.
6970 * Defaults to an empty array
@@ -96,7 +97,7 @@ public function __construct(CacheItemPoolInterface $pool, $streamFactory, array
9697 * cache responses with `private` cache directive.
9798 *
9899 * @param StreamFactory|StreamFactoryInterface $streamFactory
99- * @param array $config For all possible config options see the constructor docs
100+ * @param mixed[] $config For all possible config options see the constructor docs
100101 *
101102 * @return CachePlugin
102103 */
@@ -119,7 +120,7 @@ public static function clientCache(CacheItemPoolInterface $pool, $streamFactory,
119120 * cache responses with the `private`or `no-cache` directives.
120121 *
121122 * @param StreamFactory|StreamFactoryInterface $streamFactory
122- * @param array $config For all possible config options see the constructor docs
123+ * @param mixed[] $config For all possible config options see the constructor docs
123124 *
124125 * @return CachePlugin
125126 */
@@ -128,6 +129,11 @@ public static function serverCache(CacheItemPoolInterface $pool, $streamFactory,
128129 return new self ($ pool , $ streamFactory , $ config );
129130 }
130131
132+ /**
133+ * {@inheritdoc}
134+ *
135+ * @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception (The same as HttpAsyncClient)
136+ */
131137 protected function doHandleRequest (RequestInterface $ request , callable $ next , callable $ first )
132138 {
133139 $ method = strtoupper ($ request ->getMethod ());
@@ -215,11 +221,9 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
215221 * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be
216222 * returned is $maxAge.
217223 *
218- * @param int|null $maxAge
219- *
220224 * @return int|null Unix system time passed to the PSR-6 cache
221225 */
222- private function calculateCacheItemExpiresAfter ($ maxAge )
226+ private function calculateCacheItemExpiresAfter (? int $ maxAge ): ? int
223227 {
224228 if (null === $ this ->config ['cache_lifetime ' ] && null === $ maxAge ) {
225229 return null ;
@@ -232,11 +236,9 @@ private function calculateCacheItemExpiresAfter($maxAge)
232236 * Calculate the timestamp when a response expires. After that timestamp, we need to send a
233237 * If-Modified-Since / If-None-Match request to validate the response.
234238 *
235- * @param int|null $maxAge
236- *
237239 * @return int|null Unix system time. A null value means that the response expires when the cache item expires
238240 */
239- private function calculateResponseExpiresAt ($ maxAge )
241+ private function calculateResponseExpiresAt (? int $ maxAge ): ? int
240242 {
241243 if (null === $ maxAge ) {
242244 return null ;
@@ -268,10 +270,8 @@ protected function isCacheable(ResponseInterface $response)
268270
269271 /**
270272 * Verify that we can cache this request.
271- *
272- * @return bool
273273 */
274- private function isCacheableRequest (RequestInterface $ request )
274+ private function isCacheableRequest (RequestInterface $ request ): bool
275275 {
276276 $ uri = $ request ->getUri ()->__toString ();
277277 foreach ($ this ->config ['blacklisted_paths ' ] as $ regex ) {
@@ -290,7 +290,7 @@ private function isCacheableRequest(RequestInterface $request)
290290 *
291291 * @return bool|string The value of the directive, true if directive without value, false if directive not present
292292 */
293- private function getCacheControlDirective (ResponseInterface $ response , $ name )
293+ private function getCacheControlDirective (ResponseInterface $ response , string $ name )
294294 {
295295 $ headers = $ response ->getHeader ('Cache-Control ' );
296296 foreach ($ headers as $ header ) {
@@ -307,22 +307,19 @@ private function getCacheControlDirective(ResponseInterface $response, $name)
307307 return false ;
308308 }
309309
310- /**
311- * @return string
312- */
313- private function createCacheKey (RequestInterface $ request )
310+ private function createCacheKey (RequestInterface $ request ): string
314311 {
315312 $ key = $ this ->config ['cache_key_generator ' ]->generate ($ request );
316313
317314 return hash ($ this ->config ['hash_algo ' ], $ key );
318315 }
319316
320317 /**
321- * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl.
318+ * Get a ttl in seconds.
322319 *
323- * @return int| null
320+ * Returns null if we do not respect cache headers and got no defaultTtl.
324321 */
325- private function getMaxAge (ResponseInterface $ response )
322+ private function getMaxAge (ResponseInterface $ response ): ? int
326323 {
327324 if (!in_array ('max-age ' , $ this ->config ['respect_response_cache_directives ' ], true )) {
328325 return $ this ->config ['default_ttl ' ];
@@ -351,7 +348,7 @@ private function getMaxAge(ResponseInterface $response)
351348 /**
352349 * Configure an options resolver.
353350 */
354- private function configureOptions (OptionsResolver $ resolver )
351+ private function configureOptions (OptionsResolver $ resolver ): void
355352 {
356353 $ resolver ->setDefaults ([
357354 'cache_lifetime ' => 86400 * 30 , // 30 days
@@ -398,10 +395,7 @@ private function configureOptions(OptionsResolver $resolver)
398395 });
399396 }
400397
401- /**
402- * @return ResponseInterface
403- */
404- private function createResponseFromCacheItem (CacheItemInterface $ cacheItem )
398+ private function createResponseFromCacheItem (CacheItemInterface $ cacheItem ): ResponseInterface
405399 {
406400 $ data = $ cacheItem ->get ();
407401
@@ -415,17 +409,13 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem)
415409 throw new RewindStreamException ('Cannot rewind stream. ' , 0 , $ e );
416410 }
417411
418- $ response = $ response ->withBody ($ stream );
419-
420- return $ response ;
412+ return $ response ->withBody ($ stream );
421413 }
422414
423415 /**
424- * Get the value of the "If-Modified-Since" header.
425- *
426- * @return string|null
416+ * Get the value for the "If-Modified-Since" header.
427417 */
428- private function getModifiedSinceHeaderValue (CacheItemInterface $ cacheItem )
418+ private function getModifiedSinceHeaderValue (CacheItemInterface $ cacheItem ): ? string
429419 {
430420 $ data = $ cacheItem ->get ();
431421 // The isset() is to be removed in 2.0.
@@ -441,10 +431,8 @@ private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem)
441431
442432 /**
443433 * Get the ETag from the cached response.
444- *
445- * @return string|null
446434 */
447- private function getETag (CacheItemInterface $ cacheItem )
435+ private function getETag (CacheItemInterface $ cacheItem ): ? string
448436 {
449437 $ data = $ cacheItem ->get ();
450438 // The isset() is to be removed in 2.0.
@@ -462,14 +450,9 @@ private function getETag(CacheItemInterface $cacheItem)
462450 }
463451
464452 /**
465- * Call the cache listeners, if they are set.
466- *
467- * @param bool $cacheHit
468- * @param CacheItemInterface|null $cacheItem
469- *
470- * @return ResponseInterface
453+ * Call the registered cache listeners.
471454 */
472- private function handleCacheListeners (RequestInterface $ request , ResponseInterface $ response , $ cacheHit , $ cacheItem )
455+ private function handleCacheListeners (RequestInterface $ request , ResponseInterface $ response , bool $ cacheHit , ? CacheItemInterface $ cacheItem ): ResponseInterface
473456 {
474457 foreach ($ this ->config ['cache_listeners ' ] as $ cacheListener ) {
475458 $ response = $ cacheListener ->onCacheResponse ($ request , $ response , $ cacheHit , $ cacheItem );
0 commit comments