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_memcached_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_memcached_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class CI_Session_memcached_driver extends CI_Session_driver implements SessionHandlerInterface { |
||
50 | |||
51 | /** |
||
52 | * Memcached instance |
||
53 | * |
||
54 | * @var Memcached |
||
55 | */ |
||
56 | protected $_memcached; |
||
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) |
||
94 | |||
95 | // ------------------------------------------------------------------------ |
||
96 | |||
97 | /** |
||
98 | * Open |
||
99 | * |
||
100 | * Sanitizes save_path and initializes connections. |
||
101 | * |
||
102 | * @param string $save_path Server path(s) |
||
103 | * @param string $name Session cookie name, unused |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function open($save_path, $name) |
||
150 | |||
151 | // ------------------------------------------------------------------------ |
||
152 | |||
153 | /** |
||
154 | * Read |
||
155 | * |
||
156 | * Reads session data and acquires a lock |
||
157 | * |
||
158 | * @param string $session_id Session ID |
||
159 | * @return string Serialized session data |
||
160 | */ |
||
161 | View Code Duplication | public function read($session_id) |
|
175 | |||
176 | // ------------------------------------------------------------------------ |
||
177 | |||
178 | /** |
||
179 | * Write |
||
180 | * |
||
181 | * Writes (create / update) session data |
||
182 | * |
||
183 | * @param string $session_id Session ID |
||
184 | * @param string $session_data Serialized session data |
||
185 | * @return bool |
||
186 | */ |
||
187 | View Code Duplication | public function write($session_id, $session_data) |
|
226 | |||
227 | // ------------------------------------------------------------------------ |
||
228 | |||
229 | /** |
||
230 | * Close |
||
231 | * |
||
232 | * Releases locks and closes connection. |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function close() |
||
252 | |||
253 | // ------------------------------------------------------------------------ |
||
254 | |||
255 | /** |
||
256 | * Destroy |
||
257 | * |
||
258 | * Destroys the current session. |
||
259 | * |
||
260 | * @param string $session_id Session ID |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function destroy($session_id) |
||
274 | |||
275 | // ------------------------------------------------------------------------ |
||
276 | |||
277 | /** |
||
278 | * Garbage Collector |
||
279 | * |
||
280 | * Deletes expired sessions |
||
281 | * |
||
282 | * @param int $maxlifetime Maximum lifetime of sessions |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function gc($maxlifetime) |
||
290 | |||
291 | // ------------------------------------------------------------------------ |
||
292 | |||
293 | /** |
||
294 | * Get lock |
||
295 | * |
||
296 | * Acquires an (emulated) lock. |
||
297 | * |
||
298 | * @param string $session_id Session ID |
||
299 | * @return bool |
||
300 | */ |
||
301 | protected function _get_lock($session_id) |
||
341 | |||
342 | // ------------------------------------------------------------------------ |
||
343 | |||
344 | /** |
||
345 | * Release lock |
||
346 | * |
||
347 | * Releases a previously acquired lock |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | View Code Duplication | protected function _release_lock() |
|
367 | |||
368 | } |
||
369 |