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 |
||
| 22 | class Cache implements \DrupalCacheInterface { |
||
| 23 | /** |
||
| 24 | * The name of the collection holding the cache data. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $bin; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * A closure wrapping MongoBinData::__construct() with its default $type. |
||
| 32 | * |
||
| 33 | * @var \Closure |
||
| 34 | */ |
||
| 35 | protected $binDataCreator; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The collection holding the cache data. |
||
| 39 | * |
||
| 40 | * @var \MongoCollection|\MongoDebugCollection|\MongoDummy |
||
| 41 | */ |
||
| 42 | protected $collection; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Has a connection exception already been notified ? |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | * |
||
| 49 | * @see \Drupal\mongodb_cache\Cache::notifyException() |
||
| 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 | public function __construct($bin) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Display an exception error message only once. |
||
| 103 | * |
||
| 104 | * @param \MongoConnectionException $e |
||
| 105 | */ |
||
| 106 | protected static function notifyException(\MongoConnectionException $e) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * An alternate \MongoBinData constructor using default $type. |
||
| 117 | * |
||
| 118 | * @param mixed $data |
||
| 119 | * The data to convert to \MongoBinData. |
||
| 120 | * |
||
| 121 | * @return \Closure |
||
| 122 | * The alternate constructor with $type following the extension version. |
||
| 123 | */ |
||
| 124 | protected function createBinData($data) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Return the proper MongoBinData constructor with its type argument. |
||
| 132 | * |
||
| 133 | * The signature of \MongoBinData::__construct() changed in 1.2.11 to require |
||
| 134 | * $type and default to BYTE_ARRAY, then again in 1.5.0 to default to GENERIC. |
||
| 135 | * |
||
| 136 | * @return \Closure |
||
| 137 | * A closure wrapping the constructor with its expected $type. |
||
| 138 | */ |
||
| 139 | protected function getBinDataCreator() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return the timestamp of the latest flush. |
||
| 159 | * |
||
| 160 | * @return int |
||
| 161 | * A UNIX timestamp. |
||
| 162 | */ |
||
| 163 | protected function getFlushTimestamp() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Record a timestamp as marking the latest flush for the current bin. |
||
| 170 | * |
||
| 171 | * As this performs a variable_set(), it is a costly operation. |
||
| 172 | * |
||
| 173 | * @param int $timestamp |
||
| 174 | * A UNIX timestamp. May be 0. |
||
| 175 | */ |
||
| 176 | protected function setFlushTimestamp($timestamp) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function get($cid) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function getMultiple(&$cids) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Garbage collection for get() and getMultiple(). |
||
| 232 | */ |
||
| 233 | protected function garbageCollection() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Prepare a cached item. |
||
| 261 | * |
||
| 262 | * Checks that items are either permanent not yet expired, and unserializes |
||
| 263 | * data as appropriate. |
||
| 264 | * |
||
| 265 | * @param array|null $cache |
||
| 266 | * An item loaded from cache_get() or cache_get_multiple(). |
||
| 267 | * |
||
| 268 | * @return false|object |
||
| 269 | * The item with data unserialized as appropriate or FALSE if there is no |
||
| 270 | * valid item to load. |
||
| 271 | */ |
||
| 272 | protected function prepareItem($cache) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function set($cid, $data, $expire = CACHE_PERMANENT) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Attempt removing data from the collection, notifying on exceptions. |
||
| 333 | * |
||
| 334 | * @param array|null $criteria |
||
| 335 | * NULL means to remove all documents from the collection. |
||
| 336 | */ |
||
| 337 | protected function attemptRemove($criteria = NULL) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | public function clear($cid = NULL, $wildcard = FALSE) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritdoc} |
||
| 424 | */ |
||
| 425 | public function isEmpty() { |
||
| 438 | |||
| 439 | } |
||
| 440 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.