Complex classes like HtmlCache 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 HtmlCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class HtmlCache extends Filesystem |
||
20 | { |
||
21 | protected $extension = '.html'; |
||
22 | |||
23 | public function getExtension() |
||
27 | |||
28 | public function setExtension($extension) |
||
32 | |||
33 | /** |
||
34 | * Set options. |
||
35 | * |
||
36 | * @param FilesystemOptions $options |
||
37 | * @return Filesystem |
||
38 | * @see getOptions() |
||
39 | */ |
||
40 | 1 | public function setOptions($options) |
|
48 | |||
49 | /** |
||
50 | * Get options. |
||
51 | * |
||
52 | * @return FilesystemOptions |
||
53 | * @see setOptions() |
||
54 | */ |
||
55 | 1 | public function getOptions() |
|
62 | /* FlushableInterface */ |
||
63 | |||
64 | /** |
||
65 | * Flush the whole storage |
||
66 | * |
||
67 | * @throws Exception\RuntimeException |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function flush() |
||
100 | /* ClearExpiredInterface */ |
||
101 | |||
102 | /** |
||
103 | * Remove expired items |
||
104 | * |
||
105 | * @return bool |
||
106 | * |
||
107 | * @triggers clearExpired.exception(ExceptionEvent) |
||
108 | */ |
||
109 | public function clearExpired() |
||
153 | |||
154 | /** |
||
155 | * Remove items matching given tags. |
||
156 | * |
||
157 | * If $disjunction only one of the given tags must match |
||
158 | * else all given tags must match. |
||
159 | * |
||
160 | * @param string[] $tags |
||
161 | * @param bool $disjunction |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function clearByTags(array $tags, $disjunction = false) |
||
206 | /* IterableInterface */ |
||
207 | |||
208 | /** |
||
209 | * Get the storage iterator |
||
210 | * |
||
211 | * @return FilesystemIterator |
||
212 | */ |
||
213 | public function getIterator() |
||
223 | |||
224 | /** |
||
225 | * Internal method to get an item. |
||
226 | * |
||
227 | * @param string $normalizedKey |
||
228 | * @param bool $success |
||
229 | * @param mixed $casToken |
||
230 | * @return null|string Data on success, null on failure |
||
231 | * @throws Exception\ExceptionInterface |
||
232 | */ |
||
233 | protected function internalGetItem( |
||
258 | |||
259 | /** |
||
260 | * Internal method to get multiple items. |
||
261 | * |
||
262 | * @param array $normalizedKeys |
||
263 | * @return array Associative array of keys and values |
||
264 | * @throws Exception\ExceptionInterface |
||
265 | */ |
||
266 | protected function internalGetItems(array & $normalizedKeys) |
||
303 | |||
304 | protected function internalHasItem(& $normalizedKey) |
||
331 | |||
332 | /** |
||
333 | * Get metadata |
||
334 | * |
||
335 | * @param string $key |
||
336 | * @return array|bool Metadata on success, false on failure |
||
337 | */ |
||
338 | public function getMetadata($key) |
||
347 | |||
348 | /** |
||
349 | * Get metadatas |
||
350 | * |
||
351 | * @param array $keys |
||
352 | * @param array $options |
||
353 | * @return array Associative array of keys and metadata |
||
354 | */ |
||
355 | public function getMetadatas(array $keys, array $options = array()) |
||
364 | |||
365 | /** |
||
366 | * Get info by key |
||
367 | * |
||
368 | * @param string $normalizedKey |
||
369 | * @return array|bool Metadata on success, false on failure |
||
370 | */ |
||
371 | protected function internalGetMetadata(& $normalizedKey) |
||
396 | |||
397 | /** |
||
398 | * Internal method to get multiple metadata |
||
399 | * |
||
400 | * @param array $normalizedKeys |
||
401 | * @return array Associative array of keys and metadata |
||
402 | * @throws Exception\ExceptionInterface |
||
403 | */ |
||
404 | protected function internalGetMetadatas(array & $normalizedKeys) |
||
431 | |||
432 | /** |
||
433 | * Internal method to store an item. |
||
434 | * |
||
435 | * @param string $normalizedKey |
||
436 | * @param mixed $value |
||
437 | * @return bool |
||
438 | * @throws Exception\ExceptionInterface |
||
439 | */ |
||
440 | protected function internalSetItem(& $normalizedKey, & $value) |
||
464 | |||
465 | /** |
||
466 | * Internal method to store multiple items. |
||
467 | * |
||
468 | * @param array $normalizedKeyValuePairs |
||
469 | * @return array Array of not stored keys |
||
470 | * @throws Exception\ExceptionInterface |
||
471 | */ |
||
472 | protected function internalSetItems(array & $normalizedKeyValuePairs) |
||
504 | |||
505 | /** |
||
506 | * Set an item only if token matches |
||
507 | * |
||
508 | * It uses the token received from getItem() to check if the item has |
||
509 | * changed before overwriting it. |
||
510 | * |
||
511 | * @param mixed $token |
||
512 | * @param string $key |
||
513 | * @param mixed $value |
||
514 | * @return bool |
||
515 | * @throws Exception\ExceptionInterface |
||
516 | * @see getItem() |
||
517 | * @see setItem() |
||
518 | */ |
||
519 | public function checkAndSetItem($token, $key, $value) |
||
528 | |||
529 | /** |
||
530 | * Internal method to set an item only if token matches |
||
531 | * |
||
532 | * @param mixed $token |
||
533 | * @param string $normalizedKey |
||
534 | * @param mixed $value |
||
535 | * @return bool |
||
536 | * @throws Exception\ExceptionInterface |
||
537 | * @see getItem() |
||
538 | * @see setItem() |
||
539 | */ |
||
540 | protected function internalCheckAndSetItem( |
||
558 | |||
559 | /** |
||
560 | * Reset lifetime of an item |
||
561 | * |
||
562 | * @param string $key |
||
563 | * @return bool |
||
564 | * @throws Exception\ExceptionInterface |
||
565 | * |
||
566 | * @triggers touchItem.pre(PreEvent) |
||
567 | * @triggers touchItem.post(PostEvent) |
||
568 | * @triggers touchItem.exception(ExceptionEvent) |
||
569 | */ |
||
570 | public function touchItem($key) |
||
579 | |||
580 | /** |
||
581 | * Reset lifetime of multiple items. |
||
582 | * |
||
583 | * @param array $keys |
||
584 | * @return array Array of not updated keys |
||
585 | * @throws Exception\ExceptionInterface |
||
586 | * |
||
587 | * @triggers touchItems.pre(PreEvent) |
||
588 | * @triggers touchItems.post(PostEvent) |
||
589 | * @triggers touchItems.exception(ExceptionEvent) |
||
590 | */ |
||
591 | public function touchItems(array $keys) |
||
600 | |||
601 | /** |
||
602 | * Internal method to reset lifetime of an item |
||
603 | * |
||
604 | * @param string $normalizedKey |
||
605 | * @return bool |
||
606 | * @throws Exception\ExceptionInterface |
||
607 | */ |
||
608 | protected function internalTouchItem(& $normalizedKey) |
||
629 | |||
630 | /** |
||
631 | * Remove an item. |
||
632 | * |
||
633 | * @param string $key |
||
634 | * @return bool |
||
635 | * @throws Exception\ExceptionInterface |
||
636 | * |
||
637 | * @triggers removeItem.pre(PreEvent) |
||
638 | * @triggers removeItem.post(PostEvent) |
||
639 | * @triggers removeItem.exception(ExceptionEvent) |
||
640 | */ |
||
641 | public function removeItem($key) |
||
650 | |||
651 | /** |
||
652 | * Remove multiple items. |
||
653 | * |
||
654 | * @param array $keys |
||
655 | * @return array Array of not removed keys |
||
656 | * @throws Exception\ExceptionInterface |
||
657 | * |
||
658 | * @triggers removeItems.pre(PreEvent) |
||
659 | * @triggers removeItems.post(PostEvent) |
||
660 | * @triggers removeItems.exception(ExceptionEvent) |
||
661 | */ |
||
662 | public function removeItems(array $keys) |
||
671 | |||
672 | /** |
||
673 | * Internal method to remove an item. |
||
674 | * |
||
675 | * @param string $normalizedKey |
||
676 | * @return bool |
||
677 | * @throws Exception\ExceptionInterface |
||
678 | */ |
||
679 | protected function internalRemoveItem(& $normalizedKey) |
||
690 | |||
691 | protected function getFileSpec($normalizedKey) |
||
699 | } |
||
700 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.