Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CI_Session_redis_driver 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 CI_Session_redis_driver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class CI_Session_redis_driver extends CI_Session_driver implements SessionHandlerInterface { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * phpRedis instance |
||
| 53 | * |
||
| 54 | * @var resource |
||
| 55 | */ |
||
| 56 | protected $_redis; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Key prefix |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $_key_prefix = 'ci_session:'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Lock key |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $_lock_key; |
||
| 71 | |||
| 72 | // ------------------------------------------------------------------------ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Class constructor |
||
| 76 | * |
||
| 77 | * @param array $params Configuration parameters |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function __construct(&$params) |
||
| 111 | |||
| 112 | // ------------------------------------------------------------------------ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Open |
||
| 116 | * |
||
| 117 | * Sanitizes save_path and initializes connection. |
||
| 118 | * |
||
| 119 | * @param string $save_path Server path |
||
| 120 | * @param string $name Session cookie name, unused |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function open($save_path, $name) |
||
| 151 | |||
| 152 | // ------------------------------------------------------------------------ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Read |
||
| 156 | * |
||
| 157 | * Reads session data and acquires a lock |
||
| 158 | * |
||
| 159 | * @param string $session_id Session ID |
||
| 160 | * @return string Serialized session data |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function read($session_id) |
|
| 176 | |||
| 177 | // ------------------------------------------------------------------------ |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Write |
||
| 181 | * |
||
| 182 | * Writes (create / update) session data |
||
| 183 | * |
||
| 184 | * @param string $session_id Session ID |
||
| 185 | * @param string $session_data Serialized session data |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function write($session_id, $session_data) |
|
| 227 | |||
| 228 | // ------------------------------------------------------------------------ |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Close |
||
| 232 | * |
||
| 233 | * Releases locks and closes connection. |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function close() |
||
| 262 | |||
| 263 | // ------------------------------------------------------------------------ |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Destroy |
||
| 267 | * |
||
| 268 | * Destroys the current session. |
||
| 269 | * |
||
| 270 | * @param string $session_id Session ID |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function destroy($session_id) |
||
| 288 | |||
| 289 | // ------------------------------------------------------------------------ |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Garbage Collector |
||
| 293 | * |
||
| 294 | * Deletes expired sessions |
||
| 295 | * |
||
| 296 | * @param int $maxlifetime Maximum lifetime of sessions |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function gc($maxlifetime) |
||
| 304 | |||
| 305 | // ------------------------------------------------------------------------ |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get lock |
||
| 309 | * |
||
| 310 | * Acquires an (emulated) lock. |
||
| 311 | * |
||
| 312 | * @param string $session_id Session ID |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function _get_lock($session_id) |
||
| 357 | |||
| 358 | // ------------------------------------------------------------------------ |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Release lock |
||
| 362 | * |
||
| 363 | * Releases a previously acquired lock |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | View Code Duplication | protected function _release_lock() |
|
| 383 | |||
| 384 | } |
||
| 385 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.