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 $respect_response_cache_directives list of cache directives this plugin will respect while caching responses |
||
64 | * @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator |
||
65 | * @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check. |
||
66 | * Defaults to an empty array |
||
67 | * } |
||
68 | */ |
||
69 | 14 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
70 | { |
||
71 | 14 | $this->pool = $pool; |
|
72 | 14 | $this->streamFactory = $streamFactory; |
|
73 | |||
74 | 14 | if (isset($config['respect_cache_headers']) && isset($config['respect_response_cache_directives'])) { |
|
75 | throw new \InvalidArgumentException( |
||
76 | 'You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives". '. |
||
77 | 'Use "respect_response_cache_directives" instead.' |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | 14 | $optionsResolver = new OptionsResolver(); |
|
82 | 14 | $this->configureOptions($optionsResolver); |
|
83 | 14 | $this->config = $optionsResolver->resolve($config); |
|
84 | |||
85 | 13 | if (null === $this->config['cache_key_generator']) { |
|
86 | 12 | $this->config['cache_key_generator'] = new SimpleGenerator(); |
|
87 | 12 | } |
|
88 | 13 | } |
|
89 | |||
90 | /** |
||
91 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
92 | * cache responses with `private` cache directive. |
||
93 | * |
||
94 | * @param CacheItemPoolInterface $pool |
||
95 | * @param StreamFactory $streamFactory |
||
96 | * @param array $config For all possible config options see the constructor docs |
||
97 | * |
||
98 | * @return CachePlugin |
||
99 | */ |
||
100 | 2 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
101 | { |
||
102 | // Allow caching of private requests |
||
103 | 2 | if (isset($config['respect_response_cache_directives'])) { |
|
104 | $config['respect_response_cache_directives'][] = 'no-cache'; |
||
105 | $config['respect_response_cache_directives'][] = 'max-age'; |
||
106 | $config['respect_response_cache_directives'] = array_unique($config['respect_response_cache_directives']); |
||
107 | } else { |
||
108 | 2 | $config['respect_response_cache_directives'] = ['no-cache', 'max-age']; |
|
109 | } |
||
110 | |||
111 | 2 | return new self($pool, $streamFactory, $config); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
116 | * cache responses with the `private`or `no-cache` directives. |
||
117 | * |
||
118 | * @param CacheItemPoolInterface $pool |
||
119 | * @param StreamFactory $streamFactory |
||
120 | * @param array $config For all possible config options see the constructor docs |
||
121 | * |
||
122 | * @return CachePlugin |
||
123 | */ |
||
124 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
125 | { |
||
126 | return new self($pool, $streamFactory, $config); |
||
127 | } |
||
128 | |||
129 | 11 | protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) |
|
211 | |||
212 | /** |
||
213 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
214 | * returned is $maxAge. |
||
215 | * |
||
216 | * @param int|null $maxAge |
||
217 | * |
||
218 | * @return int|null Unix system time passed to the PSR-6 cache |
||
219 | */ |
||
220 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
228 | |||
229 | /** |
||
230 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
231 | * If-Modified-Since / If-None-Match request to validate the response. |
||
232 | * |
||
233 | * @param int|null $maxAge |
||
234 | * |
||
235 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
236 | */ |
||
237 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
245 | |||
246 | /** |
||
247 | * Verify that we can cache this response. |
||
248 | * |
||
249 | * @param ResponseInterface $response |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 6 | protected function isCacheable(ResponseInterface $response) |
|
268 | |||
269 | /** |
||
270 | * Get the value of a parameter in the cache control header. |
||
271 | * |
||
272 | * @param ResponseInterface $response |
||
273 | * @param string $name The field of Cache-Control to fetch |
||
274 | * |
||
275 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
276 | */ |
||
277 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
293 | |||
294 | /** |
||
295 | * @param RequestInterface $request |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 10 | private function createCacheKey(RequestInterface $request) |
|
305 | |||
306 | /** |
||
307 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
308 | * |
||
309 | * @param ResponseInterface $response |
||
310 | * |
||
311 | * @return int|null |
||
312 | */ |
||
313 | 6 | private function getMaxAge(ResponseInterface $response) |
|
338 | |||
339 | /** |
||
340 | * Configure an options resolver. |
||
341 | * |
||
342 | * @param OptionsResolver $resolver |
||
343 | */ |
||
344 | 14 | private function configureOptions(OptionsResolver $resolver) |
|
345 | { |
||
346 | 14 | $resolver->setDefaults([ |
|
347 | 14 | 'cache_lifetime' => 86400 * 30, // 30 days |
|
348 | 14 | 'default_ttl' => 0, |
|
349 | //Deprecated as of v1.3, to be removed in v2.0. Use respect_response_cache_directives instead |
||
350 | 14 | 'respect_cache_headers' => null, |
|
351 | 14 | 'hash_algo' => 'sha1', |
|
352 | 14 | 'methods' => ['GET', 'HEAD'], |
|
353 | 14 | 'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'], |
|
354 | 14 | 'cache_key_generator' => null, |
|
355 | 14 | 'cache_listeners' => [], |
|
356 | 14 | ]); |
|
357 | |||
358 | 14 | $resolver->setAllowedTypes('cache_lifetime', ['int', 'null']); |
|
359 | 14 | $resolver->setAllowedTypes('default_ttl', ['int', 'null']); |
|
360 | 14 | $resolver->setAllowedTypes('respect_cache_headers', ['bool', 'null']); |
|
361 | 14 | $resolver->setAllowedTypes('methods', 'array'); |
|
362 | 14 | $resolver->setAllowedTypes('cache_key_generator', ['null', 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator']); |
|
363 | 14 | $resolver->setAllowedValues('hash_algo', hash_algos()); |
|
364 | $resolver->setAllowedValues('methods', function ($value) { |
||
365 | /* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */ |
||
366 | 14 | $matches = preg_grep('/[^A-Z0-9!#$%&\'*+\-.^_`|~]/', $value); |
|
367 | |||
368 | 14 | return empty($matches); |
|
369 | 14 | }); |
|
370 | 14 | $resolver->setAllowedTypes('cache_listeners', ['array']); |
|
371 | |||
372 | $resolver->setNormalizer('respect_cache_headers', function (Options $options, $value) { |
||
373 | 14 | if (null !== $value) { |
|
374 | @trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED); |
||
375 | } |
||
376 | |||
377 | 14 | return null === $value ? true : $value; |
|
378 | 14 | }); |
|
379 | |||
380 | 14 | $resolver->setNormalizer('respect_response_cache_directives', function (Options $options, $value) { |
|
381 | 13 | if (false === $options['respect_cache_headers']) { |
|
382 | return []; |
||
383 | } |
||
384 | |||
385 | 13 | return $value; |
|
386 | 14 | }); |
|
387 | 14 | } |
|
388 | |||
389 | /** |
||
390 | * @param CacheItemInterface $cacheItem |
||
391 | * |
||
392 | * @return ResponseInterface |
||
393 | */ |
||
394 | 3 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
412 | |||
413 | /** |
||
414 | * Get the value of the "If-Modified-Since" header. |
||
415 | * |
||
416 | * @param CacheItemInterface $cacheItem |
||
417 | * |
||
418 | * @return string|null |
||
419 | */ |
||
420 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
433 | |||
434 | /** |
||
435 | * Get the ETag from the cached response. |
||
436 | * |
||
437 | * @param CacheItemInterface $cacheItem |
||
438 | * |
||
439 | * @return string|null |
||
440 | */ |
||
441 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
442 | { |
||
443 | 2 | $data = $cacheItem->get(); |
|
444 | // The isset() is to be removed in 2.0. |
||
445 | 2 | if (!isset($data['etag'])) { |
|
446 | return; |
||
447 | } |
||
448 | |||
449 | 2 | foreach ($data['etag'] as $etag) { |
|
450 | 2 | if (!empty($etag)) { |
|
451 | 2 | return $etag; |
|
452 | } |
||
453 | } |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Call the cache listeners, if they are set. |
||
458 | * |
||
459 | * @param RequestInterface $request |
||
460 | * @param ResponseInterface $response |
||
461 | * @param bool $cacheHit |
||
462 | * @param CacheItemInterface|null $cacheItem |
||
463 | * |
||
464 | * @return ResponseInterface |
||
465 | */ |
||
466 | 11 | private function handleCacheListeners(RequestInterface $request, ResponseInterface $response, $cacheHit, $cacheItem) |
|
474 | } |
||
475 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.