From 87b9c61bbcf057847342711bcc93310ab1b47a71 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 1 Jun 2022 08:06:28 +0200 Subject: [PATCH] explicitly use 0 instead of adding null --- CHANGELOG.md | 1 + src/CachePlugin.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 754de2a..2d6048c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Test with PHP 8.1 and 8.2 - Made phpspec tests compatible with PSR-7 2.0 strict typing +- Detect `null` and use 0 explicitly to calculate expiration ## 1.7.5 - 2022-01-18 diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 77de67c..e0f2cbc 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -58,7 +58,7 @@ final class CachePlugin implements Plugin * bool respect_cache_headers: Whether to look at the cache directives or ignore them * int default_ttl: (seconds) If we do not respect cache headers or can't calculate a good ttl, use this value * string hash_algo: The hashing algorithm to use when generating cache keys - * int cache_lifetime: (seconds) To support serving a previous stale response when the server answers 304 + * int|null cache_lifetime: (seconds) To support serving a previous stale response when the server answers 304 * we have to store the cache for a longer time than the server originally says it is valid for. * We store a cache item for $cache_lifetime + max age of the response. * string[] methods: list of request methods which can be cached @@ -230,7 +230,7 @@ private function calculateCacheItemExpiresAfter(?int $maxAge): ?int return null; } - return $this->config['cache_lifetime'] + $maxAge; + return ($this->config['cache_lifetime'] ?: 0) + ($maxAge ?: 0); } /** @@ -368,7 +368,7 @@ private function configureOptions(OptionsResolver $resolver): void $resolver->setAllowedTypes('default_ttl', ['int', 'null']); $resolver->setAllowedTypes('respect_cache_headers', ['bool', 'null']); $resolver->setAllowedTypes('methods', 'array'); - $resolver->setAllowedTypes('cache_key_generator', ['null', 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator']); + $resolver->setAllowedTypes('cache_key_generator', ['null', CacheKeyGenerator::class]); $resolver->setAllowedTypes('blacklisted_paths', 'array'); $resolver->setAllowedValues('hash_algo', hash_algos()); $resolver->setAllowedValues('methods', function ($value) {