Complex classes like USession 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 USession, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class USession { |
||
| 15 | private static $name; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Returns an array stored in session variable as $arrayKey |
||
| 19 | * |
||
| 20 | * @param string $arrayKey |
||
| 21 | * the key of the array to return |
||
| 22 | * @return array |
||
| 23 | */ |
||
| 24 | public static function getArray($arrayKey) { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Adds or removes a value from an array in session |
||
| 37 | * |
||
| 38 | * @param string $arrayKey |
||
| 39 | * the key of the array to add or remove in |
||
| 40 | * @param mixed $value |
||
| 41 | * the value to add |
||
| 42 | * @param boolean $add |
||
| 43 | * If true, adds otherwise removes |
||
| 44 | * @return boolean |
||
| 45 | */ |
||
| 46 | public static function addOrRemoveValueFromArray($arrayKey, $value, $add = true) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Removes a value from an array in session |
||
| 62 | * |
||
| 63 | * @param string $arrayKey |
||
| 64 | * the key of the array to remove in |
||
| 65 | * @param mixed $value |
||
| 66 | * the value to remove |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | public static function removeValueFromArray($arrayKey, $value) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Adds a value from an array in session |
||
| 75 | * |
||
| 76 | * @param string $arrayKey |
||
| 77 | * the key of the array to add in |
||
| 78 | * @param mixed $value |
||
| 79 | * the value to add |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | public static function addValueToArray($arrayKey, $value) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Sets a boolean value at key position in session |
||
| 88 | * |
||
| 89 | * @param string $key |
||
| 90 | * the key to add or set in |
||
| 91 | * @param mixed $value |
||
| 92 | * the value to set |
||
| 93 | * @return boolean |
||
| 94 | */ |
||
| 95 | public static function setBoolean($key, $value) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns a boolean stored at the key position in session |
||
| 102 | * |
||
| 103 | * @param string $key |
||
| 104 | * the key to add or set |
||
| 105 | * @return boolean |
||
| 106 | */ |
||
| 107 | public static function getBoolean($key) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the value stored at the key position in session |
||
| 118 | * |
||
| 119 | * @param string $key |
||
| 120 | * the key to retreive |
||
| 121 | * @param mixed $default |
||
| 122 | * the default value to return if the key does not exists in session |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public static function session($key, $default = NULL) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the value stored at the key position in session |
||
| 132 | * |
||
| 133 | * @param string $key |
||
| 134 | * the key to retreive |
||
| 135 | * @param mixed $default |
||
| 136 | * the default value to return if the key does not exists in session |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | public static function get($key, $default = NULL) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Adds or sets a value to the Session at position $key |
||
| 146 | * |
||
| 147 | * @param string $key |
||
| 148 | * the key to add or set |
||
| 149 | * @param mixed $value |
||
| 150 | */ |
||
| 151 | public static function set($key, $value) { |
||
| 155 | |||
| 156 | public static function setTmp($key,$value,$duration){ |
||
| 166 | |||
| 167 | public static function getTmp($key,$default=null){ |
||
| 181 | |||
| 182 | public static function getTimeout($key){ |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Deletes the key in Session |
||
| 199 | * |
||
| 200 | * @param string $key |
||
| 201 | * the key to delete |
||
| 202 | */ |
||
| 203 | public static function delete($key) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Increment the value at the key index in session |
||
| 210 | * |
||
| 211 | * @param string $key |
||
| 212 | * @param number $inc |
||
| 213 | * @return number |
||
| 214 | */ |
||
| 215 | public static function inc($key, $inc = 1) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Decrement the value at the key index in session |
||
| 221 | * |
||
| 222 | * @param string $key |
||
| 223 | * @param number $dec |
||
| 224 | * @return number |
||
| 225 | */ |
||
| 226 | public static function dec($key, $dec = 1) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Adds a string at the end of the value at the key index in session |
||
| 232 | * |
||
| 233 | * @param string $key |
||
| 234 | * @param string $str |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public static function concat($key, $str, $default = NULL) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Applies a callback function to the value at the key index in session |
||
| 243 | * |
||
| 244 | * @param string $key |
||
| 245 | * @param string|callable $callback |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public static function apply($key, $callback, $default = NULL) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Apply a user supplied function to every member of Session array |
||
| 262 | * |
||
| 263 | * @param callable $callback |
||
| 264 | * @param mixed $userData |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public static function Walk($callback, $userData = null) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Replaces elements from Session array with $keyAndValues |
||
| 275 | * |
||
| 276 | * @param array $keyAndValues |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public static function replace($keyAndValues) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the associative array of session vars |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public static function getAll() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Start new or resume existing session |
||
| 297 | * |
||
| 298 | * @param string|null $name |
||
| 299 | * the name of the session |
||
| 300 | */ |
||
| 301 | public static function start($name = null) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns true if the session is started |
||
| 315 | * |
||
| 316 | * @return boolean |
||
| 317 | */ |
||
| 318 | public static function isStarted() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns true if the key exists in Session |
||
| 324 | * |
||
| 325 | * @param string $key |
||
| 326 | * the key to test |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | public static function exists($key) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Initialize the key in Session if key does not exists |
||
| 336 | * @param string $key |
||
| 337 | * @param mixed $value |
||
| 338 | * @return mixed |
||
| 339 | */ |
||
| 340 | public static function init($key,$value){ |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Terminates the active session |
||
| 349 | */ |
||
| 350 | public static function terminate() { |
||
| 362 | } |
||
| 363 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: