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 |
||
20 | final class CachePlugin implements Plugin |
||
21 | { |
||
22 | /** |
||
23 | * @var CacheItemPoolInterface |
||
24 | */ |
||
25 | private $pool; |
||
26 | |||
27 | /** |
||
28 | * @var StreamFactory |
||
29 | */ |
||
30 | private $streamFactory; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $config; |
||
36 | |||
37 | /** |
||
38 | * Cache directives indicating if a response can be cached |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $noCacheFlags = ['no-cache', 'private']; |
||
43 | |||
44 | /** |
||
45 | * @param CacheItemPoolInterface $pool |
||
46 | * @param StreamFactory $streamFactory |
||
47 | * @param array $config { |
||
48 | * |
||
49 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them. This option is deprecated, use `respect_response_cache_directives` instead |
||
50 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
51 | * value |
||
52 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
53 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
54 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
55 | * We store a cache item for $cache_lifetime + max age of the response. |
||
56 | * @var array $methods list of request methods which can be cached. |
||
57 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses. |
||
58 | * } |
||
59 | */ |
||
60 | 13 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
73 | |||
74 | /** |
||
75 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will cache responses with `private` cache directive |
||
76 | * |
||
77 | * @param CacheItemPoolInterface $pool |
||
78 | * @param StreamFactory $streamFactory |
||
79 | * @param array $config |
||
80 | * |
||
81 | * @return CachePlugin |
||
82 | */ |
||
83 | 1 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
99 | |||
100 | /** |
||
101 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior (refuses to cache responses with the `private`or `no-cache` directives |
||
102 | * |
||
103 | * @param CacheItemPoolInterface $pool |
||
104 | * @param StreamFactory $streamFactory |
||
105 | * @param array $config |
||
106 | * |
||
107 | * @return CachePlugin |
||
108 | */ |
||
109 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 10 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
192 | |||
193 | /** |
||
194 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
195 | * returned is $maxAge. |
||
196 | * |
||
197 | * @param int|null $maxAge |
||
198 | * |
||
199 | * @return int|null Unix system time passed to the PSR-6 cache |
||
200 | */ |
||
201 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
209 | |||
210 | /** |
||
211 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
212 | * If-Modified-Since / If-None-Match request to validate the response. |
||
213 | * |
||
214 | * @param int|null $maxAge |
||
215 | * |
||
216 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
217 | */ |
||
218 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
226 | |||
227 | /** |
||
228 | * Verify that we can cache this response. |
||
229 | * |
||
230 | * @param ResponseInterface $response |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | 6 | protected function isCacheable(ResponseInterface $response) |
|
249 | |||
250 | /** |
||
251 | * Get the value of a parameter in the cache control header. |
||
252 | * |
||
253 | * @param ResponseInterface $response |
||
254 | * @param string $name The field of Cache-Control to fetch |
||
255 | * |
||
256 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
257 | */ |
||
258 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
274 | |||
275 | /** |
||
276 | * @param RequestInterface $request |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 9 | private function createCacheKey(RequestInterface $request) |
|
289 | |||
290 | /** |
||
291 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
292 | * |
||
293 | * @param ResponseInterface $response |
||
294 | * |
||
295 | * @return int|null |
||
296 | */ |
||
297 | 6 | private function getMaxAge(ResponseInterface $response) |
|
322 | |||
323 | /** |
||
324 | * Configure an options resolver. |
||
325 | * |
||
326 | * @param OptionsResolver $resolver |
||
327 | */ |
||
328 | 13 | private function configureOptions(OptionsResolver $resolver) |
|
368 | |||
369 | /** |
||
370 | * @param CacheItemInterface $cacheItem |
||
371 | * |
||
372 | * @return ResponseInterface |
||
373 | */ |
||
374 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
384 | |||
385 | /** |
||
386 | * Get the value of the "If-Modified-Since" header. |
||
387 | * |
||
388 | * @param CacheItemInterface $cacheItem |
||
389 | * |
||
390 | * @return string|null |
||
391 | */ |
||
392 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
405 | |||
406 | /** |
||
407 | * Get the ETag from the cached response. |
||
408 | * |
||
409 | * @param CacheItemInterface $cacheItem |
||
410 | * |
||
411 | * @return string|null |
||
412 | */ |
||
413 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
431 | |||
432 | 5 | private function extractDirectives($directives, $extractDirectivesList) |
|
436 | } |
||
437 |
If you suppress an error, we recommend checking for the error condition explicitly: