Complex classes like TMemCache 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 TMemCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 89 | class TMemCache extends TCache |
||
| 90 | { |
||
| 91 | /** |
||
| 92 | * @var bool if the module is initialized |
||
| 93 | */ |
||
| 94 | private $_initialized = false; |
||
| 95 | /** |
||
| 96 | * @var Memcache the Memcache instance |
||
| 97 | */ |
||
| 98 | private $_cache; |
||
| 99 | /** |
||
| 100 | * @var string host name of the memcache server |
||
| 101 | */ |
||
| 102 | private $_host = 'localhost'; |
||
| 103 | /** |
||
| 104 | * @var int the port number of the memcache server |
||
| 105 | */ |
||
| 106 | private $_port = 11211; |
||
| 107 | |||
| 108 | private $_timeout = 360; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var int Controls the minimum value length before attempting to compress automatically. |
||
| 112 | */ |
||
| 113 | private $_threshold = 0; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var float Specifies the minimum amount of savings to actually store the value compressed. The supplied value must be between 0 and 1. Default value is 0.2 giving a minimum 20% compression savings. |
||
| 117 | */ |
||
| 118 | private $_minSavings = 0.0; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var array list of servers available |
||
| 122 | */ |
||
| 123 | private $_servers = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Destructor. |
||
| 127 | * Disconnect the memcache server. |
||
| 128 | */ |
||
| 129 | public function __destruct() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Initializes this module. |
||
| 139 | * This method is required by the IModule interface. It makes sure that |
||
| 140 | * UniquePrefix has been set, creates a Memcache instance and connects |
||
| 141 | * to the memcache server. |
||
| 142 | * @param TXmlElement $config configuration for this module, can be null |
||
| 143 | * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails |
||
| 144 | */ |
||
| 145 | public function init($config) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Loads configuration from an XML element |
||
| 182 | * @param TXmlElement $xml configuration node |
||
| 183 | * @throws TConfigurationException if log route class or type is not specified |
||
| 184 | */ |
||
| 185 | private function loadConfig($xml) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string host name of the memcache server |
||
| 221 | */ |
||
| 222 | public function getHost() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $value host name of the memcache server |
||
| 229 | * @throws TInvalidOperationException if the module is already initialized |
||
| 230 | */ |
||
| 231 | public function setHost($value) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return int port number of the memcache server |
||
| 242 | */ |
||
| 243 | public function getPort() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param int $value port number of the memcache server |
||
| 250 | * @throws TInvalidOperationException if the module is already initialized |
||
| 251 | */ |
||
| 252 | public function setPort($value) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return bool if memcached is used instead of memcache |
||
| 263 | * @deprecated since Prado 4.1, only memecached is available |
||
| 264 | */ |
||
| 265 | public function getUseMemcached() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $value if memcached instead memcache |
||
| 272 | * @throws TInvalidOperationException if the module is already initialized or usage of the old, unsupported memcache extension has been requested |
||
| 273 | * @deprecated since Prado 4.1, only memecached is available |
||
| 274 | */ |
||
| 275 | public function setUseMemcached($value) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return int minimum value length before attempting to compress |
||
| 284 | */ |
||
| 285 | public function getThreshold() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param int $value minimum value length before attempting to compress |
||
| 292 | * @throws TInvalidOperationException if the module is already initialized |
||
| 293 | */ |
||
| 294 | public function setThreshold($value) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return float minimum amount of savings to actually store the value compressed |
||
| 305 | */ |
||
| 306 | public function getMinSavings() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param float $value minimum amount of savings to actually store the value compressed |
||
| 313 | * @throws TInvalidOperationException if the module is already initialized |
||
| 314 | */ |
||
| 315 | public function setMinSavings($value) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Retrieves a value from cache with a specified key. |
||
| 326 | * This is the implementation of the method declared in the parent class. |
||
| 327 | * @param string $key a unique key identifying the cached value |
||
| 328 | * @return string the value stored in cache, false if the value is not in the cache or expired. |
||
| 329 | */ |
||
| 330 | protected function getValue($key) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Stores a value identified by a key in cache. |
||
| 337 | * This is the implementation of the method declared in the parent class. |
||
| 338 | * |
||
| 339 | * @param string $key the key identifying the value to be cached |
||
| 340 | * @param string $value the value to be cached |
||
| 341 | * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire. |
||
| 342 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
| 343 | */ |
||
| 344 | protected function setValue($key, $value, $expire) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
| 351 | * This is the implementation of the method declared in the parent class. |
||
| 352 | * |
||
| 353 | * @param string $key the key identifying the value to be cached |
||
| 354 | * @param string $value the value to be cached |
||
| 355 | * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire. |
||
| 356 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
| 357 | */ |
||
| 358 | protected function addValue($key, $value, $expire) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Deletes a value with the specified key from cache |
||
| 365 | * This is the implementation of the method declared in the parent class. |
||
| 366 | * @param string $key the key of the value to be deleted |
||
| 367 | * @return bool if no error happens during deletion |
||
| 368 | */ |
||
| 369 | protected function deleteValue($key) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Deletes all values from cache. |
||
| 376 | * Be careful of performing this operation if the cache is shared by multiple applications. |
||
| 377 | */ |
||
| 378 | public function flush() |
||
| 382 | } |
||
| 383 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..