Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

CurrentSector::build()   F
last analyzed

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 157
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 26
eloc 81
c 1
b 1
f 0
nc 921600
nop 2
dl 0
loc 157
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player;
4
5
use AbstractSmrPlayer;
6
use Menu;
7
use Smr\Database;
8
use Smr\Epoch;
9
use Smr\Page\PlayerPage;
10
use Smr\Page\ReusableTrait;
11
use Smr\Pages\Player\Planet\Main as PlanetMain;
12
use Smr\Template;
13
use Smr\TurnsLevel;
14
use SmrGame;
15
16
class CurrentSector extends PlayerPage {
17
18
	use ReusableTrait;
19
20
	public string $file = 'current_sector.php';
21
22
	/** @var array<int> */
23
	private array $unreadMissions;
24
	private ?string $attackMessage = null;
25
26
	public function __construct(
27
		private readonly ?string $message = null,
28
		private readonly ?string $errorMessage = null,
29
		private readonly ?string $missionMessage = null,
30
		private readonly ?string $tradeMessage = null,
31
		private readonly bool $showForceRefreshMessage = false
32
	) {}
33
34
	public function build(AbstractSmrPlayer $player, Template $template): void {
35
		$sector = $player->getSector();
36
37
		// If on a planet, forward to planet_main.php
38
		if ($player->isLandedOnPlanet()) {
39
			(new PlanetMain($this->message, $this->errorMessage))->go();
40
		}
41
42
		$template->assign('SpaceView', true);
43
44
		$template->assign('PageTopic', 'Current Sector: ' . $player->getSectorID() . ' (' . $sector->getGalaxy()->getDisplayName() . ')');
45
46
		Menu::navigation($player);
47
48
		// *******************************************
49
		// *
50
		// * Sector List
51
		// *
52
		// *******************************************
53
54
		// Sector links
55
		$links = [];
56
		$links['Up'] = ['ID' => $sector->getLinkUp()];
57
		$links['Right'] = ['ID' => $sector->getLinkRight()];
58
		$links['Down'] = ['ID' => $sector->getLinkDown()];
59
		$links['Left'] = ['ID' => $sector->getLinkLeft()];
60
		$links['Warp'] = ['ID' => $sector->getWarp()];
61
62
		$unvisited = [];
63
64
		$db = Database::getInstance();
65
		$dbResult = $db->read('SELECT sector_id FROM player_visited_sector WHERE sector_id IN (' . $db->escapeArray($links) . ') AND ' . $player->getSQL());
66
		foreach ($dbResult->records() as $dbRecord) {
67
			$unvisited[$dbRecord->getInt('sector_id')] = true;
68
		}
69
70
		foreach ($links as $key => $linkArray) {
71
			if ($linkArray['ID'] > 0 && $linkArray['ID'] != $player->getSectorID()) {
72
				if ($player->getLastSectorID() == $linkArray['ID']) {
73
					$class = 'lastVisited';
74
				} elseif (isset($unvisited[$linkArray['ID']])) {
75
					$class = 'unvisited';
76
				} else {
77
					$class = 'visited';
78
				}
79
				$links[$key]['Class'] = $class;
80
			}
81
		}
82
83
		$template->assign('Sectors', $links);
84
85
		doTickerAssigns($template, $player, $db);
86
87
		$this->unreadMissions ??= $player->markMissionsRead();
88
		$template->assign('UnreadMissions', $this->unreadMissions);
89
90
		// *******************************************
91
		// *
92
		// * Force and other Results
93
		// *
94
		// *******************************************
95
		$game = SmrGame::getGame($player->getGameID());
96
		if (!$game->hasStarted()) {
97
			$turnsMessage = 'The game will start in ' . format_time($game->getStartTime() - Epoch::time()) . '!';
98
		} else {
99
			$turnsMessage = $player->getTurnsLevel()->message();
100
			if ($player->getTurnsLevel() === TurnsLevel::None) {
101
				$turnsMessage .= ' You will gain another turn in ' . format_time($player->getTimeUntilNextTurn()) . '.';
102
			}
103
		}
104
		if (!empty($turnsMessage)) {
105
			$template->assign('TurnsMessage', $turnsMessage);
106
		}
107
108
		$protectionMessage = '';
109
		if ($player->getNewbieTurns()) {
110
			if ($player->getNewbieTurns() < 25) {
111
				$protectionMessage = '<span class="blue">PROTECTION</span>: You are almost out of <span class="green">NEWBIE</span> protection.';
112
			} else {
113
				$protectionMessage = '<span class="blue">PROTECTION</span>: You are under <span class="green">NEWBIE</span> protection.';
114
			}
115
		} elseif ($player->hasFederalProtection()) {
116
			$protectionMessage = '<span class="blue">PROTECTION</span>: You are under <span class="blue">FEDERAL</span> protection.';
117
		} elseif ($sector->offersFederalProtection()) {
118
			$protectionMessage = '<span class="blue">PROTECTION</span>: You are <span class="red">NOT</span> under protection.';
119
		}
120
121
		if (!empty($protectionMessage)) {
122
			$template->assign('ProtectionMessage', $protectionMessage);
123
		}
124
125
		//enableProtectionDependantRefresh($template,$player);
126
127
		// Do we have an unseen attack message to store in this var?
128
		$dbResult = $db->read('SELECT * FROM sector_message WHERE ' . $player->getSQL());
129
		if ($dbResult->hasRecord()) {
130
			$this->attackMessage = $dbResult->record()->getString('message');
131
			$db->write('DELETE FROM sector_message WHERE ' . $player->getSQL());
132
		}
133
134
		if ($this->attackMessage !== null) {
135
			checkForAttackMessage($this->attackMessage, $player);
136
		}
137
		if ($this->showForceRefreshMessage) {
138
			$template->assign('ForceRefreshMessage', getForceRefreshMessage($player));
139
		}
140
		if ($this->missionMessage !== null) {
141
			$template->assign('MissionMessage', $this->missionMessage);
142
		}
143
		if ($this->message !== null) {
144
			$template->assign('VarMessage', bbifyMessage($this->message));
145
		}
146
147
		//error msgs take precedence
148
		if ($this->errorMessage !== null) {
149
			$template->assign('ErrorMessage', $this->errorMessage);
150
		}
151
152
		// *******************************************
153
		// *
154
		// * Trade Result
155
		// *
156
		// *******************************************
157
158
		if ($this->tradeMessage !== null) {
159
			$template->assign('TradeMessage', $this->tradeMessage);
160
		}
161
162
		// *******************************************
163
		// *
164
		// * Ports
165
		// *
166
		// *******************************************
167
168
		if ($sector->hasPort()) {
169
			$port = $sector->getPort();
170
			$template->assign('PortIsAtWar', $player->getRelation($port->getRaceID()) < RELATIONS_WAR);
171
		}
172
173
		// *******************************************
174
		// *
175
		// * Ships
176
		// *
177
		// *******************************************
178
		$otherPlayers = $sector->getOtherTraders($player);
179
		$visiblePlayers = [];
180
		$cloakedPlayers = [];
181
		foreach ($otherPlayers as $accountID => $otherPlayer) {
182
			if ($player->canSee($otherPlayer)) {
183
				$visiblePlayers[$accountID] = $otherPlayer;
184
			} else {
185
				$cloakedPlayers[$accountID] = $otherPlayer;
186
			}
187
		}
188
		$template->assign('VisiblePlayers', $visiblePlayers);
189
		$template->assign('CloakedPlayers', $cloakedPlayers);
190
		$template->assign('SectorPlayersLabel', 'Ships');
191
	}
192
193
}
194
195
196
function getForceRefreshMessage(AbstractSmrPlayer $player): string {
197
	$db = Database::getInstance();
198
	$dbResult = $db->read('SELECT refresh_at FROM sector_has_forces WHERE refresh_at > ' . $db->escapeNumber(Epoch::time()) . ' AND sector_id = ' . $db->escapeNumber($player->getSectorID()) . ' AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND refresher = ' . $db->escapeNumber($player->getAccountID()) . ' ORDER BY refresh_at DESC LIMIT 1');
199
	if ($dbResult->hasRecord()) {
200
		$remainingTime = $dbResult->record()->getInt('refresh_at') - Epoch::time();
201
		$forceRefreshMessage = '<span class="green">REFRESH</span>: All forces will be refreshed in ' . $remainingTime . ' seconds.';
202
	} else {
203
		$forceRefreshMessage = '<span class="green">REFRESH</span>: All forces have finished refreshing.';
204
	}
205
	return $forceRefreshMessage;
206
}
207
208
function checkForAttackMessage(string $msg, AbstractSmrPlayer $player): void {
209
	$contains = 0;
210
	$msg = str_replace('[ATTACK_RESULTS]', '', $msg, $contains);
211
	if ($contains > 0) {
212
		// $msg now contains only the log_id, if there is one
213
		$logID = str2int($msg);
0 ignored issues
show
Bug introduced by
The function str2int was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
		$logID = /** @scrutinizer ignore-call */ str2int($msg);
Loading history...
214
215
		$template = Template::getInstance();
216
		$db = Database::getInstance();
217
		$dbResult = $db->read('SELECT sector_id,result,type FROM combat_logs WHERE log_id=' . $db->escapeNumber($logID) . ' LIMIT 1');
218
		if ($dbResult->hasRecord()) {
219
			$dbRecord = $dbResult->record();
220
			if ($player->getSectorID() == $dbRecord->getInt('sector_id')) {
221
				$results = $dbRecord->getObject('result', true);
222
				$template->assign('AttackResultsType', $dbRecord->getString('type'));
223
				$template->assign('AttackResults', $results);
224
				$template->assign('AttackLogLink', linkCombatLog($logID));
225
			}
226
		}
227
	}
228
}
229