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 | * @param CacheItemPoolInterface $pool |
||
39 | * @param StreamFactory $streamFactory |
||
40 | * @param array $config { |
||
41 | * |
||
42 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
43 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
44 | * value |
||
45 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
46 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
47 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
48 | * We store a cache item for $cache_lifetime + max age of the response. |
||
49 | * @var array $methods list of request methods which can be cached. |
||
50 | * } |
||
51 | 12 | */ |
|
52 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
61 | |||
62 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
78 | |||
79 | 3 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
83 | |||
84 | /** |
||
85 | 2 | * {@inheritdoc} |
|
86 | 2 | */ |
|
87 | 2 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
162 | |||
163 | /** |
||
164 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
165 | 5 | * returned is $maxAge. |
|
166 | * |
||
167 | 5 | * @param int|null $maxAge |
|
168 | * |
||
169 | * @return int|null Unix system time passed to the PSR-6 cache |
||
170 | */ |
||
171 | 5 | private function calculateCacheItemExpiresAfter($maxAge) |
|
179 | |||
180 | /** |
||
181 | 5 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
|
182 | * If-Modified-Since / If-None-Match request to validate the response. |
||
183 | 5 | * |
|
184 | 1 | * @param int|null $maxAge |
|
185 | * |
||
186 | 4 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
|
187 | */ |
||
188 | private function calculateResponseExpiresAt($maxAge) |
||
196 | |||
197 | /** |
||
198 | * Verify that we can cache this response. |
||
199 | * |
||
200 | * @param ResponseInterface $response |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | 5 | protected function isCacheable(ResponseInterface $response) |
|
221 | |||
222 | /** |
||
223 | * Get the value of a parameter in the cache control header. |
||
224 | * |
||
225 | * @param ResponseInterface $response |
||
226 | 8 | * @param string $name The field of Cache-Control to fetch |
|
227 | * |
||
228 | 8 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
|
229 | 8 | */ |
|
230 | 1 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
246 | |||
247 | /** |
||
248 | * @param RequestInterface $request |
||
249 | * |
||
250 | 5 | * @return string |
|
251 | 5 | */ |
|
252 | 1 | private function createCacheKey(RequestInterface $request) |
|
261 | 4 | ||
262 | 4 | /** |
|
263 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
264 | 4 | * |
|
265 | * @param ResponseInterface $response |
||
266 | 4 | * |
|
267 | * @return int|null |
||
268 | */ |
||
269 | private function getMaxAge(ResponseInterface $response) |
||
294 | 12 | ||
295 | 12 | /** |
|
296 | * Configure an options resolver. |
||
297 | * |
||
298 | * @param OptionsResolver $resolver |
||
299 | */ |
||
300 | private function configureOptions(OptionsResolver $resolver) |
||
332 | |||
333 | /** |
||
334 | * @param CacheItemInterface $cacheItem |
||
335 | * |
||
336 | * @return ResponseInterface |
||
337 | */ |
||
338 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
||
348 | |||
349 | 2 | /** |
|
350 | * Get the value of the "If-Modified-Since" header. |
||
351 | * |
||
352 | * @param CacheItemInterface $cacheItem |
||
353 | 2 | * |
|
354 | 2 | * @return string|null |
|
355 | 2 | */ |
|
356 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
||
369 | |||
370 | /** |
||
371 | * Get the ETag from the cached response. |
||
372 | * |
||
373 | * @param CacheItemInterface $cacheItem |
||
374 | * |
||
375 | * @return string|null |
||
376 | */ |
||
377 | private function getETag(CacheItemInterface $cacheItem) |
||
395 | } |
||
396 |
If you suppress an error, we recommend checking for the error condition explicitly: