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 Session 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 Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Session{ |
||
|
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * constructor for Session Object. |
||
| 14 | * |
||
| 15 | * @access private |
||
| 16 | */ |
||
| 17 | private function __construct() {} |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Starts the session if not started yet. |
||
| 21 | * |
||
| 22 | * @access public |
||
| 23 | * |
||
| 24 | */ |
||
| 25 | public static function init(){ |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Checks if session data exists and valid or not. |
||
| 34 | * |
||
| 35 | * @access public |
||
| 36 | * @static static method |
||
| 37 | * @param string $ip |
||
| 38 | * @param string $userAgent |
||
| 39 | * @return boolean |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | public static function isSessionValid($ip, $userAgent){ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get IsLoggedIn value(boolean) |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * @static static method |
||
| 79 | * @return boolean |
||
| 80 | * |
||
| 81 | */ |
||
| 82 | public static function getIsLoggedIn(){ |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get User ID. |
||
| 88 | * |
||
| 89 | * @access public |
||
| 90 | * @static static method |
||
| 91 | * @return string|null |
||
| 92 | * |
||
| 93 | */ |
||
| 94 | public static function getUserId(){ |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get User Role |
||
| 100 | * |
||
| 101 | * @access public |
||
| 102 | * @static static method |
||
| 103 | * @return string|null |
||
| 104 | * |
||
| 105 | */ |
||
| 106 | public static function getUserRole(){ |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get CSRF Token |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @static static method |
||
| 115 | * @return string|null |
||
| 116 | * |
||
| 117 | */ |
||
| 118 | public static function getCsrfToken(){ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get CSRF Token generated time |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | * @static static method |
||
| 127 | * @return string|null |
||
| 128 | * |
||
| 129 | */ |
||
| 130 | public static function getCsrfTokenTime(){ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * set session key and value |
||
| 136 | * |
||
| 137 | * @access public |
||
| 138 | * @static static method |
||
| 139 | * @param $key |
||
| 140 | * @param $value |
||
| 141 | * |
||
| 142 | */ |
||
| 143 | public static function set($key, $value){ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * get session value by $key |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * @static static method |
||
| 152 | * @param $key |
||
| 153 | * @return mixed |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | public static function get($key){ |
||
| 159 | |||
| 160 | /** |
||
| 161 | * get session value by $key and destroy it |
||
| 162 | * |
||
| 163 | * @access public |
||
| 164 | * @static static method |
||
| 165 | * @param $key |
||
| 166 | * @return mixed |
||
| 167 | * |
||
| 168 | */ |
||
| 169 | public static function getAndDestroy($key){ |
||
| 182 | |||
| 183 | /** |
||
| 184 | * matches current IP Address with the one stored in the session |
||
| 185 | * |
||
| 186 | * @access public |
||
| 187 | * @static static method |
||
| 188 | * @param string $ip |
||
| 189 | * @return bool |
||
| 190 | * |
||
| 191 | */ |
||
| 192 | private static function validateIPAddress($ip){ |
||
| 200 | |||
| 201 | /** |
||
| 202 | * matches current user agent with the one stored in the session |
||
| 203 | * |
||
| 204 | * @access public |
||
| 205 | * @static static method |
||
| 206 | * @param string $userAgent |
||
| 207 | * @return bool |
||
| 208 | * |
||
| 209 | */ |
||
| 210 | private static function validateUserAgent($userAgent){ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * checks if session has been expired |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * @static static method |
||
| 224 | * @return bool |
||
| 225 | * |
||
| 226 | */ |
||
| 227 | private static function validateSessionExpiry(){ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * checks for session concurrency |
||
| 240 | * |
||
| 241 | * This is done as the following: |
||
| 242 | * UserA logs in with his session id('123') and it will be stored in the database. |
||
| 243 | * Then, UserB logs in also using the same email and password of UserA from another PC, |
||
| 244 | * and also store the session id('456') in the database |
||
| 245 | * |
||
| 246 | * Now, Whenever UserA performs any action, |
||
| 247 | * You then check the session_id() against the last one stored in the database('456'), |
||
| 248 | * If they don't match then log both of them out. |
||
| 249 | * |
||
| 250 | * @access public |
||
| 251 | * @static static method |
||
| 252 | * @return bool |
||
| 253 | * @see Session::updateSessionId() |
||
| 254 | * @see http://stackoverflow.com/questions/6126285/php-stop-concurrent-user-logins |
||
| 255 | */ |
||
| 256 | public static function isConcurrentSessionExists(){ |
||
| 276 | |||
| 277 | /** |
||
| 278 | * get CSRF token and generate a new one if expired |
||
| 279 | * |
||
| 280 | * @access public |
||
| 281 | * @static static method |
||
| 282 | * @return string |
||
| 283 | * |
||
| 284 | */ |
||
| 285 | public static function generateCsrfToken(){ |
||
| 299 | |||
| 300 | /** |
||
| 301 | * reset session id, delete session file on server, and re-assign the values. |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * @static static method |
||
| 305 | * @param array $data |
||
| 306 | * @return string |
||
| 307 | * |
||
| 308 | */ |
||
| 309 | public static function reset($data){ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * update session id in database |
||
| 336 | * |
||
| 337 | * @access public |
||
| 338 | * @static static method |
||
| 339 | * @param string $userId |
||
| 340 | * @param string $sessionId |
||
| 341 | * @return string |
||
| 342 | * |
||
| 343 | */ |
||
| 344 | View Code Duplication | private static function updateSessionId($userId, $sessionId = null){ |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Remove the session |
||
| 356 | * Delete session completely from the browser cookies and destroy it's file on the server |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * @static static method |
||
| 360 | */ |
||
| 361 | public static function remove(){ |
||
| 386 | |||
| 387 | } |
||
| 388 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.