Complex classes like Cache 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 Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Cache implements \DrupalCacheInterface { |
||
| 22 | /** |
||
| 23 | * The name of the collection holding the cache data. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $bin; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A closure wrapping MongoBinData::__construct() with its default $type. |
||
| 31 | * |
||
| 32 | * @var \Closure |
||
| 33 | */ |
||
| 34 | protected $binDataCreator; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The collection holding the cache data. |
||
| 38 | * |
||
| 39 | * @var \MongoCollection|\MongoDebugCollection|\MongodbDummy |
||
| 40 | */ |
||
| 41 | protected $collection; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Has a connection exception already been notified ? |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | * |
||
| 48 | * @see \Drupal\mongodb_cache\Cache::notifyException() |
||
| 49 | * @see \Drupal\mongodb_cache\Cache::hasException() |
||
| 50 | * |
||
| 51 | * This is a static, because the plugin assumes that connection errors will be |
||
| 52 | * share between all bins, under the hypothesis that all bins will be using |
||
| 53 | * the same connection. |
||
| 54 | */ |
||
| 55 | protected static $isExceptionNotified = FALSE; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The default write options for this collection: unsafe mode. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | * |
||
| 62 | * @see self::__construct() |
||
| 63 | */ |
||
| 64 | protected $unsafe; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The name of the state variable holding the latest bin expire timestamp. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $flushVarName; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The number of seconds during which a new flush will be ignored. |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | * |
||
| 78 | * @see self::__construct() |
||
| 79 | */ |
||
| 80 | protected $stampedeDelay; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Constructor. |
||
| 84 | * |
||
| 85 | * @param string $bin |
||
| 86 | * The name of the cache bin for which to build a backend. |
||
| 87 | * |
||
| 88 | * @throws \MongoConnectionException |
||
|
|
|||
| 89 | */ |
||
| 90 | public function __construct($bin) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Display an exception error message only once. |
||
| 111 | * |
||
| 112 | * @param \MongoException $e |
||
| 113 | * The exception to notify to the user. |
||
| 114 | */ |
||
| 115 | protected static function notifyException(\MongoException $e) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * An alternate \MongoBinData constructor using default $type. |
||
| 126 | * |
||
| 127 | * @param mixed $data |
||
| 128 | * The data to convert to \MongoBinData. |
||
| 129 | * |
||
| 130 | * @return \Closure |
||
| 131 | * The alternate constructor with $type following the extension version. |
||
| 132 | */ |
||
| 133 | protected function createBinData($data) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return the proper MongoBinData constructor with its type argument. |
||
| 141 | * |
||
| 142 | * The signature of \MongoBinData::__construct() changed in 1.2.11 to require |
||
| 143 | * $type and default to BYTE_ARRAY, then again in 1.5.0 to default to GENERIC. |
||
| 144 | * |
||
| 145 | * @return \Closure |
||
| 146 | * A closure wrapping the constructor with its expected $type. |
||
| 147 | */ |
||
| 148 | protected function getBinDataCreator() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the timestamp of the latest flush. |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | * A UNIX timestamp. |
||
| 172 | */ |
||
| 173 | protected function getFlushTimestamp() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Record a timestamp as marking the latest flush for the current bin. |
||
| 180 | * |
||
| 181 | * As this performs a variable_set(), it is a costly operation. |
||
| 182 | * |
||
| 183 | * @param int $timestamp |
||
| 184 | * A UNIX timestamp. May be 0. |
||
| 185 | */ |
||
| 186 | protected function setFlushTimestamp($timestamp) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | public function get($cid) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function getMultiple(&$cids) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Garbage collection for get() and getMultiple(). |
||
| 242 | * |
||
| 243 | * @throws \MongoCursorException |
||
| 244 | * @throws \MongoCursorTimeoutException |
||
| 245 | */ |
||
| 246 | protected function garbageCollection() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Prepare a cached item. |
||
| 274 | * |
||
| 275 | * Checks that items are either permanent not yet expired, and unserializes |
||
| 276 | * data as appropriate. |
||
| 277 | * |
||
| 278 | * @param array|null $cache |
||
| 279 | * An item loaded from cache_get() or cache_get_multiple(). |
||
| 280 | * |
||
| 281 | * @return false|object |
||
| 282 | * The item with data unserialized as appropriate or FALSE if there is no |
||
| 283 | * valid item to load. |
||
| 284 | */ |
||
| 285 | protected function prepareItem($cache) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | public function set($cid, $data, $expire = CACHE_PERMANENT) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Attempt removing data from the collection, notifying on exceptions. |
||
| 346 | * |
||
| 347 | * @param array|null $criteria |
||
| 348 | * NULL means to remove all documents from the collection. |
||
| 349 | * |
||
| 350 | * @throws \MongoCursorException |
||
| 351 | * @throws \MongoCursorTimeoutException |
||
| 352 | */ |
||
| 353 | protected function attemptRemove($criteria = NULL) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | */ |
||
| 370 | public function clear($cid = NULL, $wildcard = FALSE) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Has the plugin thrown an exception at any point ? |
||
| 445 | * |
||
| 446 | * @retun bool |
||
| 447 | * Has it ? |
||
| 448 | * |
||
| 449 | * @see mongodb_cache_exit() |
||
| 450 | */ |
||
| 451 | public static function hasException() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * {@inheritdoc} |
||
| 457 | */ |
||
| 458 | public function isEmpty() { |
||
| 471 | |||
| 472 | } |
||
| 473 |