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 ElggSession 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 ElggSession, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ElggSession implements \ArrayAccess { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var SessionInterface |
||
| 44 | */ |
||
| 45 | protected $storage; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \ElggUser|null |
||
| 49 | */ |
||
| 50 | protected $logged_in_user; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $ignore_access = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param SessionInterface $storage The underlying Session implementation |
||
| 61 | * @access private Use elgg_get_session() |
||
| 62 | */ |
||
| 63 | 79 | public function __construct(SessionInterface $storage) { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Start the session |
||
| 69 | * |
||
| 70 | * @return boolean |
||
| 71 | * @throws RuntimeException If session fails to start. |
||
| 72 | * @since 1.9 |
||
| 73 | */ |
||
| 74 | 3 | public function start() { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Migrates the session to a new session id while maintaining session attributes |
||
| 82 | * |
||
| 83 | * @param boolean $destroy Whether to delete the session or let gc handle clean up |
||
| 84 | * @return boolean |
||
| 85 | * @since 1.9 |
||
| 86 | */ |
||
| 87 | 2 | public function migrate($destroy = false) { |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Invalidates the session |
||
| 93 | * |
||
| 94 | * Deletes session data and session persistence. Starts a new session. |
||
| 95 | * |
||
| 96 | * @return boolean |
||
| 97 | * @since 1.9 |
||
| 98 | */ |
||
| 99 | 1 | public function invalidate() { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Has the session been started |
||
| 109 | * |
||
| 110 | * @return boolean |
||
| 111 | * @since 1.9 |
||
| 112 | */ |
||
| 113 | public function isStarted() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the session ID |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | * @since 1.9 |
||
| 122 | */ |
||
| 123 | 2 | public function getId() { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Set the session ID |
||
| 129 | * |
||
| 130 | * @param string $id Session ID |
||
| 131 | * @return void |
||
| 132 | * @since 1.9 |
||
| 133 | */ |
||
| 134 | public function setId($id) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the session name |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | * @since 1.9 |
||
| 143 | */ |
||
| 144 | public function getName() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the session name |
||
| 150 | * |
||
| 151 | * @param string $name Session name |
||
| 152 | * @return void |
||
| 153 | * @since 1.9 |
||
| 154 | */ |
||
| 155 | public function setName($name) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get an attribute of the session |
||
| 161 | * |
||
| 162 | * @param string $name Name of the attribute to get |
||
| 163 | * @param mixed $default Value to return if attribute is not set (default is null) |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | 13 | public function get($name, $default = null) { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set an attribute |
||
| 172 | * |
||
| 173 | * @param string $name Name of the attribute to set |
||
| 174 | * @param mixed $value Value to be set |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | 11 | public function set($name, $value) { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Remove an attribute |
||
| 183 | * |
||
| 184 | * @param string $name The name of the attribute to remove |
||
| 185 | * @return mixed The removed attribute |
||
| 186 | * @since 1.9 |
||
| 187 | */ |
||
| 188 | 2 | public function remove($name) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Alias to offsetUnset() |
||
| 194 | * |
||
| 195 | * @param string $key Name |
||
| 196 | * @return void |
||
| 197 | * @deprecated 1.9 Use remove() |
||
| 198 | */ |
||
| 199 | public function del($key) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Has the attribute been defined |
||
| 206 | * |
||
| 207 | * @param string $name Name of the attribute |
||
| 208 | * @return bool |
||
| 209 | * @since 1.9 |
||
| 210 | */ |
||
| 211 | 3 | public function has($name) { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Sets the logged in user |
||
| 217 | * |
||
| 218 | * @param \ElggUser $user The user who is logged in |
||
| 219 | * @return void |
||
| 220 | * @since 1.9 |
||
| 221 | */ |
||
| 222 | public function setLoggedInUser(\ElggUser $user) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the logged in user |
||
| 229 | * |
||
| 230 | * @return \ElggUser |
||
| 231 | * @since 1.9 |
||
| 232 | */ |
||
| 233 | 37 | public function getLoggedInUser() { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Return the current logged in user by guid. |
||
| 247 | * |
||
| 248 | * @see elgg_get_logged_in_user_entity() |
||
| 249 | * @return int |
||
| 250 | */ |
||
| 251 | 32 | public function getLoggedInUserGuid() { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns whether or not the viewer is currently logged in and an admin user. |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function isAdminLoggedIn() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns whether or not the user is currently logged in |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public function isLoggedIn() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Remove the logged in user |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | * @since 1.9 |
||
| 282 | */ |
||
| 283 | public function removeLoggedInUser() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get current ignore access setting. |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function getIgnoreAccess() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set ignore access. |
||
| 299 | * |
||
| 300 | * @param bool $ignore Ignore access |
||
| 301 | * |
||
| 302 | * @return bool Previous setting |
||
| 303 | */ |
||
| 304 | 3 | public function setIgnoreAccess($ignore = true) { |
|
| 312 | |||
| 313 | // @codingStandardsIgnoreStart |
||
| 314 | /** |
||
| 315 | * Alias of getIgnoreAccess() |
||
| 316 | * |
||
| 317 | * @todo remove with elgg_get_access_object() |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | * @deprecated 1.8 Use elgg_get_ignore_access() |
||
| 321 | */ |
||
| 322 | public function get_ignore_access() { |
||
| 325 | // @codingStandardsIgnoreEnd |
||
| 326 | |||
| 327 | // @codingStandardsIgnoreStart |
||
| 328 | /** |
||
| 329 | * Alias of setIgnoreAccess() |
||
| 330 | * |
||
| 331 | * @todo remove with elgg_get_access_object() |
||
| 332 | * |
||
| 333 | * @param bool $ignore Ignore access |
||
| 334 | * |
||
| 335 | * @return bool Previous setting |
||
| 336 | * |
||
| 337 | * @deprecated 1.8 Use elgg_set_ignore_access() |
||
| 338 | */ |
||
| 339 | public function set_ignore_access($ignore = true) { |
||
| 342 | // @codingStandardsIgnoreEnd |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Adds a token to the session |
||
| 346 | * |
||
| 347 | * This is used in creation of CSRF token, and is passed to the client to allow validating tokens |
||
| 348 | * later, even if the PHP session was destroyed. |
||
| 349 | * |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | 3 | protected function generateSessionToken() { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Test if property is set either as an attribute or metadata. |
||
| 361 | * |
||
| 362 | * @param string $key The name of the attribute or metadata. |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | * @deprecated 1.9 Use has() |
||
| 366 | */ |
||
| 367 | public function __isset($key) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set a value, go straight to session. |
||
| 375 | * |
||
| 376 | * @param string $key Name |
||
| 377 | * @param mixed $value Value |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | * @deprecated 1.9 Use set() |
||
| 381 | */ |
||
| 382 | public function offsetSet($key, $value) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get a variable from either the session, or if its not in the session |
||
| 389 | * attempt to get it from an api call. |
||
| 390 | * |
||
| 391 | * @see \ArrayAccess::offsetGet() |
||
| 392 | * |
||
| 393 | * @param mixed $key Name |
||
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | * @deprecated 1.9 Use get() |
||
| 397 | */ |
||
| 398 | public function offsetGet($key) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Unset a value from the cache and the session. |
||
| 437 | * |
||
| 438 | * @see \ArrayAccess::offsetUnset() |
||
| 439 | * |
||
| 440 | * @param mixed $key Name |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | * @deprecated 1.9 Use remove() |
||
| 444 | */ |
||
| 445 | public function offsetUnset($key) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return whether the value is set in either the session or the cache. |
||
| 452 | * |
||
| 453 | * @see \ArrayAccess::offsetExists() |
||
| 454 | * |
||
| 455 | * @param int $offset Offset |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | * @deprecated 1.9 Use has() |
||
| 459 | */ |
||
| 460 | public function offsetExists($offset) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get an isolated ElggSession that does not persist between requests |
||
| 482 | * |
||
| 483 | * @return self |
||
| 484 | */ |
||
| 485 | 79 | public static function getMock() { |
|
| 490 | } |
||
| 491 |
This check marks private properties in classes that are never used. Those properties can be removed.