We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 66 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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.
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 declare(strict_types=1); |
||
| 15 | class Session { |
||
| 16 | |||
| 17 | const TIME_BEFORE_EXPIRY = 3600; |
||
| 18 | |||
| 19 | private const URL_LOAD_DELAY = array( |
||
| 20 | 'configure_hardware.php' => .4, |
||
| 21 | 'forces_drop.php' => .4, |
||
| 22 | 'forces_drop_processing.php' => .5, |
||
| 23 | 'forces_refresh_processing.php' => .4, |
||
| 24 | 'sector_jump_processing.php' => .4, |
||
| 25 | 'sector_move_processing.php' => .4, |
||
| 26 | 'sector_scan.php' => .4, |
||
| 27 | 'shop_goods_processing.php' => .4, |
||
| 28 | 'trader_attack_processing.php' => .75, |
||
| 29 | 'trader_examine.php' => .75 |
||
| 30 | ); |
||
| 31 | |||
| 32 | protected Database $db; |
||
| 33 | |||
| 34 | private string $sessionID; |
||
| 35 | private int $gameID; |
||
| 36 | private array $var; |
||
| 37 | private array $commonIDs = []; |
||
| 38 | private bool $generate; |
||
| 39 | private string $SN; |
||
| 40 | private string $lastSN; |
||
| 41 | private int $accountID; |
||
| 42 | private int $lastAccessed; |
||
| 43 | |||
| 44 | protected ?array $previousAjaxReturns; |
||
| 45 | protected array $ajaxReturns = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Return the Smr\Session in the DI container. |
||
| 49 | * If one does not exist yet, it will be created. |
||
| 50 | * This is the intended way to construct this class. |
||
| 51 | */ |
||
| 52 | public static function getInstance() : self { |
||
| 53 | return DiContainer::get(self::class); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Smr\Session constructor. |
||
| 58 | * Not intended to be constructed by hand. Use Smr\Session::getInstance(). |
||
| 59 | */ |
||
| 60 | public function __construct() { |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function fetchVarInfo() : void { |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function update() : void { |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Uniquely identifies the session in the database. |
||
| 156 | */ |
||
| 157 | public function getSessionID() : string { |
||
| 158 | return $this->sessionID; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the Game ID associated with the session. |
||
| 163 | */ |
||
| 164 | public function getGameID() : int { |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns true if the session is inside a game, false otherwise. |
||
| 170 | */ |
||
| 171 | public function hasGame() : bool { |
||
| 172 | return $this->gameID != 0; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function hasAccount() : bool { |
||
| 176 | return $this->accountID > 0; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getAccountID() : int { |
||
| 180 | return $this->accountID; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getAccount() : AbstractSmrAccount { |
||
| 184 | return SmrAccount::getAccount($this->accountID); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getPlayer(bool $forceUpdate = false) : AbstractSmrPlayer { |
||
| 188 | return SmrPlayer::getPlayer($this->accountID, $this->gameID, $forceUpdate); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets the `accountID` attribute of this session. |
||
| 193 | */ |
||
| 194 | public function setAccount(AbstractSmrAccount $account) : void { |
||
| 195 | $this->accountID = $account->getAccountID(); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Updates the `gameID` attribute of the session and deletes any other |
||
| 200 | * active sessions in this game for this account. |
||
| 201 | */ |
||
| 202 | public function updateGame(int $gameID) : void { |
||
| 203 | if ($this->gameID == $gameID) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | $this->gameID = $gameID; |
||
| 207 | $this->db->query('DELETE FROM active_session WHERE account_id = ' . $this->db->escapeNumber($this->accountID) . ' AND game_id = ' . $this->gameID); |
||
| 208 | $this->db->query('UPDATE active_session SET game_id=' . $this->db->escapeNumber($this->gameID) . ' WHERE session_id=' . $this->db->escapeString($this->sessionID)); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The SN is the URL parameter that defines the page being requested. |
||
| 213 | */ |
||
| 214 | public function getSN() : string { |
||
| 215 | return $this->SN; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns true if the current SN is different than the previous SN. |
||
| 220 | */ |
||
| 221 | public function hasChangedSN() : bool { |
||
| 222 | return $this->SN != $this->lastSN; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function destroy() : void { |
||
| 230 | } |
||
| 231 | |||
| 232 | public function getLastAccessed() : int { |
||
| 233 | return $this->lastAccessed; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if the session has a var associated with the current SN. |
||
| 238 | */ |
||
| 239 | public function findCurrentVar() : bool { |
||
| 240 | if (empty($this->var[$this->SN])) { |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | $var = $this->var[$this->SN]; |
||
| 244 | if (isset($var['body']) && isset($var['CommonID'])) { |
||
| 245 | // if(preg_match('/processing/',$var['body'])) |
||
| 246 | unset($this->commonIDs[$var['CommonID']]); //Do not store common id for current page |
||
| 247 | unset($var['CommonID']); |
||
| 248 | } |
||
| 249 | |||
| 250 | $var['RemainingPageLoads'] += 1; // Allow refreshing |
||
| 251 | |||
| 252 | return true; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the session var associated with the current SN. |
||
| 257 | * Must be called after Session::findCurrentVar sets the current SN. |
||
| 258 | */ |
||
| 259 | public function getCurrentVar() : Page { |
||
| 260 | return $this->var[$this->SN]; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Gets a var from $var, $_REQUEST, or $default. Then stores it in the |
||
| 265 | * session so that it can still be retrieved when the page auto-refreshes. |
||
| 266 | * This is the recommended way to get $_REQUEST data for display pages. |
||
| 267 | * For processing pages, see the Request class. |
||
| 268 | */ |
||
| 269 | public function getRequestVar(string $varName, string $default = null) : string { |
||
| 270 | $result = Request::getVar($varName, $default); |
||
| 271 | $this->updateVar($varName, $result); |
||
| 272 | return $result; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function getRequestVarInt(string $varName, int $default = null) : int { |
||
| 276 | $result = Request::getVarInt($varName, $default); |
||
| 277 | $this->updateVar($varName, $result); |
||
| 278 | return $result; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function getRequestVarIntArray(string $varName, array $default = null) : array { |
||
| 282 | $result = Request::getVarIntArray($varName, $default); |
||
| 283 | $this->updateVar($varName, $result); |
||
| 284 | return $result; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Replace the global $var with the given $container. |
||
| 289 | */ |
||
| 290 | public function setCurrentVar(Page $container, bool $allowUpdate = true) : void { |
||
| 291 | $var = $this->getCurrentVar(); |
||
| 292 | |||
| 293 | //Do not allow sharing SN, useful for forwarding. |
||
| 294 | global $lock; |
||
| 295 | if (isset($var['CommonID'])) { |
||
| 296 | unset($this->commonIDs[$var['CommonID']]); //Do not store common id for reset page, to allow refreshing to always give the same page in response |
||
| 297 | } |
||
| 298 | if (!isset($container['RemainingPageLoads'])) { |
||
| 299 | $container['RemainingPageLoads'] = 1; // Allow refreshing |
||
| 300 | } |
||
| 301 | if (!isset($container['PreviousRequestTime'])) { |
||
| 302 | if (isset($var['PreviousRequestTime'])) { |
||
| 303 | $container['PreviousRequestTime'] = $var['PreviousRequestTime']; // Copy across the previous request time if not explicitly set. |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | $var->exchangeArray($container); |
||
| 308 | if ($allowUpdate && !$lock && !USING_AJAX) { |
||
| 309 | $this->update(); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | public function updateVar(string $key, mixed $value) : void { |
||
| 314 | $var = $this->getCurrentVar(); |
||
| 315 | if ($value === null) { |
||
| 316 | if (isset($var[$key])) { |
||
| 317 | unset($var[$key]); |
||
| 318 | } |
||
| 319 | } else { |
||
| 320 | $var[$key] = $value; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | public function clearLinks() : void { |
||
| 327 | } |
||
| 328 | |||
| 329 | public function addLink(Page $container) : string { |
||
| 330 | $sn = $this->generateSN($container); |
||
| 331 | $this->var[$sn] = $container; |
||
| 332 | return $sn; |
||
| 333 | } |
||
| 334 | |||
| 335 | protected function generateSN(Page $container) : string { |
||
| 336 | if (isset($this->commonIDs[$container['CommonID']])) { |
||
| 337 | $sn = $this->commonIDs[$container['CommonID']]; |
||
| 338 | $container['PreviousRequestTime'] = isset($this->var[$sn]) ? $this->var[$sn]['PreviousRequestTime'] : Epoch::microtime(); |
||
| 339 | } else { |
||
| 340 | do { |
||
| 341 | $sn = random_alphabetic_string(6); |
||
| 342 | } while (isset($this->var[$sn])); |
||
| 343 | $container['PreviousRequestTime'] = Epoch::microtime(); |
||
| 344 | } |
||
| 345 | $this->commonIDs[$container['CommonID']] = $sn; |
||
| 346 | return $sn; |
||
| 347 | } |
||
| 348 | |||
| 349 | public function addAjaxReturns(string $element, string $contents) : bool { |
||
| 350 | $this->ajaxReturns[$element] = $contents; |
||
| 351 | return isset($this->previousAjaxReturns[$element]) && $this->previousAjaxReturns[$element] == $contents; |
||
| 352 | } |
||
| 353 | |||
| 354 | public function saveAjaxReturns() : void { |
||
| 360 | } |
||
| 361 | } |
||
| 362 |