Complex classes like CachePlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CachePlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class CachePlugin implements Plugin |
||
| 25 | { |
||
| 26 | use VersionBridgePlugin; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var CacheItemPoolInterface |
||
| 30 | */ |
||
| 31 | private $pool; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var StreamFactory |
||
| 35 | */ |
||
| 36 | private $streamFactory; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $config; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Cache directives indicating if a response can not be cached. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $noCacheFlags = ['no-cache', 'private', 'no-store']; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param CacheItemPoolInterface $pool |
||
| 52 | * @param StreamFactory $streamFactory |
||
| 53 | * @param array $config { |
||
| 54 | * |
||
| 55 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
| 56 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
| 57 | * value |
||
| 58 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
| 59 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
| 60 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
| 61 | * We store a cache item for $cache_lifetime + max age of the response. |
||
| 62 | * @var array $methods list of request methods which can be cached |
||
| 63 | * @var array $blacklisted_paths list of regex patterns of paths explicitly not to be cached |
||
| 64 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses |
||
| 65 | * @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator |
||
| 66 | * @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check. |
||
| 67 | * Defaults to an empty array |
||
| 68 | * } |
||
| 69 | */ |
||
| 70 | 16 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
| 90 | * cache responses with `private` cache directive. |
||
| 91 | * |
||
| 92 | * @param CacheItemPoolInterface $pool |
||
| 93 | * @param StreamFactory $streamFactory |
||
| 94 | * @param array $config For all possible config options see the constructor docs |
||
| 95 | * |
||
| 96 | * @return CachePlugin |
||
| 97 | */ |
||
| 98 | 4 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
| 114 | * cache responses with the `private`or `no-cache` directives. |
||
| 115 | * |
||
| 116 | * @param CacheItemPoolInterface $pool |
||
| 117 | * @param StreamFactory $streamFactory |
||
| 118 | * @param array $config For all possible config options see the constructor docs |
||
| 119 | * |
||
| 120 | * @return CachePlugin |
||
| 121 | */ |
||
| 122 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
| 126 | |||
| 127 | 13 | protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
| 212 | * returned is $maxAge. |
||
| 213 | * |
||
| 214 | * @param int|null $maxAge |
||
| 215 | * |
||
| 216 | * @return int|null Unix system time passed to the PSR-6 cache |
||
| 217 | */ |
||
| 218 | 7 | private function calculateCacheItemExpiresAfter($maxAge) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
| 229 | * If-Modified-Since / If-None-Match request to validate the response. |
||
| 230 | * |
||
| 231 | * @param int|null $maxAge |
||
| 232 | * |
||
| 233 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 234 | */ |
||
| 235 | 7 | private function calculateResponseExpiresAt($maxAge) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Verify that we can cache this response. |
||
| 246 | * |
||
| 247 | * @param ResponseInterface $response |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | 8 | protected function isCacheable(ResponseInterface $response) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Verify that we can cache this request. |
||
| 269 | * |
||
| 270 | * @param RequestInterface $request |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | 7 | private function isCacheableRequest(RequestInterface $request) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Get the value of a parameter in the cache control header. |
||
| 287 | * |
||
| 288 | * @param ResponseInterface $response |
||
| 289 | * @param string $name The field of Cache-Control to fetch |
||
| 290 | * |
||
| 291 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
| 292 | */ |
||
| 293 | 8 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param RequestInterface $request |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 12 | private function createCacheKey(RequestInterface $request) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 324 | * |
||
| 325 | * @param ResponseInterface $response |
||
| 326 | * |
||
| 327 | * @return int|null |
||
| 328 | */ |
||
| 329 | 7 | private function getMaxAge(ResponseInterface $response) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Configure an options resolver. |
||
| 357 | * |
||
| 358 | * @param OptionsResolver $resolver |
||
| 359 | */ |
||
| 360 | 16 | private function configureOptions(OptionsResolver $resolver) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param CacheItemInterface $cacheItem |
||
| 409 | * |
||
| 410 | * @return ResponseInterface |
||
| 411 | */ |
||
| 412 | 3 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Get the value of the "If-Modified-Since" header. |
||
| 433 | * |
||
| 434 | * @param CacheItemInterface $cacheItem |
||
| 435 | * |
||
| 436 | * @return string|null |
||
| 437 | */ |
||
| 438 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get the ETag from the cached response. |
||
| 454 | * |
||
| 455 | * @param CacheItemInterface $cacheItem |
||
| 456 | * |
||
| 457 | * @return string|null |
||
| 458 | */ |
||
| 459 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Call the cache listeners, if they are set. |
||
| 476 | * |
||
| 477 | * @param RequestInterface $request |
||
| 478 | * @param ResponseInterface $response |
||
| 479 | * @param bool $cacheHit |
||
| 480 | * @param CacheItemInterface|null $cacheItem |
||
| 481 | * |
||
| 482 | * @return ResponseInterface |
||
| 483 | */ |
||
| 484 | 13 | private function handleCacheListeners(RequestInterface $request, ResponseInterface $response, $cacheHit, $cacheItem) |
|
| 492 | } |
||
| 493 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.