We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 62 | 
| Total Lines | 340 | 
| 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 { | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * Smr\Session constructor.  | 
            ||
| 58 | * Not intended to be constructed by hand. Use Smr\Session::getInstance().  | 
            ||
| 59 | */  | 
            ||
| 60 | 	public function __construct() { | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | 	public function fetchVarInfo() : void { | 
            ||
| 106 | 		$this->db->query('SELECT * FROM active_session WHERE session_id = ' . $this->db->escapeString($this->sessionID) . ' LIMIT 1'); | 
            ||
| 107 | 		if ($this->db->nextRecord()) { | 
            ||
| 108 | $this->generate = false;  | 
            ||
| 109 | 			$this->sessionID = $this->db->getField('session_id'); | 
            ||
| 110 | 			$this->accountID = $this->db->getInt('account_id'); | 
            ||
| 111 | 			$this->gameID = $this->db->getInt('game_id'); | 
            ||
| 112 | 			$this->lastAccessed = $this->db->getInt('last_accessed'); | 
            ||
| 113 | 			$this->lastSN = $this->db->getField('last_sn'); | 
            ||
| 114 | // We may not have ajax_returns if ajax was disabled  | 
            ||
| 115 | 			$this->previousAjaxReturns = $this->db->getObject('ajax_returns', true, true); | 
            ||
| 116 | |||
| 117 | 			$this->var = $this->db->getObject('session_var', true); | 
            ||
| 118 | |||
| 119 | 			foreach ($this->var as $key => $value) { | 
            ||
| 120 | 				if ($value['RemainingPageLoads'] < 0) { | 
            ||
| 121 | //This link is no longer valid  | 
            ||
| 122 | unset($this->var[$key]);  | 
            ||
| 123 | 				} else { | 
            ||
| 124 | // The following is skipped for the current SN, because:  | 
            ||
| 125 | // a) If we decremented RemainingPageLoads, we wouldn't be  | 
            ||
| 126 | // able to refresh the current page.  | 
            ||
| 127 | // b) If we register its CommonID and then subsequently  | 
            ||
| 128 | // modify its data (which is quite common for the  | 
            ||
| 129 | // "current var"), the CommonID is not updated. Then any  | 
            ||
| 130 | // var with the same data as the original will wrongly  | 
            ||
| 131 | // share its CommonID.  | 
            ||
| 132 | 					if ($key !== $this->SN) { | 
            ||
| 133 | $this->var[$key]['RemainingPageLoads'] -= 1;  | 
            ||
| 134 | 						if (isset($value['CommonID'])) { | 
            ||
| 135 | $this->commonIDs[$value['CommonID']] = $key;  | 
            ||
| 136 | }  | 
            ||
| 137 | }  | 
            ||
| 138 | }  | 
            ||
| 139 | }  | 
            ||
| 140 | 		} else { | 
            ||
| 141 | $this->generate = true;  | 
            ||
| 142 | $this->accountID = 0;  | 
            ||
| 143 | $this->gameID = 0;  | 
            ||
| 144 | $this->var = array();  | 
            ||
| 145 | }  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | 	public function update() : void { | 
            ||
| 149 | 		foreach ($this->var as $key => $value) { | 
            ||
| 150 | 			if ($value['RemainingPageLoads'] <= 0) { | 
            ||
| 151 | //This link was valid this load but will not be in the future, removing it now saves database space and data transfer.  | 
            ||
| 152 | unset($this->var[$key]);  | 
            ||
| 153 | }  | 
            ||
| 154 | }  | 
            ||
| 155 | 		if (!$this->generate) { | 
            ||
| 156 | 			$this->db->query('UPDATE active_session SET account_id=' . $this->db->escapeNumber($this->accountID) . ',game_id=' . $this->db->escapeNumber($this->gameID) . (!USING_AJAX ? ',last_accessed=' . $this->db->escapeNumber(Epoch::time()) : '') . ',session_var=' . $this->db->escapeObject($this->var, true) . | 
            ||
| 157 | ',last_sn=' . $this->db->escapeString($this->SN) .  | 
            ||
| 158 | ' WHERE session_id=' . $this->db->escapeString($this->sessionID) . (USING_AJAX ? ' AND last_sn=' . $this->db->escapeString($this->lastSN) : '') . ' LIMIT 1');  | 
            ||
| 159 | 		} else { | 
            ||
| 160 | 			$this->db->query('DELETE FROM active_session WHERE account_id = ' . $this->db->escapeNumber($this->accountID) . ' AND game_id = ' . $this->db->escapeNumber($this->gameID)); | 
            ||
| 161 | 			$this->db->query('INSERT INTO active_session (session_id, account_id, game_id, last_accessed, session_var) VALUES(' . $this->db->escapeString($this->sessionID) . ',' . $this->db->escapeNumber($this->accountID) . ',' . $this->db->escapeNumber($this->gameID) . ',' . $this->db->escapeNumber(Epoch::time()) . ',' . $this->db->escapeObject($this->var, true) . ')'); | 
            ||
| 162 | $this->generate = false;  | 
            ||
| 163 | }  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Uniquely identifies the session in the database.  | 
            ||
| 168 | */  | 
            ||
| 169 | 	public function getSessionID() : string { | 
            ||
| 170 | return $this->sessionID;  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Returns the Game ID associated with the session.  | 
            ||
| 175 | */  | 
            ||
| 176 | 	public function getGameID() : int { | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Returns true if the session is inside a game, false otherwise.  | 
            ||
| 182 | */  | 
            ||
| 183 | 	public function hasGame() : bool { | 
            ||
| 184 | return $this->gameID != 0;  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | 	public function hasAccount() : bool { | 
            ||
| 188 | return $this->accountID > 0;  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | 	public function getAccountID() : int { | 
            ||
| 192 | return $this->accountID;  | 
            ||
| 193 | }  | 
            ||
| 194 | |||
| 195 | 	public function getAccount() : AbstractSmrAccount { | 
            ||
| 196 | return SmrAccount::getAccount($this->accountID);  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | 	public function getPlayer(bool $forceUpdate = false) : AbstractSmrPlayer { | 
            ||
| 200 | return SmrPlayer::getPlayer($this->accountID, $this->gameID, $forceUpdate);  | 
            ||
| 201 | }  | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Sets the `accountID` attribute of this session.  | 
            ||
| 205 | */  | 
            ||
| 206 | 	public function setAccount(AbstractSmrAccount $account) : void { | 
            ||
| 207 | $this->accountID = $account->getAccountID();  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * Updates the `gameID` attribute of the session and deletes any other  | 
            ||
| 212 | * active sessions in this game for this account.  | 
            ||
| 213 | */  | 
            ||
| 214 | 	public function updateGame(int $gameID) : void { | 
            ||
| 215 | 		if ($this->gameID == $gameID) { | 
            ||
| 216 | return;  | 
            ||
| 217 | }  | 
            ||
| 218 | $this->gameID = $gameID;  | 
            ||
| 219 | 		$this->db->query('DELETE FROM active_session WHERE account_id = ' . $this->db->escapeNumber($this->accountID) . ' AND game_id = ' . $this->gameID); | 
            ||
| 220 | 		$this->db->query('UPDATE active_session SET game_id=' . $this->db->escapeNumber($this->gameID) . ' WHERE session_id=' . $this->db->escapeString($this->sessionID)); | 
            ||
| 221 | }  | 
            ||
| 222 | |||
| 223 | /**  | 
            ||
| 224 | * The SN is the URL parameter that defines the page being requested.  | 
            ||
| 225 | */  | 
            ||
| 226 | 	public function getSN() : string { | 
            ||
| 227 | return $this->SN;  | 
            ||
| 228 | }  | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * Returns true if the current SN is different than the previous SN.  | 
            ||
| 232 | */  | 
            ||
| 233 | 	public function hasChangedSN() : bool { | 
            ||
| 234 | return $this->SN != $this->lastSN;  | 
            ||
| 235 | }  | 
            ||
| 236 | |||
| 237 | 	public function destroy() : void { | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | 	public function getLastAccessed() : int { | 
            ||
| 245 | return $this->lastAccessed;  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Check if the session has a var associated with the current SN.  | 
            ||
| 250 | */  | 
            ||
| 251 | 	public function hasCurrentVar() : bool { | 
            ||
| 252 | return isset($this->var[$this->SN]);  | 
            ||
| 253 | }  | 
            ||
| 254 | |||
| 255 | /**  | 
            ||
| 256 | * Returns the session var associated with the current SN.  | 
            ||
| 257 | */  | 
            ||
| 258 | 	public function getCurrentVar() : Page { | 
            ||
| 259 | return $this->var[$this->SN];  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Gets a var from $var, $_REQUEST, or $default. Then stores it in the  | 
            ||
| 264 | * session so that it can still be retrieved when the page auto-refreshes.  | 
            ||
| 265 | * This is the recommended way to get $_REQUEST data for display pages.  | 
            ||
| 266 | * For processing pages, see the Request class.  | 
            ||
| 267 | */  | 
            ||
| 268 | 	public function getRequestVar(string $varName, string $default = null) : string { | 
            ||
| 269 | $result = Request::getVar($varName, $default);  | 
            ||
| 270 | $this->updateVar($varName, $result);  | 
            ||
| 271 | return $result;  | 
            ||
| 272 | }  | 
            ||
| 273 | |||
| 274 | 	public function getRequestVarInt(string $varName, int $default = null) : int { | 
            ||
| 275 | $result = Request::getVarInt($varName, $default);  | 
            ||
| 276 | $this->updateVar($varName, $result);  | 
            ||
| 277 | return $result;  | 
            ||
| 278 | }  | 
            ||
| 279 | |||
| 280 | 	public function getRequestVarIntArray(string $varName, array $default = null) : array { | 
            ||
| 281 | $result = Request::getVarIntArray($varName, $default);  | 
            ||
| 282 | $this->updateVar($varName, $result);  | 
            ||
| 283 | return $result;  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * Replace the global $var with the given $container.  | 
            ||
| 288 | */  | 
            ||
| 289 | 	public function setCurrentVar(Page $container) : void { | 
            ||
| 290 | $var = $this->hasCurrentVar() ? $this->getCurrentVar() : null;  | 
            ||
| 291 | |||
| 292 | //Do not allow sharing SN, useful for forwarding.  | 
            ||
| 293 | 		if (isset($var['CommonID'])) { | 
            ||
| 294 | unset($this->commonIDs[$var['CommonID']]); //Do not store common id for reset page, to allow refreshing to always give the same page in response  | 
            ||
| 295 | }  | 
            ||
| 296 | 		if (!isset($container['RemainingPageLoads'])) { | 
            ||
| 297 | $container['RemainingPageLoads'] = 1; // Allow refreshing  | 
            ||
| 298 | }  | 
            ||
| 299 | 		if (!isset($container['PreviousRequestTime'])) { | 
            ||
| 300 | 			if (isset($var['PreviousRequestTime'])) { | 
            ||
| 301 | $container['PreviousRequestTime'] = $var['PreviousRequestTime']; // Copy across the previous request time if not explicitly set.  | 
            ||
| 302 | }  | 
            ||
| 303 | }  | 
            ||
| 304 | |||
| 305 | $this->var[$this->SN] = $container;  | 
            ||
| 306 | }  | 
            ||
| 307 | |||
| 308 | 	public function updateVar(string $key, mixed $value) : void { | 
            ||
| 309 | $var = $this->getCurrentVar();  | 
            ||
| 310 | 		if ($value === null) { | 
            ||
| 311 | 			if (isset($var[$key])) { | 
            ||
| 312 | unset($var[$key]);  | 
            ||
| 313 | }  | 
            ||
| 314 | 		} else { | 
            ||
| 315 | $var[$key] = $value;  | 
            ||
| 316 | }  | 
            ||
| 317 | }  | 
            ||
| 318 | |||
| 319 | 	public function clearLinks() : void { | 
            ||
| 322 | }  | 
            ||
| 323 | |||
| 324 | 	public function addLink(Page $container) : string { | 
            ||
| 325 | $sn = $this->generateSN($container);  | 
            ||
| 326 | $this->var[$sn] = $container;  | 
            ||
| 327 | return $sn;  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | 	protected function generateSN(Page $container) : string { | 
            ||
| 331 | 		if (isset($this->commonIDs[$container['CommonID']])) { | 
            ||
| 332 | $sn = $this->commonIDs[$container['CommonID']];  | 
            ||
| 333 | $container['PreviousRequestTime'] = isset($this->var[$sn]) ? $this->var[$sn]['PreviousRequestTime'] : Epoch::microtime();  | 
            ||
| 334 | 		} else { | 
            ||
| 335 | 			do { | 
            ||
| 336 | $sn = random_alphabetic_string(6);  | 
            ||
| 337 | } while (isset($this->var[$sn]));  | 
            ||
| 338 | $container['PreviousRequestTime'] = Epoch::microtime();  | 
            ||
| 339 | }  | 
            ||
| 340 | $this->commonIDs[$container['CommonID']] = $sn;  | 
            ||
| 341 | return $sn;  | 
            ||
| 342 | }  | 
            ||
| 343 | |||
| 344 | 	public function addAjaxReturns(string $element, string $contents) : bool { | 
            ||
| 345 | $this->ajaxReturns[$element] = $contents;  | 
            ||
| 346 | return isset($this->previousAjaxReturns[$element]) && $this->previousAjaxReturns[$element] == $contents;  | 
            ||
| 347 | }  | 
            ||
| 348 | |||
| 349 | 	public function saveAjaxReturns() : void { | 
            ||
| 355 | }  | 
            ||
| 356 | }  | 
            ||
| 357 |