Complex classes like HttpCache 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 HttpCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class HttpCache implements HttpKernelInterface, TerminableInterface |
||
| 29 | { |
||
| 30 | private $kernel; |
||
| 31 | private $store; |
||
| 32 | private $request; |
||
| 33 | private $surrogate; |
||
| 34 | private $surrogateCacheStrategy; |
||
| 35 | private $options = array(); |
||
| 36 | private $traces = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor. |
||
| 40 | * |
||
| 41 | * The available options are: |
||
| 42 | * |
||
| 43 | * * debug: If true, the traces are added as a HTTP header to ease debugging |
||
| 44 | * |
||
| 45 | * * default_ttl The number of seconds that a cache entry should be considered |
||
| 46 | * fresh when no explicit freshness information is provided in |
||
| 47 | * a response. Explicit Cache-Control or Expires headers |
||
| 48 | * override this value. (default: 0) |
||
| 49 | * |
||
| 50 | * * private_headers Set of request headers that trigger "private" cache-control behavior |
||
| 51 | * on responses that don't explicitly state whether the response is |
||
| 52 | * public or private via a Cache-Control directive. (default: Authorization and Cookie) |
||
| 53 | * |
||
| 54 | * * allow_reload Specifies whether the client can force a cache reload by including a |
||
| 55 | * Cache-Control "no-cache" directive in the request. Set it to ``true`` |
||
| 56 | * for compliance with RFC 2616. (default: false) |
||
| 57 | * |
||
| 58 | * * allow_revalidate Specifies whether the client can force a cache revalidate by including |
||
| 59 | * a Cache-Control "max-age=0" directive in the request. Set it to ``true`` |
||
| 60 | * for compliance with RFC 2616. (default: false) |
||
| 61 | * |
||
| 62 | * * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the |
||
| 63 | * Response TTL precision is a second) during which the cache can immediately return |
||
| 64 | * a stale response while it revalidates it in the background (default: 2). |
||
| 65 | * This setting is overridden by the stale-while-revalidate HTTP Cache-Control |
||
| 66 | * extension (see RFC 5861). |
||
| 67 | * |
||
| 68 | * * stale_if_error Specifies the default number of seconds (the granularity is the second) during which |
||
| 69 | * the cache can serve a stale response when an error is encountered (default: 60). |
||
| 70 | * This setting is overridden by the stale-if-error HTTP Cache-Control extension |
||
| 71 | * (see RFC 5861). |
||
| 72 | * |
||
| 73 | * @param HttpKernelInterface $kernel An HttpKernelInterface instance |
||
| 74 | * @param StoreInterface $store A Store instance |
||
| 75 | * @param SurrogateInterface $surrogate A SurrogateInterface instance |
||
| 76 | * @param array $options An array of options |
||
| 77 | */ |
||
| 78 | public function __construct(HttpKernelInterface $kernel, StoreInterface $store, SurrogateInterface $surrogate = null, array $options = array()) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Gets the current store. |
||
| 100 | * |
||
| 101 | * @return StoreInterface $store A StoreInterface instance |
||
| 102 | */ |
||
| 103 | public function getStore() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns an array of events that took place during processing of the last request. |
||
| 110 | * |
||
| 111 | * @return array An array of events |
||
| 112 | */ |
||
| 113 | public function getTraces() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns a log message for the events of the last request processing. |
||
| 120 | * |
||
| 121 | * @return string A log message |
||
| 122 | */ |
||
| 123 | public function getLog() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Gets the Request instance associated with the master request. |
||
| 135 | * |
||
| 136 | * @return Request A Request instance |
||
| 137 | */ |
||
| 138 | public function getRequest() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Gets the Kernel instance. |
||
| 145 | * |
||
| 146 | * @return HttpKernelInterface An HttpKernelInterface instance |
||
| 147 | */ |
||
| 148 | public function getKernel() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Gets the Surrogate instance. |
||
| 155 | * |
||
| 156 | * @return SurrogateInterface A Surrogate instance |
||
| 157 | * |
||
| 158 | * @throws \LogicException |
||
| 159 | */ |
||
| 160 | public function getSurrogate() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the Esi instance. |
||
| 171 | * |
||
| 172 | * @return Esi An Esi instance |
||
| 173 | * |
||
| 174 | * @throws \LogicException |
||
| 175 | * |
||
| 176 | * @deprecated since version 2.6, to be removed in 3.0. Use getSurrogate() instead |
||
| 177 | */ |
||
| 178 | public function getEsi() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function terminate(Request $request, Response $response) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Forwards the Request to the backend without storing the Response in the cache. |
||
| 248 | * |
||
| 249 | * @param Request $request A Request instance |
||
| 250 | * @param bool $catch Whether to process exceptions |
||
| 251 | * |
||
| 252 | * @return Response A Response instance |
||
| 253 | */ |
||
| 254 | protected function pass(Request $request, $catch = false) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Invalidates non-safe methods (like POST, PUT, and DELETE). |
||
| 263 | * |
||
| 264 | * @param Request $request A Request instance |
||
| 265 | * @param bool $catch Whether to process exceptions |
||
| 266 | * |
||
| 267 | * @return Response A Response instance |
||
| 268 | * |
||
| 269 | * @throws \Exception |
||
| 270 | * |
||
| 271 | * @see RFC2616 13.10 |
||
| 272 | */ |
||
| 273 | protected function invalidate(Request $request, $catch = false) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Lookups a Response from the cache for the given Request. |
||
| 306 | * |
||
| 307 | * When a matching cache entry is found and is fresh, it uses it as the |
||
| 308 | * response without forwarding any request to the backend. When a matching |
||
| 309 | * cache entry is found but is stale, it attempts to "validate" the entry with |
||
| 310 | * the backend using conditional GET. When no matching cache entry is found, |
||
| 311 | * it triggers "miss" processing. |
||
| 312 | * |
||
| 313 | * @param Request $request A Request instance |
||
| 314 | * @param bool $catch whether to process exceptions |
||
| 315 | * |
||
| 316 | * @return Response A Response instance |
||
| 317 | * |
||
| 318 | * @throws \Exception |
||
| 319 | */ |
||
| 320 | protected function lookup(Request $request, $catch = false) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Validates that a cache entry is fresh. |
||
| 362 | * |
||
| 363 | * The original request is used as a template for a conditional |
||
| 364 | * GET request with the backend. |
||
| 365 | * |
||
| 366 | * @param Request $request A Request instance |
||
| 367 | * @param Response $entry A Response instance to validate |
||
| 368 | * @param bool $catch Whether to process exceptions |
||
| 369 | * |
||
| 370 | * @return Response A Response instance |
||
| 371 | */ |
||
| 372 | protected function validate(Request $request, Response $entry, $catch = false) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Forwards the Request to the backend and determines whether the response should be stored. |
||
| 427 | * |
||
| 428 | * This methods is triggered when the cache missed or a reload is required. |
||
| 429 | * |
||
| 430 | * @param Request $request A Request instance |
||
| 431 | * @param bool $catch whether to process exceptions |
||
| 432 | * |
||
| 433 | * @return Response A Response instance |
||
| 434 | */ |
||
| 435 | protected function fetch(Request $request, $catch = false) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Forwards the Request to the backend and returns the Response. |
||
| 459 | * |
||
| 460 | * @param Request $request A Request instance |
||
| 461 | * @param bool $catch Whether to catch exceptions or not |
||
| 462 | * @param Response $entry A Response instance (the stale entry if present, null otherwise) |
||
| 463 | * |
||
| 464 | * @return Response A Response instance |
||
| 465 | */ |
||
| 466 | protected function forward(Request $request, $catch = false, Response $entry = null) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Checks whether the cache entry is "fresh enough" to satisfy the Request. |
||
| 520 | * |
||
| 521 | * @param Request $request A Request instance |
||
| 522 | * @param Response $entry A Response instance |
||
| 523 | * |
||
| 524 | * @return bool true if the cache entry if fresh enough, false otherwise |
||
| 525 | */ |
||
| 526 | protected function isFreshEnough(Request $request, Response $entry) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Locks a Request during the call to the backend. |
||
| 541 | * |
||
| 542 | * @param Request $request A Request instance |
||
| 543 | * @param Response $entry A Response instance |
||
| 544 | * |
||
| 545 | * @return bool true if the cache entry can be returned even if it is staled, false otherwise |
||
| 546 | */ |
||
| 547 | protected function lock(Request $request, Response $entry) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Writes the Response to the cache. |
||
| 599 | * |
||
| 600 | * @param Request $request A Request instance |
||
| 601 | * @param Response $response A Response instance |
||
| 602 | * |
||
| 603 | * @throws \Exception |
||
| 604 | */ |
||
| 605 | protected function store(Request $request, Response $response) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Restores the Response body. |
||
| 630 | * |
||
| 631 | * @param Request $request A Request instance |
||
| 632 | * @param Response $response A Response instance |
||
| 633 | */ |
||
| 634 | private function restoreResponseBody(Request $request, Response $response) |
||
| 666 | |||
| 667 | protected function processResponseBody(Request $request, Response $response) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Checks if the Request includes authorization or other sensitive information |
||
| 676 | * that should cause the Response to be considered private by default. |
||
| 677 | * |
||
| 678 | * @param Request $request A Request instance |
||
| 679 | * |
||
| 680 | * @return bool true if the Request is private, false otherwise |
||
| 681 | */ |
||
| 682 | private function isPrivateRequest(Request $request) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Records that an event took place. |
||
| 701 | * |
||
| 702 | * @param Request $request A Request instance |
||
| 703 | * @param string $event The event name |
||
| 704 | */ |
||
| 705 | private function record(Request $request, $event) |
||
| 713 | } |
||
| 714 |
If you suppress an error, we recommend checking for the error condition explicitly: